-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvis.py
133 lines (114 loc) · 4.93 KB
/
vis.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
__author__ = 'Piotr Stępień'
# Given im1 and im2 images stored as numpy arrays ...
# import pyelastix
# from scipy.io import loadmat, savemat
# import numpy as np
import matplotlib.pyplot as plt
# from skimage.filters.thresholding import threshold_otsu
# from skimage.morphology import remove_small_objects
# from skimage.measure import label, regionprops
# from scipy import ndimage as ndi
class IndexTracker:
def __init__(self, X, sliced_axis=2, axis_labels=['y', 'x', 'z'], fun=None, range=None):
self.fig, self.ax = plt.subplots(1, 1)
self.axis_labels = axis_labels
self.current_axis = int(sliced_axis)
self.current_label = axis_labels[self.current_axis]
self.X = X
if fun is None:
fun = lambda x: x
self.fun = fun
self.range = range
try:
self.current_axis_size = self.X.shape[self.current_axis]
except:
self.current_axis_size = self.X.shape()[self.current_axis]
# self.X_rot = self.X.copy()
# rows, cols, self.slices = X.shape
try:
self.ind = self.X.shape[self.current_axis] // 2
except:
self.ind = self.X.shape()[self.current_axis] // 2
self.y_mouse_prev = None
temp_x = self.X[:, :, self.ind]
temp_x = fun(temp_x)
self.im = self.ax.imshow(temp_x)
if range is not None:
self.im.set_clim(range[0], range[1])
# self.im = self.ax.imshow(self.X_rot[:, :, self.ind])
self.fig.colorbar(self.im, ax=self.ax)
self.update()
self.ax.set_title('Mouse drag up/down to select slice')
def onmousemove(self, event):
if event.x is not None and event.y is not None:
if str(event.button) == 'MouseButton.LEFT':
steps = int(round((event.y - self.y_mouse_prev) / 2))
# if steps > 0:
# print('up')
# if steps < 0:
# print('down')
self.ind = (self.ind + steps) % self.current_axis_size
self.update()
# if self.y_mouse_prev > event.y:
# # print('up')
# if self.ind < self.current_axis_size:
# self.ind = (self.ind + 1) % self.current_axis_size
# self.update()
# if self.y_mouse_prev < event.y:
# # print('down')
# if self.ind > 0:
# self.ind = (self.ind - 1) % self.current_axis_size
# self.update()
self.y_mouse_prev = event.y
else:
self.y_mouse_prev = None
def onrightmouseclick(self, event):
if str(event.button) == 'MouseButton.RIGHT':
self.current_axis = int(self.current_axis + 1)
self.current_axis = self.current_axis % 3
self.current_label = self.axis_labels[self.current_axis]
try:
self.current_axis_size = self.X.shape[self.current_axis]
except:
self.current_axis_size = self.X.shape()[self.current_axis]
self.ind = self.ind % self.current_axis_size
self.ax.clear()
if self.current_axis == 0:
X = self.fun(self.X[self.ind])
elif self.current_axis == 1:
X = self.fun(self.X[:, self.ind, :])
else:
X = self.fun(self.X[..., self.ind])
self.im = self.ax.imshow(X)
if range is not None:
self.im.set_clim(self.range[0], self.range[1])
# self.X_rot = self.X.copy()
# if self.current_axis == 0:
# self.X_rot = np.rot90(self.X_rot, axes=[0, 2])
# elif self.current_axis == 1:
# self.X_rot = np.rot90(self.X_rot, axes=[1, 2])
# self.X_rot = np.rot90(self.X_rot, axes=[0, 1], k=2)
# self.X_rot = np.rot90(self.X_rot, axes=[0, 2], k=2)
self.update()
# plt.colorbar()
def update(self):
if self.current_axis == 0:
X = self.X[self.ind]
elif self.current_axis == 1:
X = self.X[:, self.ind, :]
else:
X = self.X[..., self.ind]
X = self.fun(X)
self.im.set_data(X)
# self.im.set_data(self.X_rot[:, :, self.ind])
self.ax.set_title(self.current_label + ' = ' + str(self.ind) + ' / ' + str(self.current_axis_size))
self.im.axes.figure.canvas.draw()
def vis(vol, sliced_axis=2, axis_labels=['y', 'x', 'z'], fun=None, range=None):
# import matplotlib.style as mplstyle
# mplstyle.use('fast')
tracker = IndexTracker(vol, sliced_axis=sliced_axis, axis_labels=axis_labels, fun=fun, range=range)
tracker.fig.canvas.mpl_connect('motion_notify_event', tracker.onmousemove)
tracker.fig.canvas.mpl_connect('button_press_event', tracker.onrightmouseclick)
plt.show(block=True)
if __name__ == "__main__":
test = 1