-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroipoly.py
More file actions
156 lines (131 loc) · 6.08 KB
/
roipoly.py
File metadata and controls
156 lines (131 loc) · 6.08 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'''Draw polygon regions of interest (ROIs) in matplotlib images,
similar to Matlab's roipoly function.
See the file example.py for an application.
Created by Joerg Doepfert 2014 based on code posted by Daniel
Kornhauser.
'''
import numpy as np
import sys
import matplotlib.pyplot as plt
import matplotlib.path as mplPath
class roipoly:
def __init__(self, fig=[], ax=[], roicolor='b'):
if fig == []:
fig = plt.gcf()
if ax == []:
ax = plt.gca()
self.previous_point = []
self.allxpoints = []
self.allypoints = []
self.start_point = []
self.end_point = []
self.line = None
self.roicolor = roicolor
self.fig = fig
self.ax = ax
#self.fig.canvas.draw()
self.__ID1 = self.fig.canvas.mpl_connect(
'motion_notify_event', self.__motion_notify_callback)
self.__ID2 = self.fig.canvas.mpl_connect(
'button_press_event', self.__button_press_callback)
if sys.flags.interactive:
plt.show(block=False)
else:
plt.show()
def getMask(self, currentImage):
ny, nx = np.shape(currentImage)
poly_verts = [(self.allxpoints[0], self.allypoints[0])]
for i in range(len(self.allxpoints)-1, -1, -1):
poly_verts.append((self.allxpoints[i], self.allypoints[i]))
# Create vertex coordinates for each grid cell...
# (<0,0> is at the top left of the grid in this system)
x, y = np.meshgrid(np.arange(nx), np.arange(ny))
x, y = x.flatten(), y.flatten()
points = np.vstack((x,y)).T
ROIpath = mplPath.Path(poly_verts)
grid = ROIpath.contains_points(points).reshape((ny,nx))
return grid
def displayROI(self,**linekwargs):
l = plt.Line2D(self.allxpoints +
[self.allxpoints[0]],
self.allypoints +
[self.allypoints[0]],
color=self.roicolor, **linekwargs)
ax = plt.gca()
ax.add_line(l)
plt.draw()
def displayMean(self,currentImage, **textkwargs):
mask = self.getMask(currentImage)
meanval = np.mean(np.extract(mask, currentImage))
stdval = np.std(np.extract(mask, currentImage))
string = "%.3f +- %.3f" % (meanval, stdval)
plt.text(self.allxpoints[0], self.allypoints[0],
string, color=self.roicolor,
bbox=dict(facecolor='w', alpha=0.6), **textkwargs)
def __motion_notify_callback(self, event):
if event.inaxes:
ax = event.inaxes
x, y = event.xdata, event.ydata
if (event.button == None or event.button == 1) and self.line != None: # Move line around
self.line.set_data([self.previous_point[0], x],
[self.previous_point[1], y])
self.fig.canvas.draw()
def __button_press_callback(self, event):
if event.inaxes:
x, y = event.xdata, event.ydata
ax = event.inaxes
if event.button == 1 and event.dblclick == False: # If you press the left button, single click
if self.line == None: # if there is no line, create a line
self.line = plt.Line2D([x, x],
[y, y],
marker='o',
color=self.roicolor)
self.start_point = [x,y]
self.previous_point = self.start_point
self.allxpoints=[x]
self.allypoints=[y]
ax.add_line(self.line)
self.fig.canvas.draw()
# add a segment
else: # if there is a line, create a segment
self.line = plt.Line2D([self.previous_point[0], x],
[self.previous_point[1], y],
marker = 'o',color=self.roicolor)
self.previous_point = [x,y]
self.allxpoints.append(x)
self.allypoints.append(y)
event.inaxes.add_line(self.line)
self.fig.canvas.draw()
elif ((event.button == 1 and event.dblclick==True) or
(event.button == 3 and event.dblclick==False)) and self.line != None: # close the loop and disconnect
self.fig.canvas.mpl_disconnect(self.__ID1) #joerg
self.fig.canvas.mpl_disconnect(self.__ID2) #joerg
self.line.set_data([self.previous_point[0],
self.start_point[0]],
[self.previous_point[1],
self.start_point[1]])
ax.add_line(self.line)
self.fig.canvas.draw()
self.line = None
if sys.flags.interactive:
pass
else:
#figure has to be closed so that code can continue
plt.close(self.fig)
def roi(myroi, x, y):
vertices = []
for ind, val in enumerate(myroi.allxpoints):
cc = [val, myroi.allypoints[ind]]
vertices.append(cc)
codes = [mplPath.Path.MOVETO] #codes=[Path.MOVETO,
for ind, val in enumerate(range(len(myroi.allxpoints)-2)): #Path.LINETO,
codes.append(mplPath.Path.LINETO) #Path.LINETO,
codes.append(mplPath.Path.CLOSEPOLY) #Path.CLOSEPOLY,
#]
path = mplPath.Path(vertices, codes)
d = []
for ind, val in enumerate(y):
f = [x[ind], val]
d.append(f)
mask = path.contains_points(d)
return mask