Opencv4轮廓分析与检测

关键词:轮廓检测,分析,霍夫圆检测,外接矩形,外接旋转矩形,中心点,多变形填充,目标对象检测和分割

输入:

轮廓检测和分析:

包括圆检测,位置,中心点,外接矩形,外切矩形,轮廓面积,周长,旋转角度等。

轮廓背景分离和提取:

方法2,

目标对象检测和分割:

方法二,

参考代码,

void Test22()
{
    //0:铅笔;1:打火机;2:上面带阴影的圆;3:右边的大圆;4:左下的小圆
	vector<Point> contoursorder[5];
	Mat src, srcback, srcback2, src_gray0, src_gray, binImg;
	//1.读入并检测
	src = imread("D:/images/stuff.jpg", 1);
	srcback = imread("D:/images/stuff.jpg");
	srcback2 = srcback.clone();
	string str = "Hello, 2020!";
	putText(srcback2, str, Point(100,200), FONT_HERSHEY_SIMPLEX, 2.0, Scalar(0, 0, 255), 2, 16);

	imshow("src", srcback2);
	waitKey(0);
	Mat RoiSrcImg(src.rows, src.cols, CV_8UC3);
	RoiSrcImg.setTo(255);//颜色都设置为白色
	//imshow("RoiSrcImg", RoiSrcImg);
	//waitKey(0);

	Mat RoiSrcImg0(src.rows, src.cols, CV_8UC3);
	RoiSrcImg0.setTo(255);//颜色都设置为白色

	//【声明&初始化两个参数】declare and initialize both parameters that are subjects to change
	int cannyThreshold = 21;
	int accumulatorThreshold = 34;

	cannyThreshold = std::max(cannyThreshold, 1);
	accumulatorThreshold = std::max(accumulatorThreshold, 1);


	Mat src1 = CalculateImageGradient(src, false);
	//imshow("src1", src1);
	//waitKey(0);
	//查找轮廓
	灰度化
	//src_gray = src1;
	int iii = src1.channels();
	cvtColor(src1, src_gray, COLOR_RGB2GRAY);

	//2.转为灰度,并模糊除噪- -预处理
	cvtColor(src, src_gray0, COLOR_BGR2GRAY);
	GaussianBlur(src_gray0, src_gray0, Size(9, 9), 2, 2);

	//二值化	
	threshold(src_gray, binImg, 0, 255, THRESH_BINARY | THRESH_OTSU);
	//imshow("bin", binImg);
	//waitKey(0);
	///
	vector<vector<Point>> contours;
	//vector<Rect> boundRect(contours.size());

	//注意第5个参数为CV_RETR_EXTERNAL,只检索外框   //找轮廓
	findContours(binImg, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
	//
	cout << contours.size() << endl;
	int iSeq = -1;
	for (int i = 0; i < contours.size(); i++)
	{
		double area = contourArea(contours[i]);
		double len = arcLength(contours[i], true);

		if (area > 1000)// continue;
		{
			if (area > 6300 && area < 6400)
			{
				contoursorder[1] = contours[i];
				Rect  recttt = boundingRect(Mat(contours[i]));

				rectangle(src, recttt, (0, 0, 255), 2, 8, 0);
				//cv::drawContours(RoiSrcImg, contours, i, Scalar(0), FILLED);
				rectangle(RoiSrcImg, recttt,Scalar(0), -1);
				//imshow("RoiSrcImg1", RoiSrcImg);
				//waitKey(0);
				cv::RotatedRect &&rotate_rect = cv::minAreaRect(contours[i]);
				circle(src, rotate_rect.center, 3, Scalar(0, 0, 255), -1, 8, 0);
				string strarea = "area:" + doubleConverToString(area);

				putText(src, strarea, rotate_rect.center, FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 1, 8);
			}
			else if (area > 5900 && area < 6000)
			{
				contoursorder[0] = contours[i];
				//cv::drawContours(RoiSrcImg, contours, i, Scalar(0), FILLED);
				DrawCircumscribedRectangle2(src, RoiSrcImg,contours[i]);
			}
			else
			{

				//参数:src【gray】, src【显示】,canny阈值,累加阈值
				HoughDetection2(src_gray0, src, RoiSrcImg,cannyThreshold, accumulatorThreshold);
			}
		}
		else
		{
			drawContours(binImg, contours, i, Scalar(0), FILLED);
		}
	}
	bitwise_not(RoiSrcImg, RoiSrcImg);
	imshow("轮廓+背景", RoiSrcImg);
	waitKey(0);	
}

霍夫圆检测参考代码,

void HoughDetection2(const Mat& src_gray, const Mat& src_display, Mat& src_display2, int cannyThreshold, int accumulatorThreshold)
{
	//检测结果 向量
	std::vector<Vec3f> circles;
	//霍夫圆检测
	//参数:src,输出数组,霍夫梯度,dp?,最小圆心距,Canny阈值,累加阈值
	HoughCircles(src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows / 8, cannyThreshold, accumulatorThreshold, 0, 0);

	Mat display = src_display.clone();
	for (size_t i = 0; i < circles.size(); i++)
	{
		//Center ——(x,y)
		Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
		//半径——r
		int radius = cvRound(circles[i][2]);
		Point ptlt, ptlb, ptrb, ptrt;
		ptlt.x = circles[i][0] - radius;
		ptlt.y = circles[i][1] - radius;
		//
		ptlb.x = circles[i][0] - radius;
		ptlb.y = circles[i][1] + radius;
		/
		ptrb.x = circles[i][0] + radius;
		ptrb.y = circles[i][1] + radius;
		/
		ptrt.x = circles[i][0] + radius;
		ptrt.y = circles[i][1] - radius;
		/
		//中心圆
		circle(display, center, 3, Scalar(0, 255, 0), -1, 8, 0);
		if (0 == i)
		{
			//外轮廓圆
			circle(display, center, radius, Scalar(0, 0, 255), 3, 8, 0);
			circle(src_display2, center, radius, Scalar(0, 0, 0), -1);
			//rectangle(display, ptlt, ptrb, Scalar(0, 0, 255), 2, 8, 0);
			double darea = PI * radius*radius;
			string strarea = "area:" + doubleConverToString(darea);
			double dlength = 2 * PI*radius;
			string strperimeter = "length:" + doubleConverToString(dlength);
			putText(display, strarea, Point(cvRound(circles[i][0]), cvRound(circles[i][1])), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1, 8);
			//putText(display, strperimeter, Point(cvRound(circles[i][0]), cvRound(circles[i][1] + radius / 2)), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1, 8);
		}
		else if (1 == i)
		{
			//外轮廓圆
			circle(display, center, radius, Scalar(0, 0, 255), 3, 8, 0);
			circle(src_display2, center, radius, Scalar(0, 0, 0), -1);
			//rectangle(display, ptlt, ptrb, Scalar(0, 0, 255), 2, 8, 0);
			double darea = PI * radius*radius;
			string strarea = "area:" + doubleConverToString(darea);
			double dlength = 2 * PI*radius;
			string strperimeter = "length:" + doubleConverToString(dlength);
			putText(display, strarea, Point(cvRound(circles[i][0]), cvRound(circles[i][1] - radius / 2)), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1, 8);
			putText(display, strperimeter, Point(cvRound(circles[i][0]), cvRound(circles[i][1] + radius / 2)), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1, 8);
		}
		else if (2 == i)
		{
			//外轮廓圆
			circle(display, center, radius, Scalar(0, 0, 255), 3, 8, 0);
			circle(src_display2, center, radius, Scalar(0, 0, 0), -1);
			//rectangle(display, ptlt, ptrb, Scalar(0, 0, 255), 2, 8, 0);
			double darea = PI * radius*radius;
			string strarea = "area:" + doubleConverToString(darea);
			double dlength = 2 * PI*radius;
			string strperimeter = "length:" + doubleConverToString(dlength);
			//putText(display, strarea, Point(cvRound(circles[i][0]), cvRound(circles[i][1] - radius / 2)), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1, 8);
			putText(display, strperimeter, Point(cvRound(circles[i][0]), cvRound(circles[i][1] + radius / 2)), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1, 8);
		}
		else
		{

		}

	}

	imshow("轮廓检测和分析", display);
	waitKey(0);
	destroyAllWindows();
}

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法哥2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值