converted some more samples to C++
[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/highgui/highgui.hpp"
6 #include "opencv2/features2d/features2d.hpp"
7 #include "opencv2/imgproc/imgproc_c.h"
8
9 static CvScalar colors[] = 
10 {
11     {{0,0,255}},
12     {{0,128,255}},
13     {{0,255,255}},
14     {{0,255,0}},
15     {{255,128,0}},
16     {{255,255,0}},
17     {{255,0,0}},
18     {{255,0,255}},
19     {{255,255,255}},
20     {{196,255,255}},
21     {{255,255,196}}
22 };
23
24 static uchar bcolors[][3] = 
25 {
26     {0,0,255},
27     {0,128,255},
28     {0,255,255},
29     {0,255,0},
30     {255,128,0},
31     {255,255,0},
32     {255,0,0},
33     {255,0,255},
34     {255,255,255}
35 };
36
37 int main( int argc, char** argv )
38 {
39         char path[1024];
40         IplImage* img;
41         if (argc!=2)
42         {
43                 strcpy(path,"puzzle.png");
44                 img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
45                 if (!img)
46                 {
47                         printf("\nUsage: mser_sample <path_to_image>\n");
48                         return 0;
49                 }
50         }
51         else
52         {
53                 strcpy(path,argv[1]);
54                 img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
55         }
56         
57         if (!img)
58         {
59                 printf("Unable to load image %s\n",path);
60                 return 0;
61         }
62         IplImage* rsp = cvLoadImage( path, CV_LOAD_IMAGE_COLOR );
63         IplImage* ellipses = cvCloneImage(rsp);
64         cvCvtColor(img,ellipses,CV_GRAY2BGR);
65         CvSeq* contours;
66         CvMemStorage* storage= cvCreateMemStorage();
67         IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
68         cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
69         CvMSERParams params = cvMSERParams();//cvMSERParams( 5, 60, cvRound(.2*img->width*img->height), .25, .2 );
70
71         double t = (double)cvGetTickCount();
72         cvExtractMSER( hsv, NULL, &contours, storage, params );
73         t = cvGetTickCount() - t;
74         printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
75         uchar* rsptr = (uchar*)rsp->imageData;
76         // draw mser with different color
77         for ( int i = contours->total-1; i >= 0; i-- )
78         {
79                 CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
80                 for ( int j = 0; j < r->total; j++ )
81                 {
82                         CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
83                         rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
84                         rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
85                         rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
86                 }
87         }
88         // find ellipse ( it seems cvfitellipse2 have error or sth?
89         for ( int i = 0; i < contours->total; i++ )
90         {
91                 CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
92                 CvBox2D box = cvFitEllipse2( r );
93                 box.angle=(float)CV_PI/2-box.angle;
94                 
95                 if ( r->color > 0 )
96                         cvEllipseBox( ellipses, box, colors[9], 2 );
97                 else
98                         cvEllipseBox( ellipses, box, colors[2], 2 );
99                         
100         }
101
102         cvSaveImage( "rsp.png", rsp );
103
104         cvNamedWindow( "original", 0 );
105         cvShowImage( "original", img );
106         
107         cvNamedWindow( "response", 0 );
108         cvShowImage( "response", rsp );
109
110         cvNamedWindow( "ellipses", 0 );
111         cvShowImage( "ellipses", ellipses );
112
113         cvWaitKey(0);
114
115         cvDestroyWindow( "original" );
116         cvDestroyWindow( "response" );
117         cvDestroyWindow( "ellipses" );
118         cvReleaseImage(&rsp);
119         cvReleaseImage(&img);
120         cvReleaseImage(&ellipses);
121         
122 }