convexhull.cpp 1.41 KB
Newer Older
1 2
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
Gary Bradski's avatar
Gary Bradski committed
3 4
#include <fstream>
#include <iostream>
5 6

using namespace cv;
Gary Bradski's avatar
Gary Bradski committed
7 8
using namespace std;

9
static void help()
Gary Bradski's avatar
Gary Bradski committed
10
{
11 12 13
    cout << "\nThis sample program demonstrates the use of the convexHull() function\n"
         << "Call:\n"
         << "./convexhull\n" << endl;
Gary Bradski's avatar
Gary Bradski committed
14
}
15

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

Gary Bradski's avatar
Gary Bradski committed
21 22
    help();

23 24 25 26
    for(;;)
    {
        char key;
        int i, count = (unsigned)rng%100 + 1;
27

28 29 30 31 32 33 34
        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);
35

36 37
            points.push_back(pt);
        }
38

39
        vector<int> hull;
Ilya Lysenkov's avatar
Ilya Lysenkov committed
40
        convexHull(Mat(points), hull, true);
41

42 43
        img = Scalar::all(0);
        for( i = 0; i < count; i++ )
44
            circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);
45

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

49 50 51
        for( i = 0; i < hullcount; i++ )
        {
            Point pt = points[hull[i]];
52
            line(img, pt0, pt, Scalar(0, 255, 0), 1,LINE_AA);
53 54 55 56 57 58 59 60 61 62 63 64
            pt0 = pt;
        }

        imshow("hull", img);

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

    return 0;
}