fixed many warnings from GCC 4.6.1
[profile/ivi/opencv.git] / samples / c / mser_sample.cpp
1 /* This sample code was originally provided by Liu Liu
2  * Copyright (C) 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.hpp"
8
9 #include <iostream>
10 #include <stdio.h>
11
12 using namespace cv;
13 using namespace std;
14
15 void help()
16 {
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"
19     "\nCall:\n"
20     "./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n";
21 }
22
23 static const Vec3b bcolors[] = 
24 {
25     Vec3b(0,0,255),
26     Vec3b(0,128,255),
27     Vec3b(0,255,255),
28     Vec3b(0,255,0),
29     Vec3b(255,128,0),
30     Vec3b(255,255,0),
31     Vec3b(255,0,0),
32     Vec3b(255,0,255),
33     Vec3b(255,255,255)
34 };
35
36 int main( int argc, char** argv )
37 {
38         string path;
39         Mat img0, img, yuv, gray, ellipses;
40         help();
41     
42     img0 = imread( argc != 2 ? "puzzle.png" : argv[1], 1 );
43     if( img0.empty() )
44     {
45         if( argc != 2 )
46             cout << "\nUsage: mser_sample <path_to_image>\n";
47         else
48             cout << "Unable to load image " << argv[1] << endl;
49         return 0;
50     }
51     
52         cvtColor(img0, yuv, COLOR_BGR2YCrCb);
53     cvtColor(img0, gray, COLOR_BGR2GRAY);
54     cvtColor(gray, img, COLOR_GRAY2BGR);
55     img.copyTo(ellipses);
56     
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() );
63     
64         // draw mser's with different colors
65         for( int i = (int)contours.size()-1; i >= 0; i-- )
66         {
67                 const vector<Point>& r = contours[i];
68                 for ( int j = 0; j < (int)r.size(); j++ )
69                 {
70                         Point pt = r[j];
71             img.at<Vec3b>(pt) = bcolors[i%9];
72                 }
73         
74         // find ellipse (it seems cvfitellipse2 have error or sth?)
75         RotatedRect box = fitEllipse( r );
76         
77         box.angle=(float)CV_PI/2-box.angle;
78         ellipse( ellipses, box, Scalar(196,255,255), 2 );
79         }
80     
81         imshow( "original", img0 );
82         imshow( "response", img );
83         imshow( "ellipses", ellipses );
84     
85         waitKey(0);
86 }