image= cv::imread("D:\\1.jpg",0);
cv::GaussianBlur(image,image,cv::Size(5,5),1.5);
std::vector<cv::Vec3f> circles;
cv::HoughCircles(image, circles, CV_HOUGH_GRADIENT,
2, // accumulator resolution (size of the image / 2)
50, // minimum distance between two circles
200, // Canny high threshold
100, // minimum number of votes
25, 100); // min and max radius
std::cout << "Circles: " << circles.size() << std::endl;
// Draw the circles
image= cv::imread("D:\\1.jpg");
std::vector<cv::Vec3f>::const_iterator itc= circles.begin();
while (itc!=circles.end()) {
cv::circle(image,
cv::Point((*itc)[0], (*itc)[1]), // circle centre
(*itc)[2], // circle radius
cv::Scalar(255), // color
2); // thickness
++itc;
}
我有个疑问为什么有的时候是圆却检测不出来
std::cout<<"("<<(*itc)[0]<<","<<(*itc)[1]<<")......"<<(*itc)[2]<<std::endl;
把圆心和半径打印出来,可是有个问题就是如何解决同心圆的问题。。。。。。。。
Canny(img1,img2,120,355);
cv::imshow("img2",img2);
倒是可以检测出来圆但是那只是圆的边缘,不是一种好方法
突然间发现这种方法检测同心圆有个很大的问题,就是同心圆的minmum distance between two circles is zero,whatever be seted that cannot be detected
imread的函数原型是:Mat imread( const string& filename, int flags=1 );
Mat是OpenCV里的一个数据结构,在这里我们定义一个Mat类型的变量img,用于保存读入的图像,在本文开始有写到,我们用imread函数来读取图像,第一个字段标识图像的文件名(包括扩展名),第二个字段用于指定读入图像的颜色和深度,它的取值可以有以下几种:
1) CV_LOAD_IMAGE_UNCHANGED (<0),以原始图像读取(包括alpha通道),
2) CV_LOAD_IMAGE_GRAYSCALE ( 0),以灰度图像读取
3) CV_LOAD_IMAGE_COLOR (>0),以RGB格式读取
为啥写这个是因为我们上面的程序要求的是以灰度图的形式就行读入的,所以有必要注意一下imread的参数