/
manual_segmentation.py
231 lines (183 loc) · 7.18 KB
/
manual_segmentation.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from functools import reduce
import numpy as np
from ..draw import polygon
from .._shared.version_requirements import require
LEFT_CLICK = 1
RIGHT_CLICK = 3
def _mask_from_vertices(vertices, shape, label):
mask = np.zeros(shape, dtype=int)
pr = [y for x, y in vertices]
pc = [x for x, y in vertices]
rr, cc = polygon(pr, pc, shape)
mask[rr, cc] = label
return mask
@require("matplotlib", ">=3.3")
def _draw_polygon(ax, vertices, alpha=0.4):
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
polygon = Polygon(vertices, closed=True)
p = PatchCollection([polygon], match_original=True, alpha=alpha)
polygon_object = ax.add_collection(p)
plt.draw()
return polygon_object
@require("matplotlib", ">=3.3")
def manual_polygon_segmentation(image, alpha=0.4, return_all=False):
"""Return a label image based on polygon selections made with the mouse.
Parameters
----------
image : (M, N[, 3]) array
Grayscale or RGB image.
alpha : float, optional
Transparency value for polygons drawn over the image.
return_all : bool, optional
If True, an array containing each separate polygon drawn is returned.
(The polygons may overlap.) If False (default), latter polygons
"overwrite" earlier ones where they overlap.
Returns
-------
labels : array of int, shape ([Q, ]M, N)
The segmented regions. If mode is `'separate'`, the leading dimension
of the array corresponds to the number of regions that the user drew.
Notes
-----
Use left click to select the vertices of the polygon
and right click to confirm the selection once all vertices are selected.
Examples
--------
>>> from skimage import data, future, io
>>> camera = data.camera()
>>> mask = future.manual_polygon_segmentation(camera) # doctest: +SKIP
>>> io.imshow(mask) # doctest: +SKIP
>>> io.show() # doctest: +SKIP
"""
import matplotlib
import matplotlib.pyplot as plt
list_of_vertex_lists = []
polygons_drawn = []
temp_list = []
preview_polygon_drawn = []
if image.ndim not in (2, 3):
raise ValueError('Only 2D grayscale or RGB images are supported.')
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.imshow(image, cmap="gray")
ax.set_axis_off()
def _undo(*args, **kwargs):
if list_of_vertex_lists:
list_of_vertex_lists.pop()
# Remove last polygon from list of polygons...
last_poly = polygons_drawn.pop()
# ... then from the plot
last_poly.remove()
fig.canvas.draw_idle()
undo_pos = fig.add_axes([0.85, 0.05, 0.075, 0.075])
undo_button = matplotlib.widgets.Button(undo_pos, '\u27F2')
undo_button.on_clicked(_undo)
def _extend_polygon(event):
# Do not record click events outside axis or in undo button
if event.inaxes is None or event.inaxes is undo_pos:
return
# Do not record click events when toolbar is active
if ax.get_navigate_mode():
return
if event.button == LEFT_CLICK: # Select vertex
temp_list.append([event.xdata, event.ydata])
# Remove previously drawn preview polygon if any.
if preview_polygon_drawn:
poly = preview_polygon_drawn.pop()
poly.remove()
# Preview polygon with selected vertices.
polygon = _draw_polygon(ax, temp_list, alpha=(alpha / 1.4))
preview_polygon_drawn.append(polygon)
elif event.button == RIGHT_CLICK: # Confirm the selection
if not temp_list:
return
# Store the vertices of the polygon as shown in preview.
# Redraw polygon and store it in polygons_drawn so that
# `_undo` works correctly.
list_of_vertex_lists.append(temp_list[:])
polygon_object = _draw_polygon(ax, temp_list, alpha=alpha)
polygons_drawn.append(polygon_object)
# Empty the temporary variables.
preview_poly = preview_polygon_drawn.pop()
preview_poly.remove()
del temp_list[:]
plt.draw()
fig.canvas.mpl_connect('button_press_event', _extend_polygon)
plt.show(block=True)
labels = (
_mask_from_vertices(vertices, image.shape[:2], i)
for i, vertices in enumerate(list_of_vertex_lists, start=1)
)
if return_all:
return np.stack(labels)
else:
return reduce(np.maximum, labels, np.broadcast_to(0, image.shape[:2]))
@require("matplotlib", ">=3.3")
def manual_lasso_segmentation(image, alpha=0.4, return_all=False):
"""Return a label image based on freeform selections made with the mouse.
Parameters
----------
image : (M, N[, 3]) array
Grayscale or RGB image.
alpha : float, optional
Transparency value for polygons drawn over the image.
return_all : bool, optional
If True, an array containing each separate polygon drawn is returned.
(The polygons may overlap.) If False (default), latter polygons
"overwrite" earlier ones where they overlap.
Returns
-------
labels : array of int, shape ([Q, ]M, N)
The segmented regions. If mode is `'separate'`, the leading dimension
of the array corresponds to the number of regions that the user drew.
Notes
-----
Press and hold the left mouse button to draw around each object.
Examples
--------
>>> from skimage import data, future, io
>>> camera = data.camera()
>>> mask = future.manual_lasso_segmentation(camera) # doctest: +SKIP
>>> io.imshow(mask) # doctest: +SKIP
>>> io.show() # doctest: +SKIP
"""
import matplotlib
import matplotlib.pyplot as plt
list_of_vertex_lists = []
polygons_drawn = []
if image.ndim not in (2, 3):
raise ValueError('Only 2D grayscale or RGB images are supported.')
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.imshow(image, cmap="gray")
ax.set_axis_off()
def _undo(*args, **kwargs):
if list_of_vertex_lists:
list_of_vertex_lists.pop()
# Remove last polygon from list of polygons...
last_poly = polygons_drawn.pop()
# ... then from the plot
last_poly.remove()
fig.canvas.draw_idle()
undo_pos = fig.add_axes([0.85, 0.05, 0.075, 0.075])
undo_button = matplotlib.widgets.Button(undo_pos, '\u27F2')
undo_button.on_clicked(_undo)
def _on_lasso_selection(vertices):
if len(vertices) < 3:
return
list_of_vertex_lists.append(vertices)
polygon_object = _draw_polygon(ax, vertices, alpha=alpha)
polygons_drawn.append(polygon_object)
plt.draw()
matplotlib.widgets.LassoSelector(ax, _on_lasso_selection)
plt.show(block=True)
labels = (
_mask_from_vertices(vertices, image.shape[:2], i)
for i, vertices in enumerate(list_of_vertex_lists, start=1)
)
if return_all:
return np.stack(labels)
else:
return reduce(np.maximum, labels, np.broadcast_to(0, image.shape[:2]))