我试图检测交通信号灯内的圆圈,我只能检测到2个圆圈中的1个圆圈,而我所获得的圆圈的大小似乎太大了
Input Image: https://i.imgur.com/VkNDt2B.png
Output image: https://i.imgur.com/BBq5tE0.png
int main()
{
Mat src, gray;
src = imread("C:\/test_image2.png", 1);
resize(src, src, Size(640, 480));
cvtColor(src, gray, CV_BGR2GRAY);
// Reduce the noise so we avoid false circle detection
GaussianBlur(gray, gray, Size(9, 9), 2, 2);
vector<Vec3f> circles;
// Apply the Hough Transform to find the circles
HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 1, 60, 200, 20, 0, 35);
// Draw the circles detected
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]);
circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);// circle center
circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);// circle outline
cout << "center : " << center << "\nradius : " << radius << endl;
}
// Show your results
namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
imshow("Hough Circle Transform Demo", src);
waitKey(0);
return 0;
}
2 回答
这是一个非常巨大的图像,首先尝试裁剪到红绿灯部分(以获得开始的东西),然后通过尝试min_distance和param_1的不同组合,param_2参数尝试检测大多数圆圈(甚至是错误的圆圈) . 找出哪些值得到最多圈子和哪个组合得到最少(或没有)圈子然后微调参数以检测较小的圆圈,最后找到完美的组合
如果事先知道您正在寻找的圆圈的大小,HoughCircles效果最佳 . 我建议你为min_radius和max_radius参数提供更好的值 . 无论如何,您需要使用param1和param2参数 . 如果圆圈不是正圆,您可以尝试使用dp参数降低图像分辨率(f.ex.,dp = 2,图像缩小到其分辨率的一半) . 基本上:使用param1和param2,直到检测到您的圆圈,无论是否检测到其他圆圈 . 使用此结果找出圆圈的半径,然后修复最小和最大半径以移除大多数您不想要的圆圈,最后再使用param1和param2再次播放,直到只留下您的圆圈 .