-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.py
76 lines (65 loc) · 2.13 KB
/
Utils.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os, pickle, copy
import numpy as np
import matplotlib.pyplot as plt
def plot_scroll_Image(x):
'''
:param x: input to view of form [rows, columns, # images]
:return:
'''
if x.dtype not in ['float32','float64']:
x = copy.deepcopy(x).astype('float32')
if len(x.shape) > 3:
x = np.squeeze(x)
if len(x.shape) == 3:
if x.shape[0] != x.shape[1]:
x = np.transpose(x,[1,2,0])
elif x.shape[0] == x.shape[2]:
x = np.transpose(x, [1, 2, 0])
fig, ax = plt.subplots(1, 1)
if len(x.shape) == 2:
x = np.expand_dims(x,axis=0)
tracker = IndexTracker(ax, x)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
return fig,tracker
#Image is input in the form of [#images,512,512,#channels]
class IndexTracker(object):
def __init__(self, ax, X):
self.ax = ax
ax.set_title('use scroll wheel to navigate images')
self.X = X
rows, cols, self.slices = X.shape
self.ind = np.where(self.X != 0)[-1]
if len(self.ind) > 0:
self.ind = self.ind[len(self.ind)//2]
else:
self.ind = self.slices//2
self.im = ax.imshow(self.X[:, :, self.ind],cmap='gray')
self.update()
def onscroll(self, event):
print("%s %s" % (event.button, event.step))
if event.button == 'up':
self.ind = (self.ind + 1) % self.slices
else:
self.ind = (self.ind - 1) % self.slices
self.update()
def update(self):
self.im.set_data(self.X[:, :, self.ind])
self.ax.set_ylabel('slice %s' % self.ind)
self.im.axes.figure.canvas.draw()
def load_obj(path):
if path[-4:] != '.pkl':
path += '.pkl'
if os.path.exists(path):
with open(path, 'rb') as f:
return pickle.load(f)
else:
output = {}
return output
def save_obj(path, obj): # Save almost anything.. dictionary, list, etc.
if path[-4:] != '.pkl':
path += '.pkl'
with open(path, 'wb') as f:
pickle.dump(obj, f, pickle.DEFAULT_PROTOCOL)
return None
if __name__ == '__main__':
xxx = 1