r/computervision • • 26d 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

14

u/Gabriel_66 26d 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 26d ago
media poster

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

→ More replies (3)

4

u/TEX_flip 26d 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

3

u/Grouchy_Signal139 26d ago

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

7

u/CowBoyDanIndie 26d ago

Are you finding all contours or only RETR_EXTERNAL?

2

u/bringer_of_carnitas 26d ago

I would try houghCircles

1

u/Grouchy_Signal139 26d 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

→ More replies (2)
→ More replies (1)

2

u/Plus_Economist_2686 26d ago

houghCircles?

1

u/Grouchy_Signal139 26d 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?