# Detecting first inner black circle and making a contour

**URL:** <https://forum.opencv.org/t/detecting-first-inner-black-circle-and-making-a-contour/16687>\
**Category:** Python\
**Tags:** findcontours, contours\
**Created:** [March 4, 2024, 4:49pm UTC](https://forum.opencv.org/t/detecting-first-inner-black-circle-and-making-a-contour/16687 "2024-03-04T16:49:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeeshanbabar112](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zeeshanbabar112/32/9854_2.png) [@zeeshanbabar112](https://forum.opencv.org/u/zeeshanbabar112)\
**Post date:** [March 4, 2024, 4:49pm UTC](https://forum.opencv.org/t/detecting-first-inner-black-circle-and-making-a-contour/16687/1 "2024-03-04T16:49:21Z")

</div>

![image231](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/7/79bd2feefb15acf69f97c304ed7b7c7809face82.jpeg)

Dear All,

I am trying to determine the first inner black circle appearing after the solid white. I have tried many ways but have not been able to find a suitable threshold value for this. My code looks like this

```auto
guass_kernal = 5
eroded_kernal = (3, 3)
canny_low_limit = 12
canny_up_limit = 20
eroded_iteration = 3

def eroded_image(self, canny_image):
        dilated = cv2.dilate(canny_image, (self.eroded_kernal), iterations=self.eroded_iteration)
        eroded = cv2.erode(dilated, self.eroded_kernal, iterations=self.eroded_iteration)

        thresh = cv2.adaptiveThreshold(eroded, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 3, 2)
        return thresh

image = cv2.imread('image231.jpg', cv2.IMREAD_COLOR)
RGB_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray_image = cv2.cvtColor(RGB_image, cv2.COLOR_RGB2GRAY) #gray image

blur_image = cv2.GaussianBlur(gray_image, (guass_kernal, guass_kernal), 5)
canny_image = cv2.Canny(blur_image, canny_low_limit, canny_up_limit, None, 3)
masked_image = eroded_image(canny_image)

contours, heirarchy = cv2.findContours(masked_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (255, 0, 255), 5)

```

Please help me refine the code and detect the inner circle properly.

Regards

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 6, 2024, 10:01pm UTC](https://forum.opencv.org/t/detecting-first-inner-black-circle-and-making-a-contour/16687/2 "2024-03-06T22:01:38Z")

</div>

[crosspost](https://meta.stackoverflow.com/a/266159):

> <https://stackoverflow.com/questions/78102915/detecting-first-inner-black-circle-and-making-a-contour-opencv-and-python>

---

<div class="post-metadata">

**Author:** ![zeeshanbabar112](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/zeeshanbabar112/32/9854_2.png) [@zeeshanbabar112](https://forum.opencv.org/u/zeeshanbabar112)\
**Post date:** [March 7, 2024, 9:41am UTC](https://forum.opencv.org/t/detecting-first-inner-black-circle-and-making-a-contour/16687/3 "2024-03-07T09:41:35Z")

</div>

i am not getting any answer here. So I have to cross post the question.
