[OpenCV] Samples 05: convexhull

Posted 机器学习水很深

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[OpenCV] Samples 05: convexhull相关的知识,希望对你有一定的参考价值。

得到了复杂轮廓往往不适合特征的检测,这里再介绍一个点集凸包络的提取函数convexHull,输入参数就可以是contours组中的一个轮廓,返回外凸包络的点集 ---- 如此就能去掉凹进去的边。

对于凸包算法,其中最有名的莫过于Graham扫描算法,它的复杂度为nlog(n)

参考:计算几何之凸包(Algorithm show), 寻找轮廓

高级:Snake模型在轮廓提取中的应用 cvSnakeImage()

 

 

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

using namespace cv;
using namespace std;

static void help()
{
    cout << "\\nThis sample program demonstrates the use of the convexHull() function\\n"
         << "Call:\\n"
         << "./convexhull\\n" << endl;
}

int main( int argc, char** argv )
{
    CommandLineParser parser(argc, argv, "{help h||}");
    if (parser.has("help"))
    {
        help();
        return 0;
    }
    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

    for(;;)
    {
        char key;
        int i, count = (unsigned)rng%100 + 1;

        vector<Point> points;

        for( i = 0; i < count; i++ )
        {
            Point pt;
            pt.x = rng.uniform(img.cols/4, img.cols*3/4);
            pt.y = rng.uniform(img.rows/4, img.rows*3/4);

            points.push_back(pt);
        }

        // Jeff --> hull is the indice of corner points
        vector<int> hull;
        convexHull(Mat(points), hull, true);

/******************************************************************************/

        // Jeff --> draw the effect.
        img = Scalar::all(0);
        for( i = 0; i < count; i++ )
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);

        int hullcount = (int)hull.size();
        cout << hullcount << endl;
        Point pt0 = points[hull[hullcount-1]];

        for( i = 0; i < hullcount; i++ )
        {
            // Jeff --> extract corners.
            Point pt = points[hull[i]];
            line(img, pt0, pt, Scalar(0, 255, 0), 1,LINE_AA);
            pt0 = pt;
        }

        imshow("hull", img);

        key = (char)waitKey();
        if( key == 27 || key == \'q\' || key == \'Q\' ) // \'ESC\'
            break;
    }

    return 0;
}

  


 

轮廓的进一步描述

 

进一步参见:OpenCV成长之路:直线、轮廓的提取与描述

以上是关于[OpenCV] Samples 05: convexhull的主要内容,如果未能解决你的问题,请参考以下文章

opencv中的Conv2

Keras Conv2D 和输入通道

[OpenCV] Samples 08: edge

[OpenCV] Samples 13: opencv_version

[OpenCV] Samples 12: laplace

卷积:conv2 matlab 到 opencv