some samples updated according to new CommandLineParser class
[profile/ivi/opencv.git] / samples / c / mser_sample.cpp
1 /* This sample code was originally provided by Liu Liu
2  * Copyright� 2009, Liu Liu All rights reserved.
3  */
4
5 #include <opencv2/core/core.hpp>
6 #include "opencv2/highgui/highgui.hpp"
7 #include "opencv2/features2d/features2d.hpp"
8 #include "opencv2/imgproc/imgproc_c.h"
9
10 #include <iostream>
11
12 using namespace std;
13 using namespace cv;
14
15 void help()
16 {
17     printf("\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
18            "It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
19            "\nCall:\n"
20            "./mser_sample [--image_filename] <path_and_image_filename, default is 'puzzle.png'>\n\n");
21 }
22
23 static CvScalar colors[] = 
24 {
25     {{0,0,255}},
26     {{0,128,255}},
27     {{0,255,255}},
28     {{0,255,0}},
29     {{255,128,0}},
30     {{255,255,0}},
31     {{255,0,0}},
32     {{255,0,255}},
33     {{255,255,255}},
34     {{196,255,255}},
35     {{255,255,196}}
36 };
37
38 static uchar bcolors[][3] = 
39 {
40     {0,0,255},
41     {0,128,255},
42     {0,255,255},
43     {0,255,0},
44     {255,128,0},
45     {255,255,0},
46     {255,0,0},
47     {255,0,255},
48     {255,255,255}
49 };
50
51
52 int main( int argc, const char** argv )
53 {
54     help();
55
56     CommandLineParser parser(argc, argv);
57
58     string imageFileName = parser.get<string>("image_filename", "puzzle.png");
59
60     IplImage* img;
61
62     img = cvLoadImage( imageFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
63     if (!img)
64     {
65         printf("Unable to load image %s\n",imageFileName.c_str());
66         help();
67         return 0;
68     }
69
70     IplImage* rsp = cvLoadImage( imageFileName.c_str(), CV_LOAD_IMAGE_COLOR );
71     IplImage* ellipses = cvCloneImage(rsp);
72     cvCvtColor(img,ellipses,CV_GRAY2BGR);
73     CvSeq* contours;
74     CvMemStorage* storage= cvCreateMemStorage();
75     IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
76     cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
77     CvMSERParams params = cvMSERParams();//cvMSERParams( 5, 60, cvRound(.2*img->width*img->height), .25, .2 );
78
79     double t = (double)cvGetTickCount();
80     cvExtractMSER( hsv, NULL, &contours, storage, params );
81     t = cvGetTickCount() - t;
82     printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
83     uchar* rsptr = (uchar*)rsp->imageData;
84     // draw mser with different color
85     for ( int i = contours->total-1; i >= 0; i-- )
86     {
87         CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
88         for ( int j = 0; j < r->total; j++ )
89         {
90             CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
91             rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
92             rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
93             rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
94         }
95     }
96     // find ellipse ( it seems cvfitellipse2 have error or sth?
97     for ( int i = 0; i < contours->total; i++ )
98     {
99         CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
100         CvBox2D box = cvFitEllipse2( r );
101         box.angle=(float)CV_PI/2-box.angle;
102
103         if ( r->color > 0 )
104             cvEllipseBox( ellipses, box, colors[9], 2 );
105         else
106             cvEllipseBox( ellipses, box, colors[2], 2 );
107
108     }
109
110     cvSaveImage( "rsp.png", rsp );
111
112     cvNamedWindow( "original", 0 );
113     cvShowImage( "original", img );
114
115     cvNamedWindow( "response", 0 );
116     cvShowImage( "response", rsp );
117
118     cvNamedWindow( "ellipses", 0 );
119     cvShowImage( "ellipses", ellipses );
120
121     cvWaitKey(0);
122
123     cvDestroyWindow( "original" );
124     cvDestroyWindow( "response" );
125     cvDestroyWindow( "ellipses" );
126     cvReleaseImage(&rsp);
127     cvReleaseImage(&img);
128     cvReleaseImage(&ellipses);
129 }