convexhull.cpp 1.41 KB
Newer Older
a  
Kai Westerkamp committed
1 2 3
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <fstream>
wester committed
4 5 6 7 8 9 10 11 12 13 14 15
#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;
}

wester committed
16
int main( int /*argc*/, char** /*argv*/ )
wester committed
17 18 19 20
{
    Mat img(500, 500, CV_8UC3);
    RNG& rng = theRNG();

wester committed
21 22
    help();

wester committed
23 24
    for(;;)
    {
a  
Kai Westerkamp committed
25
        char key;
wester committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        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);
        }

        vector<int> hull;
        convexHull(Mat(points), hull, true);

        img = Scalar::all(0);
        for( i = 0; i < count; i++ )
wester committed
44
            circle(img, points[i], 3, Scalar(0, 0, 255), CV_FILLED, CV_AA);
wester committed
45 46 47 48 49 50 51

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

        for( i = 0; i < hullcount; i++ )
        {
            Point pt = points[hull[i]];
wester committed
52
            line(img, pt0, pt, Scalar(0, 255, 0), 1, CV_AA);
wester committed
53 54 55 56 57
            pt0 = pt;
        }

        imshow("hull", img);

a  
Kai Westerkamp committed
58
        key = (char)waitKey();
wester committed
59 60 61 62 63 64
        if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
            break;
    }

    return 0;
}