将HoughCircles与Python OpenCV结合使用时出现错误,提示缺少模块 [英] I get a error when using HoughCircles with Python OpenCV that a module is missing
问题描述
我有一个小的测试代码块,试图处理其中一个带球的简单照片:
I have a small test code block trying to process a simple photo with a ball in it:
#!/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)
当我尝试运行此命令时,我得到:
When I try to run this I get:
AttributeError:模块"对象没有属性"HOUGH_GRADIENT"
AttributeError: 'module' object has no attribute 'HOUGH_GRADIENT'
我已经安装和重新安装了两天,试图找出问题所在. 任何帮助或指示,将不胜感激!
I've been installing and reinstalling for two days trying to figure out whats wrong. Any help or pointers would be appreciated!
推荐答案
添加以下行:import cv2.cv as cv
并更改circles
circles = cv2.HoughCircles(gray,cv.CV_HOUGH_GRADIENT)
即cv.CV_HOUGH_GRADIENT
代替cv2.CV_HOUGH_GRADIENT
这将解决您得到的AttributeError
,但仍然会出现类型错误,您必须为dp
和minDist
提供参数
(位置3和位置4的参数),您可以据此给出.(这里我给出了1和10)
This will solve the AttributeError
that you were getting but still you'll get a type error, you'll have to provide arguments for dp
and minDist
(arguments at pos 3 and pos 4) and that you can give accordingly.(Here I've given 1 and 10)
#!/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)
还请参见此处解决的类似问题:
also please see a similar problem solved here:
http://answers.opencv.org/question/1497/errors-with-cv2houghcircles/
这篇关于将HoughCircles与Python OpenCV结合使用时出现错误,提示缺少模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!