本文主要是介绍Hough圆检测(可Trackbar调节),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <string>using namespace std;
using namespace cv;
Mat g_image1; //原图
Mat g_grayimg; //灰度图
int min_dist=8;//检测到圆心之间的最小距离
int param_1 = 100; //Canny边缘函数的高阈值
int param_2 = 10; //圆心检测阈值.
void ReadOriginalImg(string _filename)
{// Read input imageg_image1 = imread(_filename);if (!g_image1.data){/*printf("读取%s失败!", _filename);*/cout << "读取" + _filename + "失败!" << endl;system("pause");return;}// Display the color image/*cv::resize(g_image1, g_image1, cv::Size(), 0.7, 1);*/cv::namedWindow("Original Image1");cv::imshow("Original Image1", g_image1);
}///*画出所有的圆*/
//void MyHoughCircles()
//{
//
//}
static void Min_dist(int, void*)
{Mat temp = g_image1.clone();/*利用Hough变换找圆*///定义圆的存储容器vector<Vec3f> circles;HoughCircles(g_grayimg, circles, CV_HOUGH_GRADIENT, 1, g_image1.rows / min_dist, param_1, param_2, 0);//画圆for (size_t i = 0; i < circles.size(); i++){//圆心坐标Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));//半径int radius = circles[i][2];//画圆心circle(temp, center, 3, Scalar(0), -1);//画圆circle(temp, center, radius, Scalar(0, 0, 255), 2);}imshow("HoughCircles", temp);
}int main()
{/*读原图*/ReadOriginalImg("mycircle.jpg");/*对原图灰度化*/cvtColor(g_image1, g_grayimg, CV_BGR2GRAY);/*对灰度图进行高斯滤波,避免错误识别圆*/GaussianBlur(g_grayimg, g_grayimg, Size(9, 9), 2, 2);//imshow("Gray Image", g_grayimg);namedWindow("HoughCircles", CV_WINDOW_AUTOSIZE);createTrackbar("Min_dist", "HoughCircles", &min_dist, 100, Min_dist);createTrackbar("Canny高阈值", "HoughCircles", ¶m_1, 255, Min_dist);createTrackbar("圆心检测阈值", "HoughCircles", ¶m_2, 100, Min_dist);Min_dist(min_dist, 0);waitKey(0);
}
这篇关于Hough圆检测(可Trackbar调节)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!