Mohamed Abduljawad
Mohamed Abduljawad

Reputation: 150

OpenCV HouchCircles Overshoot Circles Bigger than the Image

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?

Upvotes: 0

Views: 41

Answers (0)

Related Questions