2010-11-17 00:34:27 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/core/core.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/features2d/features2d.hpp"
|
2012-03-15 22:36:01 +08:00
|
|
|
#include "opencv2/nonfree/nonfree.hpp"
|
|
|
|
#include "opencv2/legacy/legacy.hpp"
|
2010-11-17 00:34:27 +08:00
|
|
|
|
2010-07-26 16:58:46 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-07-26 16:58:46 +08:00
|
|
|
using namespace std;
|
|
|
|
using namespace cv;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2010-12-02 17:46:50 +08:00
|
|
|
{
|
2011-06-09 20:01:47 +08:00
|
|
|
cout << "This program shows the use of the Calonder point descriptor classifier"
|
2011-06-17 14:31:54 +08:00
|
|
|
"SURF is used to detect interest points, Calonder is used to describe/match these points\n"
|
|
|
|
"Format:" << endl <<
|
2011-06-09 20:01:47 +08:00
|
|
|
" classifier_file(to write) test_image file_with_train_images_filenames(txt)" <<
|
|
|
|
" or" << endl <<
|
2011-06-17 14:31:54 +08:00
|
|
|
" classifier_file(to read) test_image" << "\n" << endl <<
|
|
|
|
"Using OpenCV version " << CV_VERSION << "\n" << endl;
|
|
|
|
|
|
|
|
return;
|
2010-12-02 17:46:50 +08:00
|
|
|
}
|
2011-06-17 14:31:54 +08:00
|
|
|
|
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
/*
|
|
|
|
* Generates random perspective transform of image
|
|
|
|
*/
|
2012-06-08 01:21:29 +08:00
|
|
|
static void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG& rng )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-07-26 16:58:46 +08:00
|
|
|
H.create(3, 3, CV_32FC1);
|
|
|
|
H.at<float>(0,0) = rng.uniform( 0.8f, 1.2f);
|
|
|
|
H.at<float>(0,1) = rng.uniform(-0.1f, 0.1f);
|
|
|
|
H.at<float>(0,2) = rng.uniform(-0.1f, 0.1f)*src.cols;
|
|
|
|
H.at<float>(1,0) = rng.uniform(-0.1f, 0.1f);
|
|
|
|
H.at<float>(1,1) = rng.uniform( 0.8f, 1.2f);
|
|
|
|
H.at<float>(1,2) = rng.uniform(-0.1f, 0.1f)*src.rows;
|
|
|
|
H.at<float>(2,0) = rng.uniform( -1e-4f, 1e-4f);
|
|
|
|
H.at<float>(2,1) = rng.uniform( -1e-4f, 1e-4f);
|
|
|
|
H.at<float>(2,2) = rng.uniform( 0.8f, 1.2f);
|
|
|
|
|
|
|
|
warpPerspective( src, dst, H, src.size() );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
/*
|
|
|
|
* Trains Calonder classifier and writes trained classifier in file:
|
|
|
|
* imgFilename - name of .txt file which contains list of full filenames of train images,
|
|
|
|
* classifierFilename - name of binary file in which classifier will be written.
|
|
|
|
*
|
|
|
|
* To train Calonder classifier RTreeClassifier class need to be used.
|
|
|
|
*/
|
2012-06-08 01:21:29 +08:00
|
|
|
static void trainCalonderClassifier( const string& classifierFilename, const string& imgFilename )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-07-29 19:14:54 +08:00
|
|
|
// Reads train images
|
|
|
|
ifstream is( imgFilename.c_str(), ifstream::in );
|
|
|
|
vector<Mat> trainImgs;
|
|
|
|
while( !is.eof() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-07-29 19:14:54 +08:00
|
|
|
string str;
|
|
|
|
getline( is, str );
|
|
|
|
if (str.empty()) break;
|
|
|
|
Mat img = imread( str, CV_LOAD_IMAGE_GRAYSCALE );
|
|
|
|
if( !img.empty() )
|
|
|
|
trainImgs.push_back( img );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-07-29 19:14:54 +08:00
|
|
|
if( trainImgs.empty() )
|
|
|
|
{
|
|
|
|
cout << "All train images can not be read." << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
cout << trainImgs.size() << " train images were read." << endl;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
// Extracts keypoints from train images
|
|
|
|
SurfFeatureDetector detector;
|
|
|
|
vector<BaseKeypoint> trainPoints;
|
|
|
|
vector<IplImage> iplTrainImgs(trainImgs.size());
|
|
|
|
for( size_t imgIdx = 0; imgIdx < trainImgs.size(); imgIdx++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-07-29 19:14:54 +08:00
|
|
|
iplTrainImgs[imgIdx] = trainImgs[imgIdx];
|
|
|
|
vector<KeyPoint> kps; detector.detect( trainImgs[imgIdx], kps );
|
2010-07-26 16:58:46 +08:00
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
for( size_t pointIdx = 0; pointIdx < kps.size(); pointIdx++ )
|
2010-07-26 16:58:46 +08:00
|
|
|
{
|
2010-07-29 19:14:54 +08:00
|
|
|
Point2f p = kps[pointIdx].pt;
|
|
|
|
trainPoints.push_back( BaseKeypoint(cvRound(p.x), cvRound(p.y), &iplTrainImgs[imgIdx]) );
|
2010-07-26 16:58:46 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
// Trains Calonder classifier on extracted points
|
|
|
|
RTreeClassifier classifier;
|
|
|
|
classifier.train( trainPoints, theRNG(), 48, 9, 100 );
|
|
|
|
// Writes classifier
|
|
|
|
classifier.write( classifierFilename.c_str() );
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
/*
|
|
|
|
* Test Calonder classifier to match keypoints on given image:
|
|
|
|
* classifierFilename - name of file from which classifier will be read,
|
|
|
|
* imgFilename - test image filename.
|
|
|
|
*
|
|
|
|
* To calculate keypoint descriptors you may use RTreeClassifier class (as to train),
|
|
|
|
* but it is convenient to use CalonderDescriptorExtractor class which is wrapper of
|
|
|
|
* RTreeClassifier.
|
|
|
|
*/
|
2012-06-08 01:21:29 +08:00
|
|
|
static void testCalonderClassifier( const string& classifierFilename, const string& imgFilename )
|
2010-07-29 19:14:54 +08:00
|
|
|
{
|
|
|
|
Mat img1 = imread( imgFilename, CV_LOAD_IMAGE_GRAYSCALE ), img2, H12;
|
|
|
|
if( img1.empty() )
|
2010-07-26 16:58:46 +08:00
|
|
|
{
|
|
|
|
cout << "Test image can not be read." << endl;
|
2010-07-29 19:14:54 +08:00
|
|
|
exit(-1);
|
2010-07-26 16:58:46 +08:00
|
|
|
}
|
2010-07-29 19:14:54 +08:00
|
|
|
warpPerspectiveRand( img1, img2, H12, theRNG() );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-07-26 16:58:46 +08:00
|
|
|
// Exstract keypoints from test images
|
|
|
|
SurfFeatureDetector detector;
|
2010-07-29 19:14:54 +08:00
|
|
|
vector<KeyPoint> keypoints1; detector.detect( img1, keypoints1 );
|
|
|
|
vector<KeyPoint> keypoints2; detector.detect( img2, keypoints2 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-07-29 19:14:54 +08:00
|
|
|
// Compute descriptors
|
|
|
|
CalonderDescriptorExtractor<float> de( classifierFilename );
|
|
|
|
Mat descriptors1; de.compute( img1, keypoints1, descriptors1 );
|
|
|
|
Mat descriptors2; de.compute( img2, keypoints2, descriptors2 );
|
2010-07-26 16:58:46 +08:00
|
|
|
|
|
|
|
// Match descriptors
|
2012-03-15 22:36:01 +08:00
|
|
|
BFMatcher matcher(NORM_L1);
|
2010-10-29 16:44:42 +08:00
|
|
|
vector<DMatch> matches;
|
|
|
|
matcher.match( descriptors1, descriptors2, matches );
|
2010-07-26 16:58:46 +08:00
|
|
|
|
|
|
|
// Prepare inlier mask
|
|
|
|
vector<char> matchesMask( matches.size(), 0 );
|
2010-07-29 19:14:54 +08:00
|
|
|
vector<Point2f> points1; KeyPoint::convert( keypoints1, points1 );
|
|
|
|
vector<Point2f> points2; KeyPoint::convert( keypoints2, points2 );
|
|
|
|
Mat points1t; perspectiveTransform(Mat(points1), points1t, H12);
|
2010-07-26 16:58:46 +08:00
|
|
|
for( size_t mi = 0; mi < matches.size(); mi++ )
|
|
|
|
{
|
2011-06-17 14:31:54 +08:00
|
|
|
if( norm(points2[matches[mi].trainIdx] - points1t.at<Point2f>((int)mi,0)) < 4 ) // inlier
|
2010-07-26 16:58:46 +08:00
|
|
|
matchesMask[mi] = 1;
|
|
|
|
}
|
2010-07-29 19:14:54 +08:00
|
|
|
|
2010-07-26 16:58:46 +08:00
|
|
|
// Draw
|
|
|
|
Mat drawImg;
|
2010-07-29 19:14:54 +08:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask );
|
2010-07-26 16:58:46 +08:00
|
|
|
string winName = "Matches";
|
|
|
|
namedWindow( winName, WINDOW_AUTOSIZE );
|
|
|
|
imshow( winName, drawImg );
|
|
|
|
waitKey();
|
2010-07-29 19:14:54 +08:00
|
|
|
}
|
|
|
|
|
2011-06-09 20:01:47 +08:00
|
|
|
int main( int argc, char **argv )
|
2010-07-29 19:14:54 +08:00
|
|
|
{
|
2011-06-09 20:01:47 +08:00
|
|
|
if( argc != 4 && argc != 3 )
|
2010-07-29 19:14:54 +08:00
|
|
|
{
|
2011-06-17 14:31:54 +08:00
|
|
|
help();
|
2010-07-29 19:14:54 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-06-09 20:01:47 +08:00
|
|
|
if( argc == 4 )
|
|
|
|
trainCalonderClassifier( argv[1], argv[3] );
|
2010-07-29 19:14:54 +08:00
|
|
|
|
2011-06-09 20:01:47 +08:00
|
|
|
testCalonderClassifier( argv[1], argv[2] );
|
2010-07-29 19:14:54 +08:00
|
|
|
|
2010-07-27 20:26:52 +08:00
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|