您好,登录后才能下订单哦!
本文小编为大家详细介绍“怎么使用Opencv检测多个圆形”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么使用Opencv检测多个圆形”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
主要是利用霍夫圆检测、面积筛选等完成多个圆形检测,具体代码及结果如下。
第一部分是头文件(common.h):
#pragma once #include<opencv2/opencv.hpp> #include<opencv2/highgui.hpp> #include<iostream> using namespace std; using namespace cv; extern Mat src; void imageBasicInformation(Mat& src);//图像基本信息 const Mat houghCirclePre(Mat& srcPre);//霍夫圆检测预处理 void houghCircle(Mat& srcPreHough);//霍夫圆检测 const Mat RectCirclePre(Mat& srcPre);//面积筛选拟合圆的预处理 void AreaCircles(Mat& AreaInput);//面积筛选拟合圆检测
第二部分是主函数:
#include"common.h" Mat src; int main() { src = imread("1.jpg",1); if (src.empty()) { cout << "图像不存在!" << endl; } else { namedWindow("原图", 1); imshow("原图", src); imageBasicInformation(src); Mat srcPreHough = houghCirclePre(src); houghCircle(srcPreHough); Mat RectCir = RectCirclePre(src); AreaCircles(RectCir); waitKey(0); destroyAllWindows(); } return 0; }
第三部分为霍夫圆检测函数(hough.cpp)
主要包括输出图像的基本信息函数:void imageBasicInformation(Mat& src)
霍夫圆检测预处理函数:const Mat houghCirclePre(Mat& srcPre)
霍夫圆检测函数:void houghCircle(Mat& srcPreHough)
#include"common.h" Mat graySrc, srcPre;//灰度图,霍夫检测预处理, Mat threshold_grayaSrc;//二值化图 Mat erode_threshold_graySrc, dilate_threshold_graySrc;//二值化后腐蚀,二值化后膨胀 void imageBasicInformation(Mat& src) { int cols = src.cols; int rows = src.rows; int channels = src.channels(); cout << "图像宽为:" << cols << endl; cout << "图像高为:" << rows << endl; cout << "图像通道数:" << channels << endl; } const Mat houghCirclePre(Mat& srcPre) { double houghCirclePreTime = static_cast<double>(getTickCount()); cvtColor(srcPre, graySrc, COLOR_BGR2GRAY); GaussianBlur(graySrc, graySrc, Size(3, 3), 2, 2);//滤波 threshold(graySrc, threshold_grayaSrc, 150, 255, 1);//二值化 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); dilate(threshold_grayaSrc, dilate_threshold_graySrc, element);//膨胀 erode(dilate_threshold_graySrc, erode_threshold_graySrc, element);//腐蚀 houghCirclePreTime = ((double)getTickCount() - houghCirclePreTime) / getTickFrequency(); cout << "霍夫圆预处理时间为:" << houghCirclePreTime << "秒" << endl; return erode_threshold_graySrc; } void houghCircle(Mat& srcPreHough) { cout << "进入霍夫圆检测" << endl; vector<Vec3f> circles; HoughCircles(srcPreHough, circles, HOUGH_GRADIENT, 1, 60, 1, 35, 0, 0); cout << "圆的个数" << circles.size() << endl; 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(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//画圆 } namedWindow("霍夫检测结果", 0); imshow("霍夫检测结果", src); imwrite("霍夫圆检测结果.jpg", src);//保存检测结果 }
第四部分为利用面积筛选拟合圆检测(AreaCircle.cpp)
主要包括预处理函数:const Mat RectCirclePre(Mat& srcPre)
面积筛选拟合圆检测函数:void AreaCircles(Mat& AreaInput)
#include"common.h" Mat graySrc, srcPre;//灰度图,霍夫检测预处理, Mat threshold_grayaSrc;//二值化图 Mat erode_threshold_graySrc, dilate_threshold_graySrc;//二值化后腐蚀,二值化后膨胀 void imageBasicInformation(Mat& src) { int cols = src.cols; int rows = src.rows; int channels = src.channels(); cout << "图像宽为:" << cols << endl; cout << "图像高为:" << rows << endl; cout << "图像通道数:" << channels << endl; } const Mat houghCirclePre(Mat& srcPre) { double houghCirclePreTime = static_cast<double>(getTickCount()); cvtColor(srcPre, graySrc, COLOR_BGR2GRAY); GaussianBlur(graySrc, graySrc, Size(3, 3), 2, 2);//滤波 threshold(graySrc, threshold_grayaSrc, 150, 255, 1);//二值化 Mat element = getStructuringElement(MORPH_RECT, Size(15, 15)); dilate(threshold_grayaSrc, dilate_threshold_graySrc, element);//膨胀 erode(dilate_threshold_graySrc, erode_threshold_graySrc, element);//腐蚀 houghCirclePreTime = ((double)getTickCount() - houghCirclePreTime) / getTickFrequency(); cout << "霍夫圆预处理时间为:" << houghCirclePreTime << "秒" << endl; return erode_threshold_graySrc; } void houghCircle(Mat& srcPreHough) { cout << "进入霍夫圆检测" << endl; vector<Vec3f> circles; HoughCircles(srcPreHough, circles, HOUGH_GRADIENT, 1, 60, 1, 35, 0, 0); cout << "圆的个数" << circles.size() << endl; 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(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//画圆 } namedWindow("霍夫检测结果", 0); imshow("霍夫检测结果", src); imwrite("霍夫圆检测结果.jpg", src);//保存检测结果 }
结果如下(自己画的两个圆):
原图:
以下为霍夫圆检测结果:
以下为面积筛选拟合圆结果:
读到这里,这篇“怎么使用Opencv检测多个圆形”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。