图像轮廓发现
基本概念图像轮廓
其实就是图像的边界,主要是针对二值图像,轮廓是一系列点的集合
获取图像时,对轮廓进行组织描述
把所有的轮廓变成一个树形结构
基于连通组件的轮廓发现
反映图像拓扑结构
算法原理
部分重要的参数
代码演示
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main() {
Mat src = imread("D:/ps/te.png");
if (src.empty())
{
cout << "could not find the image";
return -1;
}
//namedWindow("input", WINDOW_FREERATIO);
//imshow("input", src);
//二值化
GaussianBlur(src, src, Size(3, 3), 0);
//高斯模糊
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
//转为灰度图像
threshold(gray, binary,0,255, THRESH_BINARY | THRESH_OTSU);
//二值化
imshow("binary", binary);
vector<vector<Point>>contours;
//用于存放轮廓的vector
vector<Vec4i>hierachy;
//CHAIN_APPROX_SIMPLE 和CHAIN_APPROX_NONE 顶点 全连
findContours(binary, contours, hierachy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE,Point());
//寻找轮廓
drawContours(src, contours, -1, Scalar(0, 0, 255), 2, 8);
imshow("find contours demo", src);
waitKey(0);
destroyAllWindows();
return 0;
}
代码实现需要用到的API
findContours()
drawContours()