White circles extraction from a little darker background

I'm trying to extract three white circles on the top left corner from the image below:

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:

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.

Topic opencv computer-vision python

Category Data Science


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.