TransWikia.com

White circles extraction from a little darker background

Data Science Asked on September 27, 2021

I’m trying to extract three white circles on the top left corner from the image below:
enter image description here

I’ve tried to use:

clone = img.copy()

# Threshold grayscaled image to get binary image
ret,gray_threshed = cv2.threshold(img,90,100,cv2.THRESH_TRUNC)
cv2.imshow('gray', gray_threshed)

# Find edges
edge_detected_image = cv2.Canny(gray_threshed, 350, 100)

# Find contours
contours, _= cv2.findContours(edge_detected_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

contour_list = []
for contour in contours:
    contour_list.append(contour)

# Draw contours
cv2.drawContours(clone, contour_list, -1, (255,0,0), 2)

#Resize image
clone = cv2.resize(clone, (800, 800)) 

cv2.imshow('Objects Detected',clone)

The result was:
enter image description here

I’ve also tried sth like:

img = cv2.blur(img,(5, 5))    

img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,method=cv2.HOUGH_GRADIENT,dp=1,minDist=2,param1=50,param2=60,minRadius=0,maxRadius=300)

circles = np.uint64(np.around(circles))
for i in circles[0,:]:
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

But it showed the error due to the fact that it hadn’t found any circles.

Any hints how to solve my kind of problem are welcome. Thanks in advance.

One Answer

Follow the following steps:

Step 1: use an averaging filter to minimise the noise present in the image

Step 2: threshold the image such that pixels with RGB values within [0, 0, 0] and [1, 1, 1] are set to 0 and rest as 1. This step will lead you to generate a binary image.

Step 3: use Hough transform in OpenCV to detect circles. Tune the parameters of the Hough transform to get your desired output.

Check this out for more details of Hough transform for circles: https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

Answered by user1825567 on September 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP