我有一个小的测试代码块,试图处理其中一个带球的简单照片:
#!/usr/local/bin/python import cv2 import numpy as np img = cv2.imread("b.jpg") gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) circles = cv2.HoughCircles(gray,cv2.CV_HOUGH_GRADIENT)
当我尝试运行此命令时,我得到:
AttributeError:“模块”对象没有属性“ HOUGH_GRADIENT”
我已经安装和重新安装了两天,试图找出问题所在。任何帮助或指针,将不胜感激!
添加此行:import cv2.cv as cv并更改circles
import cv2.cv as cv
circles
circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT)
即cv.CV_HOUGH_GRADIENT代替cv2.CV_HOUGH_GRADIENT
cv.CV_HOUGH_GRADIENT
cv2.CV_HOUGH_GRADIENT
这将解决AttributeError您遇到的问题,但仍然会出现类型错误,您必须提供dpand的minDist 参数(在pos 3和pos 4的参数),并可以据此给出。(这里我给出了1和10)
AttributeError
dp
minDist
#!/usr/local/bin/python import cv2 import cv2.cv as cv import numpy as np img = cv2.imread("test.jpg") gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT, 1, 10)
还请参见此处解决的类似问题:
http://answers.opencv.org/question/1497/errors-with- cv2houghcircles/