简体   繁体   中英

How to find the circle in the given images using opencv python (hough circles )?

I have an image,it contan the circle based stamp.I am trying to find that stamp based circle using hough cicles algorithm but I could not find that circle.

my code is:

image1=cv2.imread('1.jpg')
gray_image=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
th2 = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
        cv2.THRESH_BINARY,19,2)
output=image1.copy()
circles = cv2.HoughCircles(th2, cv2.HOUGH_GRADIENT, 1.3, 100)
if circles is not None:
     circles = np.round(circles[0, :]).astype("int")
     for (x, y, r) in circles:
    
         cv2.circle(output, (x, y), r, (0, 255, 0), 2)
         print(x,y,r)
plt.imshow(output)

input images在此处输入图像描述

output images:

在此处输入图像描述

I am getting like output image circle but i could not get the stamp circle.Please tell me how to solve this problem or how to set the parameter inside algorithms? Thanks..

You can try this:

image = cv2.imread('1.jpg')
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 1000)
# ensure at least some circles were found
if circles is not None:
    # 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 circles
    for (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
    plt.imshow(output)

the magic is here cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 1000) you can try to find best parameters ( accumulator value --> 1.2, minDist --> 1000 ) for your images

Your code is perfectly fine except that you need to add other parameters to HoughCircles. Also, you do not need to run thresholding on the image before running Hough transform since Canny edge will run on it in any case. You instead provide canny parameters - param1 and param2 in call to HoughCircles.

image1=cv2.imread('/path/to/your/image/doc_hough.jpeg')
gray_image=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

output=image1.copy()
circles = cv2.HoughCircles(gray_image, cv2.HOUGH_GRADIENT, 1.3, 100, param1=80, param2=140, minRadius=30, maxRadius=100)
if circles is not None:
     circles = np.round(circles[0, :]).astype("int")
     for (x, y, r) in circles:
    
         cv2.circle(output, (x, y), r, (0, 255, 0), 2)
         print(x,y,r)
plt.imshow(output)

在此处输入图像描述

https://scikit-image.org/docs/dev/auto_examples/edges/plot_circular_elliptical_hough_transform.html

Here is how to find the circle using skimage. Step 1: find the canny edges from the grayscale image. Step 2: look for circles using the min and max radii size. Step 3: draw the circle or circles 4. display the image

from skimage.transform import hough_circle,hough_circle_peaks
from skimage.draw import circle_perimeter
from skimage.feature import canny
from skimage import color

image1=plt.imread('embassy.jpg')
#slice a section fo the image
image_slice = img_as_ubyte(image1[960:1230, 70:870])
#find the canny edges
grayscale_image=rgb2gray(image_slice)
canny_edges = canny(grayscale_image, sigma=1.0)
plt.imshow(canny_edges,cmap='gray')
plt.show()

hough_radii = np.arange(65, 90, 2)
hough_res = hough_circle(canny_edges, hough_radii)

accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii,
                                       total_num_peaks=4)

fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10, 4))

t_image = color.gray2rgb(image_slice)

for center_y, center_x, radius in zip(cy, cx, radii):
     circy, circx = circle_perimeter(center_y, center_x, radius,
                                shape=t_image.shape)
     #set the circle color
     t_image[circy, circx] = (220, 20, 20)

     ax.imshow(t_image, cmap='gray')
     plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM