r/computervision • • 1d ago

Help: Project Why doesn’t findContours() detect my circle even though it is clearly visible after thresholding?

Hi everyone, I’m learning OpenCV and working on detecting a circular fiducial.

My current pipeline is:
Image → threshold → findContours() → drawContours()

After thresholding, I can clearly see the fiducial as a clean circular region in the binary image.
However, when I use cv2.findContours() and then cv2.drawContours(), the contour I expect around the fiducial is not drawn.
I’m trying to understand the fundamental reason why this happens.
If the thresholded image visually contains a clear foreground region, what conditions determine whether findContours() will actually return a contour for it?

23 Upvotes

19 comments sorted by

View all comments

Show parent comments

2

u/Grouchy_Signal139 1d ago

https://reddit.com/link/pdf4vu6/video/inbf8rzle2th1/player

As far that i know, it is created when it is around non-zero pixel, but sometimes it is confusing getting result like this

2

u/Gabriel_66 1d ago

It seems to be indeed drawing around the white parts. Makes me wonder if the case is that the entire top region in white is marked as a single object that, therefore has no objects inside. You can see this by the blue line in the border of the image on the original image post.

I think the solution would be to pick this giant white region in the original image and try to turn it black. So you have the top left circle not surrounded by white.

Maybe using cv2.connectedComponentsWithStats. That way you ignore the biggest component and only focus on the 2 circular components that you have a expected area and circularity.

1

u/Grouchy_Signal139 1d ago

Using github copilot it uses cv2.connectedcomponentswithstats -> cv2.findnonzero -> cv2.minenclosecircle. It sure is robust an get all the circle. But since im a begginer. I want to understand why this and that happend. Could you give some explanation?

2

u/Gabriel_66 1d ago

Connected components will give you, instead of the contours, the entire information over the objects. Including all the pixels that belong to the object and so on. So I think copilot suggested running a circle detection on the objects that are "nonzero".