OpenCV C++ findContours函数 轮廓检测

#include 
#include 
#include 
#include 


using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    Mat imageSource = imread("C:\\Users\\shang\\Desktop\\图片\\Image_20201108014812956.bmp", 0);
    //imshow("原始图像", imageSource);

    Mat image;
    GaussianBlur(imageSource, image, Size(3, 3), 0);//高斯滤波
    Canny(imageSource, image, 100, 250);

    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;

    findContours(image, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE, Point());
    Mat imageContours = Mat::zeros(image.size(), CV_8UC1);
    Mat Contours = Mat::zeros(image.size(), CV_8UC1); //绘制

    for (int i = 0; i < contours.size(); i++)
    {
        //contours[i]代表的是第i个轮廓,contours[i].size()代表的是第i个轮廓上所有的像素点数
        for (int j = 0; j < contours[i].size(); j++)
        {
            //绘制出contours向量所有的像素点
            Point P = Point(contours[i][j].x, contours[i][j].y);
            Contours.at<uchar>(P) = 255;
        }
        //输出hierarchy向量内容
        char ch[256];
        sprintf_s(ch, "%d", i);
        string str = ch;
        cout << "向量hierarchy的第" << str << "个元素内容为:" << hierarchy[i] << endl << endl;
        //绘制轮廓
        drawContours(imageContours, contours, i, Scalar(255), 1, 8, hierarchy);
    }
    imshow("Contours Image CV_TRET_TREE", imageContours); //轮廓
    imshow("Point of Contours CV_CHAIN_APPROX_NONE", Contours); //向量contours内保存的所有轮廓点集
    waitKey(0);
    return 0;
}```

你可能感兴趣的:(图像处理,c++,opencv)