forked from alkasm/magicwand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicwand.py
198 lines (167 loc) · 6.7 KB
/
magicwand.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
import cv2
import numpy as np
from collections import namedtuple
from itertools import cycle
import uuid # for unique filenames
Point = namedtuple('Point', 'x, y')
class SelectionWindow:
_displays = cycle(['selection', 'mask', 'applied mask'])
def __init__(self, name, image, connectivity=4):
# general params
self.name = name
self._image = image
self._h, self._w = image.shape[:2]
if len(image.shape) == 3:
self._channels = 3
else:
self._channels = 1
self._selection = image.copy()
self._mask = 255*np.ones((self._h, self._w), dtype=np.uint8)
self._applied_mask = image.copy()
self._curr_display = next(self._displays)
# parameters for floodfill
self.connectivity = connectivity
self._tolerance = (20,)*3
self._seed_point = Point(0, 0)
self._flood_mask = np.zeros((self._h+2, self._w+2), dtype=np.uint8)
def _onchange(self, pos):
self._tolerance = (pos,)*3
self._magicwand()
def _onclick(self, event, x, y, flags, param):
if flags & cv2.EVENT_FLAG_LBUTTON:
self._seed_point = Point(x, y)
self._magicwand()
def _magicwand(self):
self._flood_mask[:] = 0
flags = self.connectivity | 255 << 8 # bit shift
flags |= cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY
flood_image = self._image.copy()
cv2.floodFill(flood_image, self._flood_mask, self._seed_point, 0,
self._tolerance, self._tolerance, flags)
self._mask = self._flood_mask[1:-1, 1:-1].copy()
self._update_window()
def _drawselection(self):
# find contours around mask
self._selection = self._image.copy()
self._contours = cv2.findContours(self._mask, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(self._selection, self._contours, -1,
color=(255,)*3, thickness=-1) # highlight contours
self._selection = cv2.addWeighted(
self._image, 0.75, self._selection, 0.25, 0) # outline contours
cv2.drawContours(self._selection, self._contours, -1,
color=(255,)*3, thickness=1)
def _flip_displays(self):
self._curr_display = next(self._displays)
self._update_window()
if self.verbose:
print('Displaying %s' % self._curr_display)
def _print_stats(self):
print('Mean Color: ', self.mean,
'\nStdDev Color:', self.stddev,
'\nMin Color: ', self.min,
'\nMax Color: ', self.max,
'\nSeed Point: ', self.seedpt)
def _save(self):
if self._curr_display == 'selection':
filename = 'selection_' + uuid.uuid1().hex + '.png'
cv2.imwrite(filename, self._selection)
elif self._curr_display == 'mask':
filename = 'mask_' + uuid.uuid1().hex + '.png'
cv2.imwrite(filename, self._mask)
elif self._curr_display == 'applied mask':
filename = 'applied_mask_' + uuid.uuid1().hex + '.png'
cv2.imwrite(filename, self._applied_mask)
if self.verbose:
print('Saved image as', filename)
def _close(self):
if self.verbose:
print('Closing window')
print('\n--------------------------------------')
self._print_stats()
print('--------------------------------------\n')
cv2.destroyWindow(self.name)
def _update_window(self):
if self._curr_display == 'selection':
self._drawselection()
cv2.imshow(self.name, self._selection)
elif self._curr_display == 'mask':
cv2.imshow(self.name, self._mask)
elif self._curr_display == 'applied mask':
self._applied_mask = cv2.bitwise_and(
self._image, self._image, mask=self._mask)
cv2.imshow(self.name, self._applied_mask)
def show(self, verbose=False):
# create window, event callbacks
cv2.namedWindow(self.name)
cv2.setMouseCallback(self.name, self._onclick)
cv2.createTrackbar('Tolerance', self.name,
self._tolerance[0], 255, self._onchange)
self.verbose = verbose
if verbose:
print('Click anywhere to select a region of similar colors.')
print('Move the slider to include a wider range of colors.\n')
print(('Press [m] to switch between displaying the selection, '
'mask, or applied mask'))
print('Press [p] to print color statistics of current selection')
print('Press [s] to save the currently displayed image')
print('Press [q] or [esc] to close the window')
print('------------------------------------------------------------\n')
# display the image and wait for a keypress or trackbar change
cv2.imshow(self.name, self._image)
while(True):
k = cv2.waitKey() & 0xFF
if k == ord('q') or k == 27: # 27 is [esc]
self._close()
break
elif k == ord('m'):
self._flip_displays()
elif k == ord('p'):
self._print_stats()
elif k == ord('s'):
self._save()
@property
def mask(self):
return self._mask
@property
def applied_mask(self):
self._applied_mask = cv2.bitwise_and(
self._image, self._image, mask=self._mask)
return self._applied_mask
@property
def selection(self):
self._drawselection()
return self._selection
@property
def contours(self):
self._drawselection()
return self._contours
@property
def seedpt(self):
return self._seed_point
@property
def mean(self):
mean = cv2.meanStdDev(self._image, self._mask)[0]
if self._channels == 1:
return mean[0, 0]
return mean[:, 0]
@property
def stddev(self):
stddev = cv2.meanStdDev(self._image, self._mask)[1]
if self._channels == 1:
return stddev[0, 0]
return stddev[:, 0]
@property
def min(self):
if self._channels == 1:
return cv2.minMaxLoc(self._image, self._mask)[0]
min_val = [cv2.minMaxLoc(self._image[:, :, i], self._mask)[0]
for i in range(3)]
return np.array(min_val, dtype=np.uint8)
@property
def max(self):
if self._channels == 1:
return cv2.minMaxLoc(self._image, self._mask)[1]
max_val = [cv2.minMaxLoc(self._image[:, :, i], self._mask)[1]
for i in range(3)]
return np.array(max_val, dtype=np.uint8)