OpenCV之圆的检测识别

整个具体流程大概是获取图像-->图像二值化,灰度图(cvtColor)-->图像降噪(GaussianBlur)->轮廓识别(cvFindContours)-->形状判断

识别圆

在识别圆方面,OpenCV有内置的方法即霍夫圆变化:

HoughCircles(edges, circles, CV_HOUGH_GRADIENT, 1.5, 10, 200, 100, 0, 0);
// edges: 灰度图像
// circles:std::vector circles;数组,用来存储圆的坐标信息
// CV_HOUGH_GRADIENT:Hough 变换方式,目前只支持CV_HOUGH_GRADIENT
// 1.5:累加器图像的分辨率,1的时候是与获取到的图像相同,1.5就是1.5倍
// 10:圆与圆的最小距离,两个圆心距离如果在范围内则被认定为1个圆
// 200: 100-200两个参数选就够了
// 100: 默认100,数值越低识别圆越不精确(圆的数量识别变多可能有个弧线就被识别是圆)
// 最后两个参数分别是识别 圆的最小,最大的面积

源码

#include 
#include 
#include 
#include 
#include   
#include   

using namespace cv;

void SendMessageOne()
{
	//开起摄像头
	VideoCapture capture;
	capture.open(0);
	Mat edges;  //定义转化的灰度图
	if (!capture.isOpened())
		namedWindow("【效果图】", CV_WINDOW_NORMAL);

	while (1)
	{
		int Y = 0, J = 0;
		Mat frame;
		capture >> frame;
		cvtColor(frame, edges, CV_BGR2GRAY);
		//高斯滤波
		GaussianBlur(edges, edges, Size(7, 7), 2, 2);
		std::vector circles;//存储每个圆的位置信息
		//霍夫圆
		HoughCircles(edges, circles, CV_HOUGH_GRADIENT, 1.5, 10, 100, 100, 0, 100);
		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(frame, center, radius, Scalar(155, 50, 255), 3, 8, 0);
			int R = frame.at(cvRound(circles[i][1]), cvRound(circles[i][0]))[2];//R
			int G = frame.at(cvRound(circles[i][1]), cvRound(circles[i][0]))[1];//G
			int B = frame.at(cvRound(circles[i][1]), cvRound(circles[i][0]))[0];//B
			std::cout << "圆的半径是" << radius << std::endl;
			std::cout << "圆的X是" << circles[i][0] << "圆的Y是" << circles[i][1] << std::endl;
		}

		imshow("【效果图】", frame);
		waitKey(30);
	}
}

int main()
{
	std::thread *a = new std::thread(SendMessageOne);
	a->join();
	return 0;
}

 

你可能感兴趣的:(【功成不必在我】)