2010-05-11 19:44:00 +02:00
|
|
|
/* This sample code was originally provided by Liu Liu
|
2012-03-15 15:36:01 +01:00
|
|
|
* Copyright (C) 2009, Liu Liu All rights reserved.
|
2010-05-11 19:44:00 +02:00
|
|
|
*/
|
|
|
|
|
2010-11-26 18:59:40 +01:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/features2d/features2d.hpp"
|
2012-03-15 15:36:01 +01:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
#include <iostream>
|
2011-07-14 14:50:07 +02:00
|
|
|
#include <stdio.h>
|
2010-12-03 03:13:16 +01:00
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void help()
|
2010-12-03 03:13:16 +01:00
|
|
|
{
|
2012-06-07 19:21:29 +02:00
|
|
|
cout << "\nThis program demonstrates the Maximal Extremal Region interest point detector.\n"
|
2012-03-15 15:36:01 +01:00
|
|
|
"It finds the most stable (in size) dark and white regions as a threshold is increased.\n"
|
|
|
|
"\nCall:\n"
|
|
|
|
"./mser_sample <path_and_image_filename, Default is 'puzzle.png'>\n\n";
|
2010-12-03 03:13:16 +01:00
|
|
|
}
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static const Vec3b bcolors[] =
|
2010-11-26 18:59:40 +01:00
|
|
|
{
|
2012-03-15 15:36:01 +01:00
|
|
|
Vec3b(0,0,255),
|
|
|
|
Vec3b(0,128,255),
|
|
|
|
Vec3b(0,255,255),
|
|
|
|
Vec3b(0,255,0),
|
|
|
|
Vec3b(255,128,0),
|
|
|
|
Vec3b(255,255,0),
|
|
|
|
Vec3b(255,0,0),
|
|
|
|
Vec3b(255,0,255),
|
|
|
|
Vec3b(255,255,255)
|
2010-11-26 18:59:40 +01:00
|
|
|
};
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
int main( int argc, char** argv )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-06-07 19:21:29 +02:00
|
|
|
string path;
|
|
|
|
Mat img0, img, yuv, gray, ellipses;
|
|
|
|
help();
|
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
img0 = imread( argc != 2 ? "puzzle.png" : argv[1], 1 );
|
|
|
|
if( img0.empty() )
|
|
|
|
{
|
|
|
|
if( argc != 2 )
|
|
|
|
cout << "\nUsage: mser_sample <path_to_image>\n";
|
|
|
|
else
|
|
|
|
cout << "Unable to load image " << argv[1] << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-07 19:21:29 +02:00
|
|
|
|
|
|
|
cvtColor(img0, yuv, COLOR_BGR2YCrCb);
|
2012-03-15 15:36:01 +01:00
|
|
|
cvtColor(img0, gray, COLOR_BGR2GRAY);
|
|
|
|
cvtColor(gray, img, COLOR_GRAY2BGR);
|
|
|
|
img.copyTo(ellipses);
|
2012-06-07 19:21:29 +02:00
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
vector<vector<Point> > contours;
|
2012-06-07 19:21:29 +02:00
|
|
|
double t = (double)getTickCount();
|
2012-03-15 15:36:01 +01:00
|
|
|
MSER()(yuv, contours);
|
2012-06-07 19:21:29 +02:00
|
|
|
t = (double)getTickCount() - t;
|
|
|
|
printf( "MSER extracted %d contours in %g ms.\n", (int)contours.size(),
|
2012-03-15 15:36:01 +01:00
|
|
|
t*1000./getTickFrequency() );
|
2012-06-07 19:21:29 +02:00
|
|
|
|
|
|
|
// draw mser's with different colors
|
|
|
|
for( int i = (int)contours.size()-1; i >= 0; i-- )
|
|
|
|
{
|
|
|
|
const vector<Point>& r = contours[i];
|
|
|
|
for ( int j = 0; j < (int)r.size(); j++ )
|
|
|
|
{
|
|
|
|
Point pt = r[j];
|
2012-03-17 10:22:31 +01:00
|
|
|
img.at<Vec3b>(pt) = bcolors[i%9];
|
2012-06-07 19:21:29 +02:00
|
|
|
}
|
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
// find ellipse (it seems cvfitellipse2 have error or sth?)
|
|
|
|
RotatedRect box = fitEllipse( r );
|
2012-06-07 19:21:29 +02:00
|
|
|
|
2012-03-15 15:36:01 +01:00
|
|
|
box.angle=(float)CV_PI/2-box.angle;
|
|
|
|
ellipse( ellipses, box, Scalar(196,255,255), 2 );
|
2012-06-07 19:21:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
imshow( "original", img0 );
|
|
|
|
imshow( "response", img );
|
|
|
|
imshow( "ellipses", ellipses );
|
|
|
|
|
|
|
|
waitKey(0);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|