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检测多个圆形(霍夫圆检测,轮廓面积筛选)的文章就介绍到这了,更多相关Opencv检测圆形内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章