1 /* This sample code was originally provided by Liu Liu
2 * Copyright� 2009, Liu Liu All rights reserved.
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"
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"
20 "./mser_sample [--image_filename] <path_and_image_filename, default is 'puzzle.png'>\n\n");
23 static CvScalar colors[] =
38 static uchar bcolors[][3] =
52 int main( int argc, const char** argv )
56 CommandLineParser parser(argc, argv);
58 string imageFileName = parser.get<string>("image_filename", "puzzle.png");
62 img = cvLoadImage( imageFileName.c_str(), CV_LOAD_IMAGE_GRAYSCALE );
65 printf("Unable to load image %s\n",imageFileName.c_str());
70 IplImage* rsp = cvLoadImage( imageFileName.c_str(), CV_LOAD_IMAGE_COLOR );
71 IplImage* ellipses = cvCloneImage(rsp);
72 cvCvtColor(img,ellipses,CV_GRAY2BGR);
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 );
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-- )
87 CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
88 for ( int j = 0; j < r->total; j++ )
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];
96 // find ellipse ( it seems cvfitellipse2 have error or sth?
97 for ( int i = 0; i < contours->total; i++ )
99 CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
100 CvBox2D box = cvFitEllipse2( r );
101 box.angle=(float)CV_PI/2-box.angle;
104 cvEllipseBox( ellipses, box, colors[9], 2 );
106 cvEllipseBox( ellipses, box, colors[2], 2 );
110 cvSaveImage( "rsp.png", rsp );
112 cvNamedWindow( "original", 0 );
113 cvShowImage( "original", img );
115 cvNamedWindow( "response", 0 );
116 cvShowImage( "response", rsp );
118 cvNamedWindow( "ellipses", 0 );
119 cvShowImage( "ellipses", ellipses );
123 cvDestroyWindow( "original" );
124 cvDestroyWindow( "response" );
125 cvDestroyWindow( "ellipses" );
126 cvReleaseImage(&rsp);
127 cvReleaseImage(&img);
128 cvReleaseImage(&ellipses);