今天在一个项目中,需要把从雷达中采集到的点拟合成一个圆。想起了大名鼎鼎的霍夫圆算法,于是在opencv中找到了霍夫圆的函数以及例程,测试了一下感觉效果并不理想。代码几乎是霍夫圆的历程直接拷贝的。
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
//==========================================载入原始图和Mat变量定义
Mat src = imread("1.png");
Mat cannyed,gray;
//==========================================进行边缘检测,转化为灰度图,模糊处理
cvtColor(src, gray, COLOR_BGR2GRAY);
Canny(gray, cannyed, 50, 200, 3);
GaussianBlur(cannyed, cannyed, Size(9, 9), 2, 2);
vector<Vec3f> circles;
//==========================================霍夫圆检测
HoughCircles(cannyed, circles, HOUGH_GRADIENT, 2, 100, 100, 100, 20, 200);
//==========================================绘制检测到的圆
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(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
// draw the circle outline
circle(src, center, radius, Scalar(0, 0, 255), 1, 8, 0);
}
//==========================================显示原图与检测到的图
namedWindow("circles", 1);
imshow("circles", src);
waitKey(0);
return 0;
}
算法结果如下,其中黑色圆是原始图像,红色圆是检测结果,绿色点是检测出的圆心。
明显看到检测的精度很低,不知道是不是参数上设置问题。