opencv学习日记——轮廓检测与绘制(效果很棒)
1、图像结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ffa8688664c5cb76df89060365f49f2d.png)
2、代码部分
#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("圆形.png");
if (img.empty())
{
cout << "输入为空" << endl;
return -1;
}
imshow("原图", img);
Mat gray, binary;
cvtColor(img, gray, COLOR_BGR2GRAY);
GaussianBlur(gray, gray, Size(13, 13), 4, 4);
threshold(gray, binary, 170, 255, THRESH_BINARY | THRESH_OTSU);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
for (int t = 0; t < contours.size(); t++)
{
drawContours(img, contours, t, Scalar(0, 0, 255), 2, 8);
}
for (int i = 0; i < hierarchy.size(); i++)
{
cout << hierarchy[i] << endl;
}
imshow("轮廓结果", img);
waitKey(0);
return 0;
}
‘’‘’‘’‘’‘’‘’‘’‘’
[-1, -1, 1, -1]
[4, -1, 2, 0]
[3, -1, -1, 1]
[-1, 2, -1, 1]
[5, 1, -1, 0]
[6, 4, -1, 0]
[12, 5, 7, 0]
[8, -1, -1, 6]
[9, 7, -1, 6]
[10, 8, -1, 6]
[11, 9, -1, 6]
[-1, 10, -1, 6]
[14, 6, 13, 0]
[-1, -1, -1, 12]
[15, 12, -1, 0]
[-1, 14, 16, 0]
[-1, -1, -1, 15]