程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

OpenCV不规则ROI提取

发布于2019-10-29 16:59     阅读(1815)     评论(0)     点赞(2)     收藏(5)


提取不规则感兴趣区域

void anomalyRoi(Mat &src, Mat &dst)
{
	Mat original = src.clone();
	//高斯滤波
	GaussianBlur(original, original, Size(3, 3), 11, 11, 4);
	//灰度化
	cvtColor(original, original, COLOR_RGB2GRAY);
	//二值化
	Mat threshold_image;
	threshold(original, threshold_image, 90, 225, 3);

	//形态学操作,Size的第个值是水平方向,第二值是垂直方向
	Mat morphologyKernel = getStructuringElement(MORPH_RECT, Size(1, 3));
	//闭运算
	morphologyEx(threshold_image, threshold_image, CV_MOP_CLOSE, morphologyKernel);

	Canny(threshold_image, threshold_image, 45, 45 * 3);
	imshow("Canny", threshold_image);
	vector<vector<Point>> contours;
	vector<Vec4i> hireachy;

	//找轮廓
	findContours(threshold_image, contours, hireachy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(-1, -1));
	int flag_count = 0;
	///vector< vector<Point>> contours_poly(contours.size());
	//建一个全黑的图像
	Mat show_threImage = Mat::zeros(threshold_image.size(), CV_8UC3);
	double s_area = 0;
	for (size_t i = 0; i < contours.size(); i++)
	{
		//在全黑的图像上画轮廓
		drawContours(show_threImage, contours, static_cast<int>(i), Scalar(255, 255, 255), 2, 8, hireachy, 0, Point()); 
		//利用面积进行判断是否为最大区域
		double area = contourArea(contours[i]);
		if (s_area < area)
		{
			s_area = area;
		}
		else
		{
			s_area = s_area;
		}

		if (area == s_area)
		{
			flag_count = static_cast<int>(i);
		}
		else
		{
			flag_count = flag_count;
		}
	}

	imshow("Draw_Image_Contours", show_threImage);

	Mat gray(src.size(), src.type(), Scalar(0, 0, 0));
	drawContours(gray, contours, flag_count, Scalar(255, 255, 255), 4, 8, hireachy, 0, Point());
	namedWindow("gray", 0);
	imshow("gray", gray);
	
	//为了找内部的一个漫水填充的种子点
	Rect s = boundingRect(contours[flag_count]);
	//黑色区域变成白色,遇到白色区域停止
	floodFill(gray, Point(s.x + s.width / 2, s.y + s.height / 2), Scalar(255,255,255));
	bitwise_and(src, gray, dst);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66


所属网站分类: 技术文章 > 博客

作者:头疼不是病

链接:https://www.pythonheidong.com/blog/article/147690/8c6fc7b23af3f6d5abc3/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)