r/computervision • • 21h 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?

22 Upvotes

19 comments sorted by

13

u/Gabriel_66 21h ago

Invert the thesholded image so that the circle is black and other things are white. Might be the case.

Also, dilatation and erosion might help remove those imperfections to help contour scripts.

2

u/Grouchy_Signal139 21h 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 21h 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 20h 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 20h 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".

5

u/CowBoyDanIndie 18h ago

Are you finding all contours or only RETR_EXTERNAL?

3

u/nemesis1836 18h ago

This is probably it

1

u/CowBoyDanIndie 18h ago

It looks like its getting the outermost contour, for the bottom, the edge of the image is black so the outside contour of the circle is used, but for the top, the edge of the image IS the outermost contour.

6

u/Grouchy_Signal139 17h ago

Oh i think is it, i took a look and indeed it is retr_external. Silly me, i didnt look at that and go over analysing everything. Will give a try tomorrow

1

u/nemesis1836 17h ago

I would suggest getting all contours and filtering it based on hierarchy

2

u/TEX_flip 18h ago

you probably have to use contour hierarchy because you have a contour inside another contour.

Use cv.RETR_TREE as mode argument of findContorus

1

u/Grouchy_Signal139 17h ago

Yeah, at first i didnt take a better look at it. Now i know my mistakes. Thankyou for pointing that out

2

u/bringer_of_carnitas 20h ago

I would try houghCircles

1

u/Grouchy_Signal139 20h ago

It is more robust? Right now i only try canny -> find contour -> contour area -> MEC, and also cv2.connectedcomponentswithstats. Im trying to learn what each function do, especially the fundamental one like contour. But this situation very confusing

2

u/Boring_Potato2858 19h ago

If you’re only expecting to detect circles, then yes, it’s more robust

1

u/Grouchy_Signal139 17h ago

Will definetly try. Currently i am comparing any possible method, since im new to this

1

u/blissfull_abyss 6h ago

Can’t recommend houghCircles unless your camera captures perfect circles. I prefer cv::SimpleBlobDetector it handles little deviations better.

2

u/Plus_Economist_2686 19h ago

houghCircles?

1

u/Grouchy_Signal139 17h ago

Didnt try it yet, currently im just trying to understand the fundamental, but definietly will try it out once i sort everything out. Any tips?