至于大小,下面的两个参数(后面)200, 100)默认为0,这可能意味着全检测到。
从C++示例的源代码中,我还可以猜测您不需要执行Canny边缘检测:#include
#include
#include
using namespace cv;
int main(int argc, char** argv)
{
Mat img, gray;
if( argc != 2 && !(img=imread(argv[1], 1)).data)
return -1;
cvtColor(img, gray, CV_BGR2GRAY);
// smooth it, otherwise a lot of false circles may be detected
GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
vector circles;
HoughCircles(gray, circles, CV_HOUGH_GRADIENT,
2, gray->rows/4, 200, 100 );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the circle center
circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
// draw the circle outline
circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
namedWindow( "circles", 1 );
imshow( "circles", img );
return 0;
}
我想你是想把这个C++代码转换成Python吧?for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
// draw the circle center
circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
// draw the circle outline
circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
据我所知,CvMat对象是可迭代的,就像列表一样:for circle in storage:
radius = circle[2]
center = (circle[0], circle[1])
cv.Circle(im, center, radius, (0, 0, 255), 3, 8, 0)
我没有任何测试图像,所以不要相信我的话。完整的代码可能是:import cv
def main():
im = cv.LoadImage('Proba.jpg')
gray = cv.CreateImage(cv.GetSize(im), 8, 1)
edges = cv.CreateImage(cv.GetSize(im), 8, 1)
cv.CvtColor(im, gray, cv.CV_BGR2GRAY)
#cv.Canny(gray, edges, 20, 55, 3)
storage = cv.CreateMat(im.width, 1, cv.CV_32FC3)
cv.HoughCircles(edges, storage, cv.CV_HOUGH_GRADIENT, 5, 25, 200, 10)
for i in xrange(storage.width - 1):
radius = storage[i, 2]
center = (storage[i, 0], storage[i, 1])
print (radius, center)
cv.Circle(im, center, radius, (0, 0, 255), 3, 8, 0)
cv.NamedWindow('Circles')
cv.ShowImage('Circles', im)
cv.WaitKey(0)
if __name__ == '__main__':
main()