设为首页收藏本站 |天气与日历| 2025-04-20 星期日 06:01:00 乙巳(蛇)年 三月廿三 卯时 谷雨
     
切换到窄版

私人站点

 找回密码
 立即注册
搜索
查看: 415|回复: 0

轮廓发现(find contour)

[复制链接]

954

主题

954

帖子

3875

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3875
发表于 2022-3-13 09:21:21 | 显示全部楼层 |阅读模式
1.png
轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法。
所以边缘提取的阈值选定会影响最终轮廓发现结果




API:
轮廓发现(find contour)


在二值图像上发现轮廓使用
[C++] 纯文本查看 复制代码
API cv::findContours(
InputOutputArray  binImg, // 输入图像,非0的像素被看成1,0的像素值保持不变,8-bit
 OutputArrayOfArrays  contours,//  全部发现的轮廓对象
OutputArray,  hierachy// 图该的拓扑结构,可选,该轮廓发现算法正是基于图像拓扑结构实现。
int mode, //  轮廓返回的模式
int method,// 发现方法
Point offset=Point()//  轮廓像素的位移,默认(0, 0)没有位移
)



轮廓绘制(draw contour)

在二值图像上发现轮廓使用API cv::findContours之后对发现的轮廓数据进行绘制显示

[C++] 纯文本查看 复制代码
drawContours(
InputOutputArray  binImg, // 输出图像
 OutputArrayOfArrays  contours,//  全部发现的轮廓对象
Int contourIdx// 轮廓索引号
const Scalar & color,// 绘制时候颜色
int  thickness,// 绘制线宽
int  lineType ,// 线的类型LINE_8
InputArray hierarchy,// 拓扑结构图
int maxlevel,// 最大层数, 0只绘制当前的,1表示绘制绘制当前及其内嵌的轮廓
Point offset=Point()// 轮廓位移,可选





处理流程

输入图像转为灰度图像cvtColor
使用Canny进行边缘提取,得到二值图像
使用findContours寻找轮廓
使用drawContours绘制轮廓



[C++] 纯文本查看 复制代码
/*
注意: 轮廓的发现和绘制

处理流程
输入图像转为灰度图像cvtColor
使用Canny进行边缘提取,得到二值图像
使用findContours寻找轮廓
使用drawContours绘制轮廓

API cv::findContours(
InputOutputArray  binImg, // 输入图像,非0的像素被看成1,0的像素值保持不变,8-bit
 OutputArrayOfArrays  contours,//  全部发现的轮廓对象
OutputArray,  hierachy// 图该的拓扑结构,可选,该轮廓发现算法正是基于图像拓扑结构实现。
int mode, //  轮廓返回的模式
int method,// 发现方法
Point offset=Point()//  轮廓像素的位移,默认(0, 0)没有位移
)

drawContours(
InputOutputArray  binImg, // 输出图像
 OutputArrayOfArrays  contours,//  全部发现的轮廓对象
Int contourIdx// 轮廓索引号
const Scalar & color,// 绘制时候颜色
int  thickness,// 绘制线宽
int  lineType ,// 线的类型LINE_8
InputArray hierarchy,// 拓扑结构图
int maxlevel,// 最大层数, 0只绘制当前的,1表示绘制绘制当前及其内嵌的轮廓
Point offset=Point()// 轮廓位移,可选

*/
#include <opencv2/opencv.hpp>
#include <iostream>
#include<math.h>
using namespace std;
using namespace cv;

#define anti_interference  0 // 值为1时开启中值滤波抗干扰
Mat src, dst, gray;
//声明演示轮廓函数
void Demo_Contours(int, void*);
//阈值
int threshold_val = 100;
//最大阈值
int thresholde_max = 255;

int main(int argc, char** argv) {
	src = imread("D:/IDE/opencv-3.1.0/demo.jpg");
	if (!src.data) {
		printf("加载图片异常\n");
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);
#if(anti_interference==1)
	{
		//中值滤波抗干扰
		medianBlur(src, src, 7);
	}
#endif
	//转灰度图
	cvtColor(src, src, CV_BGR2GRAY);
	//创建滑动条到窗口
	createTrackbar("轮廓演示:", "input", &threshold_val, thresholde_max, Demo_Contours);
	Demo_Contours(0, 0);

	waitKey(0);
	return 0;
}

 
void Demo_Contours(int, void*) {
	Mat canny_output;
	vector<vector<Point>> contourPoints;
	vector <Vec4i> hierachy;


	//检测边缘
	Canny(src, canny_output, threshold_val, threshold_val * 2,3, false);
	//查找轮廓
	findContours(canny_output, contourPoints, hierachy, RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0.0));

	//初始化dst图片,
	dst = Mat::zeros(src.size(), CV_8UC3);
	//设置随机数种子
	RNG rng = (12345);

	

	//绘制轮廓
	for (size_t i = 0; i < contourPoints.size(); i++) {
		//设置颜色为随机
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		//绘制
		drawContours(dst, contourPoints, i, color, 1, LINE_AA, hierachy, 0, Point(0, 0));
	}
	imshow("输出效果", dst);

}


bbbbbbb.jpg

ccc.jpg


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|编程站点 ( 冀ICP备2023028127号-2 )|友链申请|

GMT+8, 2025-4-20 06:01 , Processed in 0.089263 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表