-
Notifications
You must be signed in to change notification settings - Fork 6
/
visualize.py
55 lines (40 loc) · 1.45 KB
/
visualize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
import imageio
import numpy as np
import os
def center_crop(x, current_size, desired_size):
start = int((current_size - desired_size)/2)
return x[:,:, start:(start + desired_size), start:(start + desired_size)]
def convert(img, target_type_min, target_type_max, target_type):
imin = img.min()
imax = img.max()
a = (target_type_max - target_type_min) / (imax - imin)
b = target_type_max - a * imax
new_img = (a * img + b).astype(target_type)
return new_img
def save_image(img, subject, category, repeat, roi):
plt.figure()
plt.imshow(img, aspect='equal')
plt.tight_layout()
plt.axis('off')
output_dir = './img/S%0d'%subject + '/ROI%02d'%roi + '/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
plt.imsave(output_dir + 'C%04d'%category + '_repeat%d'%repeat + '.png', img, format='png')
return
def save_gif(img, subject, category, repeat, roi):
fig = plt.figure()
plt.tight_layout()
plt.axis('off')
ima = []
for cur in img:
im = plt.imshow(cur, animated=True, aspect='equal')
ima.append( [im] )
ani = ArtistAnimation(fig, ima, interval=30, blit=True)
output_dir = output_dir = './img/S%0d'%subject + '/ROI%02d'%roi + '/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
img_gif = convert(np.asarray(img), 0, 255, np.uint8)
imageio.mimwrite(output_dir + 'C%04d'%category + '_repeat%d'%repeat + '.png', img_gif, fps=32)
return