1 /* This sample code was originally provided by Liu Liu
2 * Copyright (C) 2009, Liu Liu All rights reserved.
5 #include "opencv2/highgui/highgui.hpp"
6 #include "opencv2/features2d/features2d.hpp"
7 #include "opencv2/imgproc/imgproc.hpp"
17 cout << "\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 <path_and_image_filename, Default is 'puzzle.png'>\n\n";
23 static const Vec3b bcolors[] =
36 int main( int argc, char** argv )
39 Mat img0, img, yuv, gray, ellipses;
42 img0 = imread( argc != 2 ? "puzzle.png" : argv[1], 1 );
46 cout << "\nUsage: mser_sample <path_to_image>\n";
48 cout << "Unable to load image " << argv[1] << endl;
52 cvtColor(img0, yuv, COLOR_BGR2YCrCb);
53 cvtColor(img0, gray, COLOR_BGR2GRAY);
54 cvtColor(gray, img, COLOR_GRAY2BGR);
57 vector<vector<Point> > contours;
58 double t = (double)getTickCount();
59 MSER()(yuv, contours);
60 t = (double)getTickCount() - t;
61 printf( "MSER extracted %d contours in %g ms.\n", (int)contours.size(),
62 t*1000./getTickFrequency() );
64 // draw mser's with different colors
65 for( int i = (int)contours.size()-1; i >= 0; i-- )
67 const vector<Point>& r = contours[i];
68 for ( int j = 0; j < (int)r.size(); j++ )
71 img.at<Vec3b>(r[j]) = bcolors[i%9];
74 // find ellipse (it seems cvfitellipse2 have error or sth?)
75 RotatedRect box = fitEllipse( r );
77 box.angle=(float)CV_PI/2-box.angle;
78 ellipse( ellipses, box, Scalar(196,255,255), 2 );
81 imshow( "original", img0 );
82 imshow( "response", img );
83 imshow( "ellipses", ellipses );