程式語言 - OpenCV - Image Processing - Convex Hull



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

main.cpp

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

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
    vector<Point> pt;
    Mat img(250, 250, CV_8UC3);

    pt.push_back(Point(50, 50));
    pt.push_back(Point(70, 220));
    pt.push_back(Point(210, 30));
    pt.push_back(Point(200, 220));

    vector<int> hull;
    convexHull(Mat(pt), hull, true);
    img = Scalar::all(0);
    for (int i = 0; i < 4; i++)
    {
        circle(img, pt[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);
    }

    int hullcount = (int)hull.size();
    Point pt0 = pt[hull[hullcount - 1]];
    for (int i = 0; i < hullcount; i++)
    {
        Point p = pt[hull[i]];
        line(img, pt0, p, Scalar(0, 255, 0), 1, LINE_AA);
        pt0 = p;
    }
    imshow("hull", 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

完成