Detecting first inner black circle and making a contour: openCV and Python

I want to detect the first inner black circle from the white solid using OpenCV and Python.

The circle is shown with arrow

I have tried thresholding but it is not working properly.

imporrt cv2

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.

Upvotes: 0

Views: 83

Answers (0)

Related Questions