Skip to content Skip to sidebar Skip to footer

Detect Overlapping Noisy Circles In Image

I try to recognize two areas in the following image. The area inside the inner and the area between the outer and inner - the border - circle with python openCV. I tried different

Solution 1:

General mistake: While using HoughCircles() , the parameters should be chosen appropriately. I see that you are only using first 4 parameters in your code. Ypu can check here to get a good idea about those parameters.

Experienced idea: While using HoughCircles , I noticed that if 2 centers of 2 circles are same or almost close to each other, HoughCircles cant detect them. Even if you assign min_dist parameter to a small value. In your case, the center of circles also same.

My suggestion: I will attach the appropriate parameters with the code for both circles. I couldnt find 2 circles with one parameter list because of the problem I explained above. My suggestion is that apply these two parameters double time for the same image and just get the circles and get the result.

For outer circle result and parameters included code:

Result:

enter image description here

# import the necessary packagesimport numpy as np
import argparse
import cv2
from PIL import Image

# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread('image.jpg')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

gray = cv2.medianBlur(gray,15)
rows = gray.shape[0]

# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT,1, rows / 8,
                               param1=100, param2=30,
                               minRadius=200, maxRadius=260)
# ensure at least some circles were foundif circles isnotNone:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    # loop over the (x, y) coordinates and radius of the circlesfor (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle# corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 4)
        cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
    # show the output image
    img = Image.fromarray(image)
    if img.height > 1500:
        imS = cv2.resize(np.hstack([image, output]), (round((img.width * 2) / 3), round(img.height / 3)))
    else:
        imS = np.hstack([image, output])
    # Resize image
    cv2.imshow("gray", gray)
    cv2.imshow("output", imS)
    cv2.waitKey(0)
else:
    print("No circle detected")

For inner circle the parameters:

circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT,1, rows / 8,
                                   param1=100, param2=30,
                                   minRadius=100, maxRadius=200)

Result:

enter image description here

Post a Comment for "Detect Overlapping Noisy Circles In Image"