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

opencv之寻找轮廓findContours

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

opencv之寻找轮廓findContours

findContours函数用于在二值图中寻找轮廓。

void cv::findContours(InputArrayimage,
OutputArrayOfArrayscontours, //检测到的轮廓,运算结果存储在这,每一个轮廓存储为一个点向量,即point类型中的vector表示
OutputArrayhierarchy,//可选的输出向量,包含图像的拓扑信息,每个轮廓contours[i]对应4个hierarchy元素hierarchy[i][0]--[i][3]分别表示后一个轮廓,前一个轮廓,父轮廓,内嵌轮廓的索引编号。
int mode,  //轮廓检索模式
int method,  // 轮廓近似方法
Pointoffset = Point()  // 每个轮廓点的可选偏移量
)

findContours常与drawContours()函数配合使用

例如:

        vector>contours;

        findContours(img,contours,RETR_EXTERNAL,CHAIN_APPROX_NONE);

drawContour:

void cv::drawContours(InputOutputArrayimage,
InputArrayOfArrayscontours,//所有输入的轮廓,每个轮廓存储为一个点向量
int contourIdx, // 轮廓绘制的指示变量,若为负则绘制所有轮廓
const Scalar & color,
int thickness = 1,  //线条粗细
int lineType = LINE_8, //线条类型
InputArrayhierarchy = noArray(), //可选层次结构信息
int maxLevel = INT_MAX,//用于绘制轮廓的最大等级
Pointoffset = Point() //可选轮廓偏移参数
)

案例:

#include
#include
#include
#include
using namespace std;
using namespace cv;

//全局变量随机函数
RNG rng(12345);

int main()
{

	Mat src = imread("C:/Users/Administrator/Desktop/3.png");
	if (src.empty())
	{
		cout << "请检查图片是否存在..." << endl;
		return -1;
	}
	Mat grayimg;

	cvtColor(src, grayimg, COLOR_BGR2GRAY);
	blur(grayimg, grayimg, Size(5, 5));
	namedWindow("src");
	imshow("src", src);

	//初始化阈值
	int thresh = 80, thresh_max = 255;
	Mat edgeimg;
	Canny(grayimg, edgeimg, thresh, thresh * 2, 3);

	//查找轮廓
	vector>contours;
	vectorhierarchy;
	findContours(edgeimg, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	//绘制轮廓
	Mat drawimg = Mat::zeros(edgeimg.size(), CV_8UC3);
	for (int i = 0; i < contours.size(); i++)
	{
		//drawContours(drawimg, contours, i, Scalar::all(rng.uniform(0, 255)), 2, 8, hierarchy, 0, Point(0, 0));
		drawContours(drawimg, contours, i, Scalar(122,160,144), 2, 8, hierarchy, 0, Point(0, 0));
	}
	imshow("dstimg", drawimg);


	waitKey(0);
	return 0;
}

结果展示:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649623.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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