Closer with contours
Browse files- interpretter_notes.py +10 -1
- understand.py +73 -10
interpretter_notes.py
CHANGED
@@ -130,4 +130,13 @@ array([[1907, 887],
|
|
130 |
"""
|
131 |
>>> cv.boundingRect(c[0])
|
132 |
(399, 340, 5, 3)
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
"""
|
131 |
>>> cv.boundingRect(c[0])
|
132 |
(399, 340, 5, 3)
|
133 |
+
|
134 |
+
>>> get_coordinates_for_bb_simple(results["segmentation"], 1)
|
135 |
+
((399, 300), (538, 392))
|
136 |
+
>>> make_new_bounding_box(cv.boundingRect(c[0]), cv.boundingRect(c[1]))
|
137 |
+
(399, 300, 140, 93)
|
138 |
+
>>> cv.boundingRect(c[0])
|
139 |
+
(399, 340, 5, 3)
|
140 |
+
>>> cv.boundingRect(c[1])
|
141 |
+
(409, 300, 130, 93)
|
142 |
+
"""
|
understand.py
CHANGED
@@ -103,9 +103,9 @@ def get_coordinates_for_bb_simple(map_to_use, label_id):
|
|
103 |
else:
|
104 |
mask = (map_to_use.numpy() == label_id)
|
105 |
|
106 |
-
|
107 |
-
x_max, x_min = max(
|
108 |
-
y_max, y_min = max(
|
109 |
return (x_min, y_min), (x_max, y_max)
|
110 |
|
111 |
def make_simple_box(left_top, right_bottom, map_size):
|
@@ -123,7 +123,7 @@ def make_simple_box(left_top, right_bottom, map_size):
|
|
123 |
plt.show()
|
124 |
|
125 |
|
126 |
-
def
|
127 |
"""
|
128 |
map_to_use: You have to pass in `results["segmentation"]`
|
129 |
"""
|
@@ -137,14 +137,15 @@ def test(map_to_use, label_id):
|
|
137 |
left_x, top_y = lt
|
138 |
right_x, bottom_y = rb
|
139 |
|
140 |
-
mask[left_x:right_x
|
141 |
-
mask[left_x:right_x
|
142 |
-
mask[
|
143 |
-
mask[
|
144 |
|
145 |
visual_mask = (mask* 255).astype(np.uint8)
|
146 |
visual_mask = Image.fromarray(visual_mask)
|
147 |
-
plt.imshow(
|
|
|
148 |
plt.show()
|
149 |
|
150 |
def contour_map(map_to_use, label_id):
|
@@ -166,4 +167,66 @@ def contour_map(map_to_use, label_id):
|
|
166 |
|
167 |
# Idea for determining if close
|
168 |
# https://dsp.stackexchange.com/questions/2564/opencv-c-connect-nearby-contours-based-on-distance-between-them
|
169 |
-
# Bing Search: cv determine if 2 contours belong together
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
else:
|
104 |
mask = (map_to_use.numpy() == label_id)
|
105 |
|
106 |
+
y_vals, x_vals = np.where(mask==True)
|
107 |
+
x_max, x_min = max(x_vals), min(x_vals)
|
108 |
+
y_max, y_min = max(y_vals), min(y_vals)
|
109 |
return (x_min, y_min), (x_max, y_max)
|
110 |
|
111 |
def make_simple_box(left_top, right_bottom, map_size):
|
|
|
123 |
plt.show()
|
124 |
|
125 |
|
126 |
+
def map_bounding_box_draw(map_to_use, label_id, img_obj=TEST_IMAGE):
|
127 |
"""
|
128 |
map_to_use: You have to pass in `results["segmentation"]`
|
129 |
"""
|
|
|
137 |
left_x, top_y = lt
|
138 |
right_x, bottom_y = rb
|
139 |
|
140 |
+
mask[top_y, left_x:right_x] = .5
|
141 |
+
mask[bottom_y, left_x:right_x] = .5
|
142 |
+
mask[ top_y:bottom_y, left_x] = .5
|
143 |
+
mask[ top_y:bottom_y, right_x] = .5
|
144 |
|
145 |
visual_mask = (mask* 255).astype(np.uint8)
|
146 |
visual_mask = Image.fromarray(visual_mask)
|
147 |
+
plt.imshow(img_obj)
|
148 |
+
plt.imshow(visual_mask, alpha=0.25)
|
149 |
plt.show()
|
150 |
|
151 |
def contour_map(map_to_use, label_id):
|
|
|
167 |
|
168 |
# Idea for determining if close
|
169 |
# https://dsp.stackexchange.com/questions/2564/opencv-c-connect-nearby-contours-based-on-distance-between-them
|
170 |
+
# Bing Search: cv determine if 2 contours belong together
|
171 |
+
|
172 |
+
def find_if_close(contour1, contour2, c_dist=50):
|
173 |
+
"""
|
174 |
+
Source: https://dsp.stackexchange.com/questions/2564/opencv-c-connect-nearby-contours-based-on-distance-between-them
|
175 |
+
|
176 |
+
"""
|
177 |
+
row1, row2 = contour1.shape[0], contour2.shape[0]
|
178 |
+
for i in range(row1):
|
179 |
+
for j in range(row2):
|
180 |
+
dist = np.linalg.norm(contour1[i]-contour2[j])
|
181 |
+
if abs(dist) < c_dist:
|
182 |
+
return True
|
183 |
+
elif i == (row1-1) and j == (row2-1):
|
184 |
+
return False
|
185 |
+
|
186 |
+
|
187 |
+
def make_new_bounding_box(bb1, bb2):
|
188 |
+
x1, y1, w1, h1 = bb1
|
189 |
+
x2, y2, w2, h2 = bb2
|
190 |
+
new_x = min(x1, x2)
|
191 |
+
new_y = min(y1, y2)
|
192 |
+
new_w = abs(max(x1+w1, x2+w2) - new_x)
|
193 |
+
new_h = abs(max(y1+h1, y2+h2) - new_y)
|
194 |
+
|
195 |
+
return (new_x, new_y, new_w, new_h)
|
196 |
+
|
197 |
+
def map_bounding_box_draw(map_to_use, label_id, img_obj=TEST_IMAGE, v="cv"):
|
198 |
+
"""
|
199 |
+
map_to_use: You have to pass in `results["segmentation"]`
|
200 |
+
v: version of bounding box
|
201 |
+
cv, coord
|
202 |
+
"""
|
203 |
+
if torch.cuda.is_available():
|
204 |
+
mask = (map_to_use.cpu().numpy() == label_id)
|
205 |
+
else:
|
206 |
+
mask = (map_to_use.numpy() == label_id)
|
207 |
+
|
208 |
+
if v == "cv":
|
209 |
+
c, v = contour_map(map_to_use, label_id)
|
210 |
+
x, y, w, h = make_new_bounding_box(cv.boundingRect(c[0]), cv.boundingRect(c[1]))
|
211 |
+
lt = (x, y)
|
212 |
+
rb = (x + w, y + h)
|
213 |
+
left_x, top_y = lt
|
214 |
+
right_x, bottom_y = rb
|
215 |
+
elif v == "coord":
|
216 |
+
lt, rb = get_coordinates_for_bb_simple(map_to_use, label_id)
|
217 |
+
left_x, top_y = lt
|
218 |
+
right_x, bottom_y = rb
|
219 |
+
else:
|
220 |
+
print(f"Not available `v` command {v}")
|
221 |
+
return
|
222 |
+
|
223 |
+
mask[top_y, left_x:right_x] = .5
|
224 |
+
mask[bottom_y, left_x:right_x] = .5
|
225 |
+
mask[ top_y:bottom_y, left_x] = .5
|
226 |
+
mask[ top_y:bottom_y, right_x] = .5
|
227 |
+
|
228 |
+
visual_mask = (mask* 255).astype(np.uint8)
|
229 |
+
visual_mask = Image.fromarray(visual_mask)
|
230 |
+
plt.imshow(img_obj)
|
231 |
+
plt.imshow(visual_mask, alpha=0.25)
|
232 |
+
plt.show(block=False)
|