forked from backtime92/CRAFT-Reimplementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watershed.py
234 lines (195 loc) · 7.89 KB
/
watershed.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
232
233
234
import cv2
import numpy as np
import math
def watershed1(image, viz=False):
boxes = []
if len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image
if viz:
cv2.imshow("gray", gray)
cv2.waitKey()
ret, binary = cv2.threshold(gray, 0.6 * np.max(gray), 255, cv2.THRESH_BINARY)
if viz:
cv2.imshow("binary", binary)
cv2.waitKey()
kernel = np.ones((3, 3), np.uint8)
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
mb = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
sure_bg = cv2.dilate(mb, kernel, iterations=3)
if viz:
cv2.imshow("sure_bg", sure_bg)
cv2.waitKey()
dist = cv2.distanceTransform(mb, cv2.DIST_L2, 5)
if viz:
cv2.imshow("dist", dist)
cv2.waitKey()
ret, sure_fg = cv2.threshold(dist, 0.2 * np.max(dist), 255, cv2.THRESH_BINARY)
surface_fg = np.uint8(sure_fg)
if viz:
cv2.imshow("surface_fg", surface_fg)
cv2.waitKey()
unknown = cv2.subtract(sure_bg, surface_fg)
ret, markers = cv2.connectedComponents(surface_fg)
markers = markers + 1
markers[unknown == 255] = 0
if viz:
color_markers = np.uint8(markers)
color_markers = cv2.applyColorMap(color_markers, cv2.COLORMAP_JET)
cv2.imshow("color_markers", color_markers)
cv2.waitKey()
markers = cv2.watershed(image, markers=markers)
image[markers == -1] = [0, 0, 255]
if viz:
cv2.imshow("image", image)
cv2.waitKey()
for i in range(2, np.max(markers) + 1):
np_contours = np.roll(np.array(np.where(markers == i)), 1, axis=0).transpose().reshape(-1, 2)
# print(np_contours.shape)
rectangle = cv2.minAreaRect(np_contours)
box = cv2.boxPoints(rectangle)
w, h = np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[1] - box[2])
box_ratio = max(w, h) / (min(w, h) + 1e-5)
if abs(1 - box_ratio) <= 0.1:
l, r = min(np_contours[:, 0]), max(np_contours[:, 0])
t, b = min(np_contours[:, 1]), max(np_contours[:, 1])
box = np.array([[l, t], [r, t], [r, b], [l, b]], dtype=np.float32)
# make clock-wise order
startidx = box.sum(axis=1).argmin()
box = np.roll(box, 4 - startidx, 0)
box = np.array(box)
boxes.append(box)
return np.array(boxes)
def getDetCharBoxes_core(textmap, text_threshold=0.7, low_text=0.4):
# prepare data
textmap = textmap.copy()
img_h, img_w = textmap.shape
""" labeling method """
ret, text_score = cv2.threshold(textmap, low_text, 1, 0)
nLabels, labels, stats, centroids = cv2.connectedComponentsWithStats(text_score.astype(np.uint8),
connectivity=4)
det = []
mapper = []
for k in range(1, nLabels):
# size filtering
size = stats[k, cv2.CC_STAT_AREA]
if size < 10: continue
# thresholding
if np.max(textmap[labels == k]) < text_threshold: continue
# make segmentation map
segmap = np.zeros(textmap.shape, dtype=np.uint8)
segmap[labels == k] = 255
# segmap[np.logical_and(link_score == 1, text_score == 0)] = 0 # remove link area
x, y = stats[k, cv2.CC_STAT_LEFT], stats[k, cv2.CC_STAT_TOP]
w, h = stats[k, cv2.CC_STAT_WIDTH], stats[k, cv2.CC_STAT_HEIGHT]
niter = int(math.sqrt(size * min(w, h) / (w * h)) * 2)
sx, ex, sy, ey = x - niter, x + w + niter + 1, y - niter, y + h + niter + 1
# boundary check
if sx < 0: sx = 0
if sy < 0: sy = 0
if ex >= img_w: ex = img_w
if ey >= img_h: ey = img_h
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1 + niter, 1 + niter))
segmap[sy:ey, sx:ex] = cv2.dilate(segmap[sy:ey, sx:ex], kernel)
# make box
np_contours = np.roll(np.array(np.where(segmap != 0)), 1, axis=0).transpose().reshape(-1, 2)
rectangle = cv2.minAreaRect(np_contours)
box = cv2.boxPoints(rectangle)
# align diamond-shape
w, h = np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[1] - box[2])
box_ratio = max(w, h) / (min(w, h) + 1e-5)
if abs(1 - box_ratio) <= 0.1:
l, r = min(np_contours[:, 0]), max(np_contours[:, 0])
t, b = min(np_contours[:, 1]), max(np_contours[:, 1])
box = np.array([[l, t], [r, t], [r, b], [l, b]], dtype=np.float32)
# make clock-wise order
startidx = box.sum(axis=1).argmin()
box = np.roll(box, 4 - startidx, 0)
box = np.array(box)
det.append(box)
mapper.append(k)
return det, labels, mapper
def watershed(image, viz=False):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray) / 255.0
boxes, _, _ = getDetCharBoxes_core(gray)
return np.array(boxes)
def area_ratio(area, box,mask, max_number):
#print(box.transpose(1,0).shape)
cv2.fillPoly(mask, [np.int32(box)], (255))
box_area = np.sum(np.greater(mask, 0))
ratio = box_area/area
#ratio_max = (max_number)/2
return ratio, box_area
def watershed2(image, viz=False):
boxes = []
if len(image.shape) == 3:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
else:
gray = image
if viz:
cv2.imshow("gray", gray)
cv2.waitKey()
ret, binary = cv2.threshold(gray, 0.2 * np.max(gray), 255, cv2.THRESH_BINARY)
if viz:
cv2.imshow("binary", binary)
cv2.waitKey()
kernel = np.ones((3, 3), np.uint8)
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
mb = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel, iterations=2)
sure_bg = cv2.dilate(mb, kernel, iterations=3)
if viz:
cv2.imshow("sure_bg", sure_bg)
cv2.waitKey()
dist = cv2.distanceTransform(sure_bg, cv2.DIST_L2, 3)
if viz:
cv2.imshow("dist", dist)
cv2.waitKey()
#print(dist.max())
ret, sure_fg = cv2.threshold(gray, 0.4 * gray.max(), 255, cv2.THRESH_BINARY)
surface_fg = np.uint8(sure_fg)
if viz:
cv2.imshow("surface_fg", surface_fg)
cv2.waitKey()
unknown = cv2.subtract(sure_bg, surface_fg)
ret, markers = cv2.connectedComponents(surface_fg)
markers = markers + 1
markers[unknown == 255] = 0
if viz:
color_markers = np.uint8(markers)
color_markers = cv2.applyColorMap(color_markers, cv2.COLORMAP_JET)
cv2.imshow("color_markers", color_markers)
cv2.waitKey()
markers = cv2.watershed(image, markers=markers)
image[markers == -1] = [0, 0, 255]
if viz:
cv2.imshow("image", image)
cv2.waitKey()
area = gray.shape[0] * gray.shape[1]
max_number = np.max(markers)
for i in range(0, np.max(markers) + 1):
mask = np.zeros((gray.shape[0], gray.shape[1]), dtype=np.uint8)
np_contours = np.roll(np.array(np.where(markers == i)), 1, axis=0).transpose().reshape(-1, 2)
# print(np_contours.shape)
rectangle = cv2.minAreaRect(np_contours)
box = cv2.boxPoints(rectangle)
w, h = np.linalg.norm(box[0] - box[1]), np.linalg.norm(box[1] - box[2])
box_ratio = max(w, h) / (min(w, h) + 1e-5)
if abs(1 - box_ratio) <= 0.1:
l, r = min(np_contours[:, 0]), max(np_contours[:, 0])
t, b = min(np_contours[:, 1]), max(np_contours[:, 1])
box = np.array([[l, t], [r, t], [r, b], [l, b]], dtype=np.float32)
# make clock-wise order
startidx = box.sum(axis=1).argmin()
box = np.roll(box, 4 - startidx, 0)
box = np.array(box)
boxes.append(box)
# ratio, box_area = area_ratio(area, box, mask, max_number)
# if ratio < 0.3 and box_area > 4:
# boxes.append(box)
return np.array(boxes)
if __name__ == '__main__':
image = cv2.imread('images/standard.jpg', cv2.IMREAD_COLOR)
boxes = watershed(image, False)
print(boxes)