From f04bb007b088778457603d0e891d01358dd63e17 Mon Sep 17 00:00:00 2001
From: Constantin Wenger <constantin.wenger@googlemail.com>
Date: Fri, 21 Jun 2019 11:47:55 +0000
Subject: [PATCH] try to detect outer contours caused by sleeves
---
opencv_dnn.py | 43 ++++++++++++++++++++++++++++++++++++++++---
1 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/opencv_dnn.py b/opencv_dnn.py
index e9b8d5a..bf01980 100644
--- a/opencv_dnn.py
+++ b/opencv_dnn.py
@@ -241,6 +241,7 @@
#print('no contours')
return []
img_cont = cv2.cvtColor(img_erode, cv2.COLOR_GRAY2BGR)
+ img_cont_base = img_cont.copy()
cnts2 = sorted(cnts, key=cv2.contourArea, reverse=True)
cnts2 = cnts2[:10]
for i in range(0, len(cnts2)):
@@ -262,9 +263,45 @@
size = cv2.contourArea(cnt)
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.04 * peri, True)
- if size >= size_thresh and len(approx) < 6:
- print('Size:', size)
- cnts_rect.append(approx)
+ if size >= size_thresh and len(approx) == 4:
+ # lets see if we got a contour very close in size as child
+ if i_child != -1:
+ img_ccont = img_cont_base.copy()
+ # lets collect all children
+ c_list = [cnts[i_child]]
+ h_info = hier[0][i_child]
+ while h_info[0] != -1:
+ cld = cnts[h_info[0]]
+ c_list.append(cld)
+ h_info = hier[0][h_info[0]]
+ # child with biggest area
+ c_list.sort(key=cv2.contourArea, reverse=True)
+ c_cnt = c_list[0] # the biggest child
+ cv2.drawContours(img_ccont, c_list[:1], -1, (0, 255, 0), 1)
+ cv2.imshow('CCont %d' % i_cnt, img_ccont)
+ c_size = cv2.contourArea(c_cnt)
+ c_approx = cv2.approxPolyDP(c_cnt, 0.04 * peri, True)
+ if len(c_approx) == 4 and (c_size/size) > 0.85:
+ rect = cv2.minAreaRect(c_cnt)
+ box = cv2.boxPoints(rect)
+ box = np.intp(box)
+ print(c_cnt)
+ print(box)
+
+ print('CSize:', c_size, '%:', c_size/size)
+ b2 = []
+ for x in box:
+ b2.append([x])
+ cnts_rect.append(np.array(b2))
+
+ else:
+ print('CF:', (c_size/size))
+ print('Size:', size)
+ cnts_rect.append(approx)
+ else:
+ print('CF:', (c_size/size))
+ print('Size:', size)
+ cnts_rect.append(approx)
else:
if i_child != -1:
stack.append((i_child, hier[0][i_child]))
--
Gitblit v1.10.0