case3 利用面积和横纵比进行轮廓筛选

 <textarea readonly="readonly" name="code" class="c++"> 
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
Mat src, dst;
int main(int argc, char** argv)
{
	src = imread("E:/picture/case3.jpg", IMREAD_GRAYSCALE);
	imshow("Input image", src);
	//xingtaixue  闭操作把区域连起来
	threshold(src, src, 0, 255, THRESH_BINARY | THRESH_OTSU);
	Mat morph_img;
	Mat kernel1 = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1, -1));
	morphologyEx(src, morph_img, MORPH_CLOSE, kernel1, Point(-1, -1));
	//imshow("erod_image", morph_img);
	//开把小白色区域去掉
	Mat kernel2 = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	morphologyEx(morph_img, morph_img, MORPH_OPEN, kernel1, Point(-1, -1));
	imshow("erod_image", morph_img);
	

	// find contours
	Mat resultimg = Mat:: zeros(src.size(), CV_8UC3);
	vector<vector<Point>> contours;
	vector<Vec4i> hireachy;
	findContours(morph_img,contours,hireachy,RETR_TREE,CHAIN_APPROX_SIMPLE);
	Point cricle_point;
	//面积过滤
	for (size_t k = 0; k < contours.size(); k++)
	{
		double area = contourArea(contours[k]);
		if (area < 100)
			continue;
		Rect rect = boundingRect(contours[k]);
		//横纵比过滤
		float ratio = float(rect.width) / float(rect..height);
		if (ratio<1.1&&ratio>0.9)
		{
			drawContours(resultimg, contours, k, Scalar(0, 0, 255), 1, 8);
		}
	}
	imshow("Outimage", resultimg);
	waitKey(0);
	return 0;
}
</textarea>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值