0

I'm doing a simple circle detection with HoughCircles, for a clock, no matter how I change the parameters, the circle detection never works correctly, I tried to extra blurring and no use. The input image is this: enter image description here

The output circle is this : enter image description here

The code used is this:

import cv2
import numpy as np
from google.colab.patches import cv2_imshow

#edge detection
image = cv2.imread('cl.jpg')

image = cv2.resize(image, (int(image.shape[0]/3), int(image.shape[1]/3)))

image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (3, 3), 0)
canny = cv2.Canny(image, 30, 150)

#circle detection
img = cv2.medianBlur(image,7)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,10,
                            param1=30,param2=50,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
  max_circle = max(circles[0,:], key=lambda x:x[2])



  i = max_circle
  #outer circle
  cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
  #center of the circle
  cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
print(max_circle)

cv2_imshow(cimg)

What could be the problem? what is the best workaround other than infinite trial and error?

  • Your bottom image looks like an ellipse, not a circle. That might be why center and size aren't correctly found. – Micka Aug 27 '23 at 20:14
  • Can you try to remove the cv2.resize or make sure that the order of width and height is correctly used? Maybe they are switched and your image aspect ratio is changed (if input was not a square size)? – Micka Aug 27 '23 at 20:16
  • @Micka i thought of this possibility, and removed the resizing but still similar behavior https://imgur.com/a/FrwV3ST – Mohamed Abduljawad Aug 28 '23 at 10:39
  • can you try to use `image` (without blurring) instead of `canny` for houghCircle? See https://stackoverflow.com/a/20706100/2393191 , hiughCircle function uses canny itself, so currently canny edge detection is applied on a canny edge image before accumulating hough circles. – Micka Aug 28 '23 at 11:29
  • houghtransform always needs optimal parameters to get good results. You can easily make a circle detection based on contour detection. [Here](https://github.com/yunus-temurlenk/Opencv-Circle-Detection-Without-Houghcircle) is an approach i applied before and it is working as expected. – Yunus Temurlenk Aug 28 '23 at 18:28

0 Answers0