[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成长之路:直线、轮廓的提取与描述

时间: 2024-11-07 01:44:51

[OpenCV] Samples 05: convexhull的相关文章

[OpenCV] Samples 10: imagelist_creator

yaml写法的简单例子.将 $ ./ 1 2 3 4 5 命令的参数(代表图片地址)写入yaml中. 写yaml文件. 参考:[OpenCV] Samples 06: [ML] logistic regression 读xml文件. { /* * Jeff --> Load xml. * transform to Mat. * FileStorage. */ cout << "loading the dataset..."; // Step 1. FileStorag

[OpenCV] Samples 13: opencv_version

cv::CommandLineParser的使用. I suppose CommandLineParser::has("something") should be true when the command line has --something in it. ./a.out -h ./a.out --help 打印keys的相关内容. #include <opencv2/core/utility.hpp> #include <iostream> using

Android学习七---Hello OpenCV samples

创建一个能够使用OpenCV JavaCameraView的应用程序来了解基于OpenCV java API 的应用程序的开发流程.有了Android的基础,在程序中需要修改的几个地方1.activity_main.xml 2.AndroidManifest.xml 3.MainActivity.java 一.创建项目 安装创建android程序的方式创建一个blank activity,项目名称为hellosamples,其他采用默认的activity_main.xml,MainActivit

[OpenCV] Samples 12: laplace

先模糊再laplace,也可以替换为sobel等. 变换效果后录成视频,挺好玩. #include "opencv2/videoio/videoio.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/core/utility.hpp> #include <ctype.h>

[OpenCV] Samples 04: contours2

要先变为二值图像:cvThreshold 提取轮廓:cvFindContours #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include <math.h> #include <iostream> using namespace cv; using namespace std; static void help() { cout

[OpenCV] Samples 11: image sequence

一帧一帧地读取视频流. VideoCapture sequence(file_video); sequence >> image. #include <opencv2/core/core.hpp> #include <opencv2/videoio/videoio.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; using na

[OpenCV] Samples 07: create_mask

鼠标画线,圈地,构造相关mask图片(黑白). 支持鼠标左键右键中间键点击事件. /* * create_mask.cpp * * Author: * Siddharth Kherada <siddharthkherada27[at]gmail[dot]com> * * This tutorial demonstrates how to make mask image (black and white). * The program takes as input a source image

[OpenCV] Samples 08: edge

Canny edge detector 效率高,效果可控. TrackBar的使用. 技巧:gray找边缘后作为mask去CopyTo(). #include "opencv2/core/utility.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include <

CentOS系统上编译、安装、配置OpenCV

声明:本文是个人根据相关博客资料加上自己的经验整理,在此分享以供大家学习交流! 假设CentOS系统下的CodeBlocks已经安装完成,下面我们要在CentOS平台下编译OpenCV,并在CodeBlocks下进行开发测试. (1)下载OpenCV源码,并编译安装 当前最新的版本是OpenCV-2.2,可以从http://sourceforge.net/projects/opencvlibrary/上下载. OpenCV的编译方式有两种,一种是传统的./configure ; make ; m