1 /* This sample code was originally provided by Liu Liu
2 * Copyright (C) 2009, Liu Liu All rights reserved.
5 #include <opencv2/core/utility.hpp>
6 #include "opencv2/highgui.hpp"
7 #include "opencv2/features2d.hpp"
8 #include "opencv2/imgproc.hpp"
18 cout << "\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
19 "It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
21 "./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n";
24 static const Vec3b bcolors[] =
37 int main( int argc, char** argv )
40 Mat img0, img, yuv, gray, ellipses;
43 img0 = imread( argc != 2 ? "puzzle.png" : argv[1], 1 );
47 cout << "\nUsage: mser_sample <path_to_image>\n";
49 cout << "Unable to load image " << argv[1] << endl;
53 cvtColor(img0, yuv, COLOR_BGR2YCrCb);
54 cvtColor(img0, gray, COLOR_BGR2GRAY);
55 cvtColor(gray, img, COLOR_GRAY2BGR);
58 vector<vector<Point> > contours;
59 double t = (double)getTickCount();
60 MSER()(yuv, contours);
61 t = (double)getTickCount() - t;
62 printf( "MSER extracted %d contours in %g ms.\n", (int)contours.size(),
63 t*1000./getTickFrequency() );
65 // draw mser's with different colors
66 for( int i = (int)contours.size()-1; i >= 0; i-- )
68 const vector<Point>& r = contours[i];
69 for ( int j = 0; j < (int)r.size(); j++ )
72 img.at<Vec3b>(pt) = bcolors[i%9];
75 // find ellipse (it seems cvfitellipse2 have error or sth?)
76 RotatedRect box = fitEllipse( r );
78 box.angle=(float)CV_PI/2-box.angle;
79 ellipse( ellipses, box, Scalar(196,255,255), 2 );
82 imshow( "original", img0 );
83 imshow( "response", img );
84 imshow( "ellipses", ellipses );