栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

OpenCV使用findContours查找轮廓和相关函数——C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV使用findContours查找轮廓和相关函数——C++

OpenCV使用findContours查找轮廓和相关函数——C++
  • findContours函数的使用

findContours函数的使用
//查找前景的区域
vector> contours;
vectorhierarchy;
findContours(Image_morp, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));

自己写的代码:用于得到轮廓的最小外界矩阵、最小外接圆、轮廓的面积等

//查找前景的区域
vector> contours;
vectorhierarchy;
findContours(Image_morp, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));

//cout << "查找前景的区域" << endl;
vector > contours_poly(contours.size());
vector boundRect(contours.size());
vectorcenter(contours.size());
vectorradius(contours.size());
cout << "查找前景的区域-定义存储器" << endl;

vector xs, xy;
cout << "contours.size(): " << contours.size() << endl;
if (contours.size() == 0)
{
	cout << "没有任何前景" << endl;
	return 0;
}

float image_area = 0.0;
for (int i = 0; i < contours.size(); i++)
{
	approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);
	cout << "进入函数" << endl;
	boundRect[i] = boundingRect(Mat(contours[i]));
	image_area += contourArea(contours[i]);
	cout << "是空的" << endl;
	cout << boundRect[i].x << "     ii " << endl;
	cout << boundRect[i].y << "     ii " << endl;
	cout << boundRect[i].width << "     ii " << endl;
	cout << boundRect[i].height << "     ii " << endl;
	cout << boundRect[i] << "     ii " << endl;
	xs.push_back(boundRect[i].x);
	xs.push_back(boundRect[i].x + boundRect[i].width);
	xy.push_back(boundRect[i].y);
	xy.push_back(boundRect[i].y + boundRect[i].height);

	minEnclosingCircle(contours[i], center[i], radius[i]);
}
int x_max, x_min, y_max, y_min;
x_max = *max_element(xs.begin(), xs.end());
x_min = *min_element(xs.begin(), xs.end());
y_max = *max_element(xy.begin(), xy.end());
y_min = *min_element(xy.begin(), xy.end());

cout << "x_max: v " << x_max << endl;
cout << "x_min: v " << x_min << endl;
cout << "y_max: v " << y_max << endl;
cout << "y_min: v " << y_min << endl;
cout << "src.col: v " << pBkImage.cols << endl;
cout << " src.rows: v " << pBkImage.rows << endl;
cout << " 总面积为:  " << image_area << endl;

//画出矩阵和圆形
Mat drawing = Mat::zeros(Image_threshold.size(), CV_8UC3);
for (int i = 0; i < contours.size(); i++)
{
	Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
	drawContours(drawing, contours_poly, i, color, 1, 8, vector(), 0, Point());
	rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
	circle(drawing, center[i], (int)radius[i], color, 2, 8, 0);
}

//显示在一个窗口
namedWindow("Contours", CV_WINDOW_AUTOSIZE);
imshow("Contours", drawing);
waitKey(0);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384333.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号