00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "opencv2/highgui/highgui.hpp"
00010 #include "opencv2/features2d/features2d.hpp"
00011 #include "opencv2/imgproc/imgproc_c.h"
00012
00013
00014 void help()
00015 {
00016 printf("\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
00017 "It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
00018 "\nCall:\n"
00019 "./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n");
00020 }
00021
00022 static CvScalar colors[] =
00023 {
00024 {{0,0,255}},
00025 {{0,128,255}},
00026 {{0,255,255}},
00027 {{0,255,0}},
00028 {{255,128,0}},
00029 {{255,255,0}},
00030 {{255,0,0}},
00031 {{255,0,255}},
00032 {{255,255,255}},
00033 {{196,255,255}},
00034 {{255,255,196}}
00035 };
00036
00037 static uchar bcolors[][3] =
00038 {
00039 {0,0,255},
00040 {0,128,255},
00041 {0,255,255},
00042 {0,255,0},
00043 {255,128,0},
00044 {255,255,0},
00045 {255,0,0},
00046 {255,0,255},
00047 {255,255,255}
00048 };
00049
00050
00051 int main( int argc, char** argv )
00052 {
00053 char path[1024];
00054 IplImage* img;
00055 help();
00056 if (argc!=2)
00057 {
00058 strcpy(path,"puzzle.png");
00059 img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
00060 if (!img)
00061 {
00062 printf("\nUsage: mser_sample <path_to_image>\n");
00063 return 0;
00064 }
00065 }
00066 else
00067 {
00068 strcpy(path,argv[1]);
00069 img = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE );
00070 }
00071
00072 if (!img)
00073 {
00074 printf("Unable to load image %s\n",path);
00075 return 0;
00076 }
00077 IplImage* rsp = cvLoadImage( path, CV_LOAD_IMAGE_COLOR );
00078 IplImage* ellipses = cvCloneImage(rsp);
00079 cvCvtColor(img,ellipses,CV_GRAY2BGR);
00080 CvSeq* contours;
00081 CvMemStorage* storage= cvCreateMemStorage();
00082 IplImage* hsv = cvCreateImage( cvGetSize( rsp ), IPL_DEPTH_8U, 3 );
00083 cvCvtColor( rsp, hsv, CV_BGR2YCrCb );
00084 CvMSERParams params = cvMSERParams();
00085
00086 double t = (double)cvGetTickCount();
00087 cvExtractMSER( hsv, NULL, &contours, storage, params );
00088 t = cvGetTickCount() - t;
00089 printf( "MSER extracted %d contours in %g ms.\n", contours->total, t/((double)cvGetTickFrequency()*1000.) );
00090 uchar* rsptr = (uchar*)rsp->imageData;
00091
00092 for ( int i = contours->total-1; i >= 0; i-- )
00093 {
00094 CvSeq* r = *(CvSeq**)cvGetSeqElem( contours, i );
00095 for ( int j = 0; j < r->total; j++ )
00096 {
00097 CvPoint* pt = CV_GET_SEQ_ELEM( CvPoint, r, j );
00098 rsptr[pt->x*3+pt->y*rsp->widthStep] = bcolors[i%9][2];
00099 rsptr[pt->x*3+1+pt->y*rsp->widthStep] = bcolors[i%9][1];
00100 rsptr[pt->x*3+2+pt->y*rsp->widthStep] = bcolors[i%9][0];
00101 }
00102 }
00103
00104 for ( int i = 0; i < contours->total; i++ )
00105 {
00106 CvContour* r = *(CvContour**)cvGetSeqElem( contours, i );
00107 CvBox2D box = cvFitEllipse2( r );
00108 box.angle=(float)CV_PI/2-box.angle;
00109
00110 if ( r->color > 0 )
00111 cvEllipseBox( ellipses, box, colors[9], 2 );
00112 else
00113 cvEllipseBox( ellipses, box, colors[2], 2 );
00114
00115 }
00116
00117 cvSaveImage( "rsp.png", rsp );
00118
00119 cvNamedWindow( "original", 0 );
00120 cvShowImage( "original", img );
00121
00122 cvNamedWindow( "response", 0 );
00123 cvShowImage( "response", rsp );
00124
00125 cvNamedWindow( "ellipses", 0 );
00126 cvShowImage( "ellipses", ellipses );
00127
00128 cvWaitKey(0);
00129
00130 cvDestroyWindow( "original" );
00131 cvDestroyWindow( "response" );
00132 cvDestroyWindow( "ellipses" );
00133 cvReleaseImage(&rsp);
00134 cvReleaseImage(&img);
00135 cvReleaseImage(&ellipses);
00136
00137 }