Following code is trying to extracting bounding rectangles from contours and merge rectangles which is contained in other rectangles
for i in xrange(len(find_contours)): cv2.drawContours(z2, find_contours, i, (100, 0, 0)) rect = cv2.boundingRect(find_contours[i]) cv2.rectangle(z1, (rect[0], rect[1]), (rect[0]+rect[2], rect[1]+rect[3]), (200,255,80), 1) cv2.imshow('z1a',z1) ret,z1=cv2.threshold(z1,50,255,cv2.THRESH_BINARY) z1=np.uint8(z1) find_thresh_img, find_contours, find_h = cv2.findContours(z1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
In above found contours, one inner contour is contained within an outer contour, but is not merged with outer contour. the inner one’s border is 2 pixel away from outer one’s border.
I checked the code multiple times, and not found something wrong.
Then I tried changed the drawing rectangle border thickness to 2, and it works..
cv2.rectangle(z1, (rect[0], rect[1]), (rect[0]+rect[2], rect[1]+rect[3]), (200,255,80), 2)
Checking it in PyCharm SciView Data View I found their border is connected. So I’m not sure why it’s working is caused by increased border width or connected shapes. (But in the past cv2.findContours with RETR_EXTERNAL will work for inner contours, so it’s likely caused by thin border)
The post OpenCV findContours with RETR_EXTERNAL not working appeared first on Redino blog.