程式語言 - OpenCV - Image Processing - Find Contours



參考資訊:
https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html

main.cpp

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
    vector<vector<Point> > contours;

    Mat img = imread("main.jpg", CV_8UC1);
    if (img.empty())
    {
        printf("failed to load image\n");
        return -1;
    }
    namedWindow("image", 1);
    imshow("image", img);

    findContours(img, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);

    Mat cnt_img = Mat::zeros(250, 250, CV_8UC3);
    drawContours(cnt_img, contours, -1, Scalar(128, 255, 255));
    namedWindow("contours", 1);
    imshow("contours", cnt_img);
    waitKey();
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(main)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})

編譯並且執行

$ cmake .
$ make
$ ./main

完成