Opencv检测多个圆形(霍夫圆检测,轮廓面积筛选)
主要是利用霍夫圆检测、面积筛选等完成多个圆形检测,具体代码及结果如下。
第一部分是头文件(common.h):
#pragma once
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
extern Mat class="lazy" data-src;
void imageBasicInformation(Mat& class="lazy" data-src);//图像基本信息
const Mat houghCirclePre(Mat& class="lazy" data-srcPre);//霍夫圆检测预处理
void houghCircle(Mat& class="lazy" data-srcPreHough);//霍夫圆检测
const Mat RectCirclePre(Mat& class="lazy" data-srcPre);//面积筛选拟合圆的预处理
void AreaCircles(Mat& AreaInput);//面积筛选拟合圆检测
第二部分是主函数:
#include"common.h"
Mat class="lazy" data-src;
int main()
{
class="lazy" data-src = imread("1.jpg",1);
if (class="lazy" data-src.empty())
{
cout << "图像不存在!" << endl;
}
else
{
namedWindow("原图", 1);
imshow("原图", class="lazy" data-src);
imageBasicInformation(class="lazy" data-src);
Mat class="lazy" data-srcPreHough = houghCirclePre(class="lazy" data-src);
houghCircle(class="lazy" data-srcPreHough);
Mat RectCir = RectCirclePre(class="lazy" data-src);
AreaCircles(RectCir);
waitKey(0);
destroyAllWindows();
}
return 0;
}
第三部分为霍夫圆检测函数(hough.cpp)
主要包括输出图像的基本信息函数:void imageBasicInformation(Mat& class="lazy" data-src)
霍夫圆检测预处理函数:const Mat houghCirclePre(Mat& class="lazy" data-srcPre)
霍夫圆检测函数:void houghCircle(Mat& class="lazy" data-srcPreHough)
#include"common.h"
Mat grayclass="lazy" data-src, class="lazy" data-srcPre;//灰度图,霍夫检测预处理,
Mat threshold_grayaclass="lazy" data-src;//二值化图
Mat erode_threshold_grayclass="lazy" data-src, dilate_threshold_grayclass="lazy" data-src;//二值化后腐蚀,二值化后膨胀
void imageBasicInformation(Mat& class="lazy" data-src)
{
int cols = class="lazy" data-src.cols;
int rows = class="lazy" data-src.rows;
int channels = class="lazy" data-src.channels();
cout << "图像宽为:" << cols << endl;
cout << "图像高为:" << rows << endl;
cout << "图像通道数:" << channels << endl;
}
const Mat houghCirclePre(Mat& class="lazy" data-srcPre)
{
double houghCirclePreTime = static_cast<double>(getTickCount());
cvtColor(class="lazy" data-srcPre, grayclass="lazy" data-src, COLOR_BGR2GRAY);
GaussianBlur(grayclass="lazy" data-src, grayclass="lazy" data-src, Size(3, 3), 2, 2);//滤波
threshold(grayclass="lazy" data-src, threshold_grayaclass="lazy" data-src, 150, 255, 1);//二值化
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
dilate(threshold_grayaclass="lazy" data-src, dilate_threshold_grayclass="lazy" data-src, element);//膨胀
erode(dilate_threshold_grayclass="lazy" data-src, erode_threshold_grayclass="lazy" data-src, element);//腐蚀
houghCirclePreTime = ((double)getTickCount() - houghCirclePreTime) / getTickFrequency();
cout << "霍夫圆预处理时间为:" << houghCirclePreTime << "秒" << endl;
return erode_threshold_grayclass="lazy" data-src;
}
void houghCircle(Mat& class="lazy" data-srcPreHough)
{
cout << "进入霍夫圆检测" << endl;
vector<Vec3f> circles;
HoughCircles(class="lazy" data-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(class="lazy" data-src, center, 3, Scalar(0, 255, 0), -1, 8, 0);//画圆心
circle(class="lazy" data-src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//画圆
}
namedWindow("霍夫检测结果", 0);
imshow("霍夫检测结果", class="lazy" data-src);
imwrite("霍夫圆检测结果.jpg", class="lazy" data-src);//保存检测结果
}
第四部分为利用面积筛选拟合圆检测(AreaCircle.cpp)
主要包括预处理函数:const Mat RectCirclePre(Mat& class="lazy" data-srcPre)
面积筛选拟合圆检测函数:void AreaCircles(Mat& AreaInput)
#include"common.h"
Mat grayclass="lazy" data-src, class="lazy" data-srcPre;//灰度图,霍夫检测预处理,
Mat threshold_grayaclass="lazy" data-src;//二值化图
Mat erode_threshold_grayclass="lazy" data-src, dilate_threshold_grayclass="lazy" data-src;//二值化后腐蚀,二值化后膨胀
void imageBasicInformation(Mat& class="lazy" data-src)
{
int cols = class="lazy" data-src.cols;
int rows = class="lazy" data-src.rows;
int channels = class="lazy" data-src.channels();
cout << "图像宽为:" << cols << endl;
cout << "图像高为:" << rows << endl;
cout << "图像通道数:" << channels << endl;
}
const Mat houghCirclePre(Mat& class="lazy" data-srcPre)
{
double houghCirclePreTime = static_cast<double>(getTickCount());
cvtColor(class="lazy" data-srcPre, grayclass="lazy" data-src, COLOR_BGR2GRAY);
GaussianBlur(grayclass="lazy" data-src, grayclass="lazy" data-src, Size(3, 3), 2, 2);//滤波
threshold(grayclass="lazy" data-src, threshold_grayaclass="lazy" data-src, 150, 255, 1);//二值化
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));
dilate(threshold_grayaclass="lazy" data-src, dilate_threshold_grayclass="lazy" data-src, element);//膨胀
erode(dilate_threshold_grayclass="lazy" data-src, erode_threshold_grayclass="lazy" data-src, element);//腐蚀
houghCirclePreTime = ((double)getTickCount() - houghCirclePreTime) / getTickFrequency();
cout << "霍夫圆预处理时间为:" << houghCirclePreTime << "秒" << endl;
return erode_threshold_grayclass="lazy" data-src;
}
void houghCircle(Mat& class="lazy" data-srcPreHough)
{
cout << "进入霍夫圆检测" << endl;
vector<Vec3f> circles;
HoughCircles(class="lazy" data-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(class="lazy" data-src, center, 3, Scalar(0, 255, 0), -1, 8, 0);//画圆心
circle(class="lazy" data-src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//画圆
}
namedWindow("霍夫检测结果", 0);
imshow("霍夫检测结果", class="lazy" data-src);
imwrite("霍夫圆检测结果.jpg", class="lazy" data-src);//保存检测结果
}
结果如下(自己画的两个圆):
原图:
以下为霍夫圆检测结果:
以下为面积筛选拟合圆结果:
到此这篇关于Opencv检测多个圆形(霍夫圆检测,轮廓面积筛选)的文章就介绍到这了,更多相关Opencv检测圆形内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341