码迷,mamicode.com
首页 > 其他好文 > 详细

[OpenCV] Samples 05: convexhull

时间:2016-11-26 23:07:20      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:str   ios   function   应用   bre   mon   std   入参   凸包   

得到了复杂轮廓往往不适合特征的检测,这里再介绍一个点集凸包络的提取函数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

标签:str   ios   function   应用   bre   mon   std   入参   凸包   

原文地址:http://www.cnblogs.com/jesse123/p/6105031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!