2

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 enter image description here

output images:

enter image description here

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 ..

Amit Saini
  • 136
  • 2
  • 16

3 Answers3

2

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)

enter image description here

Knight Forked
  • 1,529
  • 12
  • 14
  • thank you , perfectly working on this image , only one more question what is the condition to find circle in the image because we are changing the parameter or values for different-2 images ? – Amit Saini May 31 '21 at 06:46
  • 1
    Unfortunately there's no single generalized condition that will find circles of all different sizes on all different kinds of images. Your chances of finding a good match depends largely on apriori knowledge on what you are looking for. But mostly you can come as close to a general code by tweaking parameters so that it finds circles in most of those cases/images. – Knight Forked May 31 '21 at 08:09
0

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

dimay
  • 2,768
  • 1
  • 13
  • 22
  • Thanks for posting answers . This is working for this images but when I read new images it do not working .can you give me a solution for another images – Amit Saini May 30 '21 at 19:17
  • or can you tell me any calculation are required to find the given circle ? – Amit Saini May 30 '21 at 19:18
  • @Saini I've added some description. I hope it will help. https://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/ – dimay May 30 '21 at 19:22
  • that means I will change the parameters for different -2 images ? how to find this parameters is right for that image? – Amit Saini May 30 '21 at 19:23
  • @Saini I don't know what images you have, you can try to change these parameters and look what result you are geting – dimay May 30 '21 at 19:27
  • I have edited the input image . Please check this image and please tell me only for this images.I will try for other images – Amit Saini May 30 '21 at 19:34
0

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()
Golden Lion
  • 3,840
  • 2
  • 26
  • 35