fitellipse.cpp 2.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/********************************************************************************
*
*
*  This program is demonstration for ellipse fitting. Program finds
*  contours and approximate it by ellipses.
*
*  Trackbar specify threshold parametr.
*
*  White lines is contours. Red lines is fitting ellipses.
*
*
*  Autor:  Denis Burenkov.
*
*
*
********************************************************************************/
17
#include "opencv2/imgproc/imgproc.hpp"
18
#include "opencv2/imgcodecs.hpp"
19
#include "opencv2/highgui/highgui.hpp"
20 21 22
#include <iostream>
using namespace cv;
using namespace std;
23

24 25 26 27 28 29 30 31
// static void help()
// {
//     cout <<
//             "\nThis program is demonstration for ellipse fitting. The program finds\n"
//             "contours and approximate it by ellipses.\n"
//             "Call:\n"
//             "./fitellipse [image_name -- Default stuff.jpg]\n" << endl;
// }
Gary Bradski's avatar
Gary Bradski committed
32

33 34 35 36 37
int sliderPos = 70;

Mat image;

void processImage(int, void*);
38 39 40 41

int main( int argc, char** argv )
{
    const char* filename = argc == 2 ? argv[1] : (char*)"stuff.jpg";
42 43 44
    image = imread(filename, 0);
    if( image.empty() )
    {
Gary Bradski's avatar
Gary Bradski committed
45
        cout << "Couldn't open image " << filename << "\nUsage: fitellipse <image_name>\n";
46 47
        return 0;
    }
48

49 50
    imshow("source", image);
    namedWindow("result", 1);
51

52
    // Create toolbars. HighGUI use.
53 54
    createTrackbar( "threshold", "result", &sliderPos, 255, processImage );
    processImage(0, 0);
55 56

    // Wait for a key stroke; the same function arranges events processing
57
    waitKey();
58 59 60 61 62
    return 0;
}

// Define trackbar callback functon. This function find contours,
// draw it and approximate it by ellipses.
63
void processImage(int /*h*/, void*)
64
{
65 66
    vector<vector<Point> > contours;
    Mat bimage = image >= sliderPos;
67

68
    findContours(bimage, contours, RETR_LIST, CHAIN_APPROX_NONE);
69

70
    Mat cimage = Mat::zeros(bimage.size(), CV_8UC3);
71

72
    for(size_t i = 0; i < contours.size(); i++)
73
    {
74
        size_t count = contours[i].size();
75 76
        if( count < 6 )
            continue;
77

78 79 80
        Mat pointsf;
        Mat(contours[i]).convertTo(pointsf, CV_32F);
        RotatedRect box = fitEllipse(pointsf);
81

82 83 84
        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
            continue;
        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8);
85

86 87
        ellipse(cimage, box, Scalar(0,0,255), 1, LINE_AA);
        ellipse(cimage, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, LINE_AA);
88 89 90
        Point2f vtx[4];
        box.points(vtx);
        for( int j = 0; j < 4; j++ )
91
            line(cimage, vtx[j], vtx[(j+1)%4], Scalar(0,255,0), 1, LINE_AA);
92 93
    }

94
    imshow("result", cimage);
95
}