轮廓发现(find contour in your image)

1、轮廓发现(find contour);
2、代码演示;

轮廓发现(find contour)

1、轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法,一般用来寻找二值图像的轮廓,所以边缘提取的阈值选定会影响最终轮廓发现结果;

相关API

1、发现轮廓API : findContours();
2、绘制轮廓API : drawContours();


参数说明:





注: maxlevel : 0–绘制当前的,1–绘制当前及其内嵌的轮廓

Code

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat src, dst;
const char *input_win = "input image";
const char *output_win = "contour image";

int threshold_value = 100;
int threshold_max = 255;


void Contour_Demo(int, void *);

int main(int argc, char** argv)
{
   
	src = imread("C:\\Users\\hello\\Desktop\\15.jpg");
	if (src.empty())
	{
   
		cout << "could not load the image..." << endl;
		return -1;
	}
	namedWindow(input_win, WINDOW_AUTOSIZE);
	namedWindow(output_win, WINDOW_AUTOSIZE);
	cvtColor(src, src, CV_BGR2GRAY);
	imshow(input_win, src);

	createTrackbar("Threshold Value:", input_win, &threshold_value, threshold_max, Contour_Demo);
	Contour_Demo(0, 0);

	//阈值通过trackbar找到最合适的值;
	
	waitKey(0);
	return 0;
}
//定义Callback 回调函数
void Contour_Demo(int, void *)
{
   
	//进行滤波
	Mat gaus,edge_image;
	GaussianBlur(src, gaus, Size(3, 3), 0, 0);
	//Canny边缘检测
	Canny(gaus, edge_image, threshold_value, threshold_value * 2, 3,false);
	//发现轮廓
	vector<vector<Point>> contourpoints;   //vector向量的使用会影响速度
	vector<Vec4i> hierachy;  //存储返回的拓扑结构
	findContours(edge_image, contourpoints, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	
	dst = Mat::zeros(src.size(), CV_8UC3);  //初始化dst对象
	RNG rng(12345);
	for (size_t i = 0; i < contourpoints.size(); i++)   //将contourpoints中的点全部描绘出来
	{
   
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		drawContours(dst, contourpoints, i, color, 2, LINE_8, hierachy, 0, Point(0, 0));
	}
	//颜色相同的部分是同一轮廓
	imshow(output_win, dst);  //显示结果

}

效果

注:轮廓检测的效果受Canny算法中阈值threshold的影响较大,如下图:需要自己去寻找合适的阈值