opencv之寻找轮廓findContours

本文详细介绍OpenCV库中的findContours函数,演示如何在二值图像中搜索轮廓,并配合drawContours进行可视化。通过C++实例展示了从读取图像、灰度处理、边缘检测到轮廓提取和绘制的完整流程。
摘要由CSDN通过智能技术生成

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<vector<Point>>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<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
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<vector<Point>>contours;
	vector<Vec4i>hierarchy;
	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;
}

结果展示:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于OpenCV C++中的findContours函数,它用于在图像中查找轮廓。 函数原型如下: ``` void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset = Point()) ``` 参数解释: - image:输入图像,要求是8位单通道二进制图像(黑白图像)。 - contours:输出的轮廓向量,每个轮廓都是一个点向量。 - hierarchy:输出的轮廓层次结构,用于描述轮廓之间的关系。 - mode:轮廓检索模式,有以下几种选择: - `CV_RETR_EXTERNAL`:只检测外部轮廓。 - `CV_RETR_LIST`:检测所有轮廓,不建立轮廓间的层次关系。 - `CV_RETR_CCOMP`:检测所有轮廓,并将其组织为两层的层次结构。 - `CV_RETR_TREE`:检测所有轮廓,并将其组织为树状结构。 - method:轮廓近似方法,有以下几种选择: - `CV_CHAIN_APPROX_NONE`:保存所有的轮廓点。 - `CV_CHAIN_APPROX_SIMPLE`:压缩水平、垂直和斜对角线段,只保留其端点。 - `CV_CHAIN_APPROX_TC89_L1`和`CV_CHAIN_APPROX_TC89_KCOS`:使用Telesco-Kanade算法进行轮廓近似。 - offset:可选参数,表示轮廓点的偏移量。 使用示例: ```cpp #include <opencv2/opencv.hpp> using namespace cv; int main() { Mat image = imread("image.jpg", 0); // 以灰度图像读取图片 if (image.empty()) { return -1; } // 二值化处理 Mat thresholded; threshold(image, thresholded, 128, 255, THRESH_BINARY); // 寻找轮廓 std::vector<std::vector<Point>> contours; std::vector<Vec4i> hierarchy; findContours(thresholded, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // 绘制轮廓 Mat result = Mat::zeros(image.size(), CV_8UC3); for (int i = 0; i < contours.size(); i++) { Scalar color = Scalar(0, 0, 255); // 蓝色 drawContours(result, contours, i, color, 2, LINE_8, hierarchy); } imshow("Contours", result); waitKey(0); return 0; } ``` 这是一个简单的示例,它加载一张图片,对其进行二值化处理,然后找到并绘制轮廓。请根据您的实际需求适当调整代码。 希望能对您有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值