关于OpenCV的函数HoughCircles()的详细介绍,大家可参见博文 https://blog.youkuaiyun.com/wenhao_ir/article/details/51783566
这两天博主发现利用OpenCV的函数HoughCircles()实现霍夫梯度法圆检测时参数dp的值对于最终结果影响是挺大的。
看下面这个代码:
这个代码用于检测医学CT图中的圆形。
代码中用到的图像下载链接:https://pan.baidu.com/s/1CdiZtiwYZIpyAUTnpaAMQQ?pwd=1w5y
//博主微信/QQ 2487872782
//有问题可以联系博主交流
//有图像处理开发需求也请联系博主
//图像处理技术交流QQ群 271891601
//OpenCV版本:3.0
//VS版本:2013
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat ctColorImg = imread("./CT-01.jpg");
//imshow("ctColorImg", ctColorImg);
Mat ctGrayImg;
cvtColor(ctColorImg, ctGrayImg, CV_BGR2GRAY);
//imshow("ctGrayImg", ctGrayImg);
GaussianBlur(ctGrayImg, ctGrayImg, Size(9, 9), 2, 2);//先对图像做高斯平滑处理
vector<Vec3f> h_circles; //用向量数组存储病灶区圆圈
HoughCircles(ctGrayImg, h_circles, CV_HOUGH_GRADIENT, 2, ctGrayImg.rows / 8, 200, 100);
for (size_t i = 0; i < h_circles.size(); i++)
{
Point center(cvRound(h_circles[i][0]), cvRound(h_circles[i][1]));
int h_radius = cvRound(h_circles[i][2]);
circle(ctColorImg, center, h_radius, Scalar(240, 0, 0), 3, 8, 0);
//以蓝色圆圈圈出CT相片上的病灶区
circle(ctColorImg, center, 3, Scalar(0, 0, 240), -1, 8, 0);
//以红色点标出病灶区的中心所在之处
}
imshow("ctColorImg", ctColorImg);
waitKey();
return(0);
}
此时函数HoughCircles()中的参数dp的值为2,运行结果如下:
可见检测到了很多圆,但如果我们把函数HoughCircles()中的参数dp的值为1,即把代码:
HoughCircles(ctGrayImg, h_circles, CV_HOUGH_GRADIENT, 2, ctGrayImg.rows / 8, 200, 100);
改为
HoughCircles(ctGrayImg, h_circles, CV_HOUGH_GRADIENT, 1, ctGrayImg.rows / 8, 200, 100);
此时运行结果如下:
可见,一个圆都没有检测到。
所以可以说利用OpenCV的函数HoughCircles()实现霍夫梯度法圆检测时参数dp的值对于最终结果影响是挺大的。
在博文https://blog.youkuaiyun.com/wenhao_ir/article/details/51783566中,参数dp的值为1,效果很好,博文https://blog.youkuaiyun.com/wenhao_ir/article/details/51783566中的图像尺寸如下:
而这篇博文中的图像尺寸如下:
所以我们可不可以说函数HoughCircles()的累计器图分辨率比较合适的值为宽300~400,高300~400之间呢?