Ishaan Taylor Ishaan Taylor - 7 months ago 163
Python Question

AttributeError: 'module' object has no attribute 'HOUGH_GRADIENT', cv.HOUGH_GRADIENT not fixing the issue

Getting an attribute error when trying to run the following code:

import cv2
import cv2.cv as cv
import numpy as np


def main():
img = cv2.imread('images/g1.jpg',0);

print(img)

img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()


I've tried to look up a solution to this problem and I substituted the
cv2
with
cv
in

circles = cv2.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)


however, I am still getting the error.

Answer

The problem is that the attribute is

cv.CV_HOUGH_GRADIENT

So you need to do

circles = cv2.HoughCircles(img, cv.CV_HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0)