How to improve square/rectangle shape detection?

Hi all,

Currently, I am working in project to find square/rectangle shape in the image and only draw contours of square/rectangle objects.
I use to code below for object classification

peri= cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt, 0.02 * peri, True)

However, when I ran the code, I found that the code is not optimized for rectangle/square detection. Most objects are not rectangle/square have been removed, but some are still remaining although they are not rectangle/square

Is there any suggestion that I could totally remove irregular shape highlighted in red rectangle

Thanks

Hello @bao_pham

Just wandering around and found this interesting. Did you managed to solve this issue?

I would draw the resulting approximated countours to see why those are detected as squares or rectangles. Also I would check if the resulting polygon aspect ratio is coherent.

Thanks,
Bob @ www.proventusnova.com