opencv实战(零)——检测标记点

  在实际应用中,能够直接利用霍夫圆检测这些理想方法的应用场景是非常少的,更多的是利用拟合的办法去寻找圆形。
  大致思路如下,首先先选择要处理的ROI部分,记录下该图的左上点在原图的坐标,如果原图过大,要先进行等比例缩放;然后利用自适应阈值和Canny边缘提取进行处理,再进行闭运算与轮廓检测,计算点集面积,通过筛选面积阈值去除杂点,最后进行轮廓检测,拟合椭圆,效果如下:
1.导入原图:
opencv实战(零)——检测标记点_第1张图片
2.截取ROI
opencv实战(零)——检测标记点_第2张图片
3.进行自适应阈值化与Canny边缘提取
opencv实战(零)——检测标记点_第3张图片
4.进行闭运算,然后轮廓检测,然后计算点集面积,通过面积阈值去除杂点
opencv实战(零)——检测标记点_第4张图片

5.再次进行轮廓检测,拟合椭圆
opencv实战(零)——检测标记点_第5张图片
opencv实战(零)——检测标记点_第6张图片
代码如下:

#include 
#include 
#include 
#include 

#define scale 2//图像缩放因子
#define cannythreshold 80

typedef struct _ROIStruct
{
	cv::Point2d ROIPoint;
	cv::Mat ROIImage;
}ROIStruct;

ROIStruct getROI(cv::Mat src,bool flag = false)
{
	ROIStruct ROI_Struct;
	cv::Rect2d ROIrect = selectROI(src);
	ROI_Struct.ROIPoint = ROIrect.tl();//获取ROI区域左上角的点
	ROI_Struct.ROIImage = src(ROIrect);

	if (flag == true)
	{
		cv::imshow("ROI", ROI_Struct.ROIImage);
	}

	return ROI_Struct;
}
int main()
{
	cv::Mat srcImage = cv::imread("7.jpg");//读取待处理的图片
	cv::resize(srcImage, srcImage, cv::Size(srcImage.cols / scale, srcImage.rows / scale));//图像缩放,否则原来图像会在ROI时显示不下
	
	ROIStruct ROI = getROI(srcImage);//选择ROI区域
	
	cv::Mat DetectImage, thresholdImage;
	ROI.ROIImage.copyTo(DetectImage);
	cv::imshow("ROI", DetectImage);
	cv::cvtColor(DetectImage, thresholdImage, CV_RGB2GRAY);
	cv::adaptiveThreshold(thresholdImage, thresholdImage, 255, CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY,11,35);//自适应阈值
	cv::Canny(thresholdImage, thresholdImage, cannythreshold, cannythreshold * 3, 3);
	
	cv::imshow("thresholdImage", thresholdImage);
	
	std::vector<std::vector<cv::Point>> contours1;
	std::vector<cv::Vec4i> hierarchy1;
	cv::Mat element = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3, 3));
	cv::morphologyEx(thresholdImage, thresholdImage, cv::MORPH_CLOSE, element,cv::Point(-1,-1),2);
	cv::Mat findImage = cv::Mat::zeros(thresholdImage.size(), CV_8UC3);

	cv::findContours(thresholdImage, contours1, hierarchy1,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

	for (int i = 0; i <contours1.size();i++)
	{
		double area = cv::contourArea(contours1[i]);
		//std::cout << i << "点集区域面积:" << area << std::endl;
		
		if (area < 120)
		{
			continue;
		}

		else
		{
			drawContours(findImage, contours1, i, cv::Scalar(255, 255, 255), -1, 8, cv::Mat(), 0, cv::Point());
		}

	}

	cv::imshow("drawing", findImage);

	cv::Mat CircleImage(findImage.size(),CV_8UC1);
	findImage.copyTo(CircleImage);
	cv::cvtColor(CircleImage, CircleImage, CV_RGB2GRAY);
	
	std::vector<std::vector<cv::Point>> contours2;
	std::vector<cv::Vec4i> hierarchy2;

	cv::Mat resultImage(CircleImage.size(), CV_8UC3);
	cv::findContours(CircleImage, contours2, hierarchy2, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
	std::vector<cv::Point> points1, points2;
	
	cv::Mat compareImage;
	DetectImage.copyTo(compareImage);

	for (int j = 0; j <contours2.size();j++)
	{
		cv::RotatedRect box = cv::fitEllipse(contours2[j]);
		points1.push_back(box.center);
		ellipse(resultImage, box, cv::Scalar(0, 0, 255), 1, CV_AA);
		ellipse(compareImage, box, cv::Scalar(0, 0, 255), 1, CV_AA);
	}
	
	for (int i = 0; i < points1.size(); i++)
	{
		cv::Point ans;
		ans.x = ROI.ROIPoint.x + points1[i].x;
		ans.x = ans.x*scale;
		ans.y = ROI.ROIPoint.y + points1[i].y;
		ans.y = ans.y*scale;
		points2.push_back(ans);
	}

	std::cout << points1 << std::endl;
	std::cout << ROI.ROIPoint << std::endl;
	std::cout << points2 << std::endl;

	cv::imshow("resultImage", resultImage);
	cv::imshow("compareImage", compareImage);
	cv::waitKey(0);
	return 0;
}

你可能感兴趣的:(学习opencv,c++)