I want to detect circles in image as below:
Image may be NSFW.
Clik here to view.
So, I tried with cv2.findContours
and cv2.Houghcircles
in python, but there were some problems.Both methods were applied after image processing (Canny, threshold, etc.)
First, if I use cv2.findCountours
, it cannot detect outer circles since they were overlapped and detect as a single contour.Here's my python code.
contours, hier = cv2.findContours(input, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)for contour in contours: c= (random.randint(0,255),random.randint(0,255),random.randint(0,255)) if area>10000 and area<100000: # Filtering the target size (x, y), radius = cv2.minEnclosingCircle(contour) center= (int(x), int(y)) radius= int(radius) cv2.circle(color, center, radius, c,5)
Image may be NSFW.
Clik here to view.
Second, if I use cv2.Houghcircles
(Split the radius range to detect concentric circles), the results were better, but still the results were poor.
for maxR in range(100,200,10): # minimum 100 to maximum 200 by step 10 circles = cv2.HoughCircles(input, cv2.HOUGH_GRADIENT, 1, 100, param1=100,param2=22,minRadius=minR,maxRadius=maxR) minR+=10 # Check to see if there is any detection if circles is not None: # If there are some detections, convert radius and x,y(center) coordinates to integer circles = np.round(circles[0, :]).astype("int") for (x, y, r) in circles: c= (random.randint(0,255),random.randint(0,255),random.randint(0,255)) cv2.circle(view, (x, y), r, c, 3) # draw circumference cv2.rectangle(view, (x - 2, y - 2), (x + 2, y + 2), c, 5) # draw center
Image may be NSFW.
Clik here to view.
Is there any better methods to detect concentric circles in image and measure center coordinates and radius of them?
I compared the encloed area and minimum enclosing circle area.And I got circular contours.
contours, hier = cv2.findContours(aaa, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)for contour in contours: c= (random.randint(0,255),random.randint(0,255),random.randint(0,255)) area = cv2.contourArea(contour) (cx, cy), radius = cv2.minEnclosingCircle(contour) err=abs((area-np.pi*radius*radius)/(np.pi*radius*radius)) if err< 0.1: cv2.drawContours(aaa, [contour], -1, (255,255,255) , thickness=cv2.FILLED) cv2.drawContours(color, [contour], -1, c , 5)
Image may be NSFW.
Clik here to view.
Then, I filled the area with white color.Image may be NSFW.
Clik here to view.
Now, the problem is very simple.All I need to do is separate the overlapping circles into their own circles. (I can find many references)
Thanks you all!