how to use hough circles in cv2 with python?

Question:

I have the following code and I want to detect the circle.

   img = cv2.imread("act_circle.png")
   gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
   circles = cv2.HoughCircles(gray,cv2.CV_HOUGH_GRADIENT)

it looks like it does not have the attribute and the error is the following

'module' object has no attribute 'CV_HOUGH_GRADIENT'

Does anybody know where this hidden parameters is?

Thanks

Asked By: Shan

||

Answers:

CV_HOUGH_GRADIENT belongs to the cv module, so you’ll need to import that:

import cv2.cv as cv

and change your function call to

circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT)

Now in current cv2 versions:

import cv2
cv2.HOUGH_GRADIENT
Answered By: jam

In my case, I am using opencv 3.0.0 and it worked the following way:

circles = cv2.HoughCircles(gray_im, cv2.HOUGH_GRADIENT, 2, 10, np.array([]), 20, 60, m/10)[0]  

i.e. instead of cv2.cv.CV_HOUGH_GRADIENT, I have used just cv2.HOUGH_GRADIENT.

Answered By: Saurav

if you use OpenCV 3, then use this code :

img = cv2.imread("act_circle.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT) # change here
Answered By: Kevin Patel