2010-11-17 00:01:56 +08:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-06-12 02:44:22 +08:00
|
|
|
#include "opencv2/calib3d/calib3d.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2010-06-12 01:17:47 +08:00
|
|
|
#include "opencv2/features2d/features2d.hpp"
|
2012-05-30 16:04:37 +08:00
|
|
|
#include "opencv2/nonfree/nonfree.hpp"
|
2010-11-17 00:01:56 +08:00
|
|
|
|
2010-05-26 23:45:32 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static void help(char** argv)
|
2010-11-25 10:41:22 +08:00
|
|
|
{
|
2010-12-04 16:30:07 +08:00
|
|
|
cout << "\nThis program demonstrats keypoint finding and matching between 2 images using features2d framework.\n"
|
2010-11-25 10:41:22 +08:00
|
|
|
<< " In one case, the 2nd image is synthesized by homography from the first, in the second case, there are 2 images\n"
|
|
|
|
<< "\n"
|
2011-05-20 20:14:35 +08:00
|
|
|
<< "Case1: second image is obtained from the first (given) image using random generated homography matrix\n"
|
2010-11-25 10:41:22 +08:00
|
|
|
<< argv[0] << " [detectorType] [descriptorType] [matcherType] [matcherFilterType] [image] [evaluate(0 or 1)]\n"
|
|
|
|
<< "Example of case1:\n"
|
|
|
|
<< "./descriptor_extractor_matcher SURF SURF FlannBased NoneFilter cola.jpg 0\n"
|
|
|
|
<< "\n"
|
2011-05-20 20:14:35 +08:00
|
|
|
<< "Case2: both images are given. If ransacReprojThreshold>=0 then homography matrix are calculated\n"
|
2010-11-25 10:41:22 +08:00
|
|
|
<< argv[0] << " [detectorType] [descriptorType] [matcherType] [matcherFilterType] [image1] [image2] [ransacReprojThreshold]\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "Matches are filtered using homography matrix in case1 and case2 (if ransacReprojThreshold>=0)\n"
|
2011-05-20 20:14:35 +08:00
|
|
|
<< "Example of case2:\n"
|
2010-11-25 10:41:22 +08:00
|
|
|
<< "./descriptor_extractor_matcher SURF SURF BruteForce CrossCheckFilter cola1.jpg cola2.jpg 3\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "Possible detectorType values: see in documentation on createFeatureDetector().\n"
|
|
|
|
<< "Possible descriptorType values: see in documentation on createDescriptorExtractor().\n"
|
|
|
|
<< "Possible matcherType values: see in documentation on createDescriptorMatcher().\n"
|
|
|
|
<< "Possible matcherFilterType values: NoneFilter, CrossCheckFilter." << endl;
|
|
|
|
}
|
|
|
|
|
2010-08-04 00:28:52 +08:00
|
|
|
#define DRAW_RICH_KEYPOINTS_MODE 0
|
|
|
|
#define DRAW_OUTLIERS_MODE 0
|
|
|
|
|
2010-10-29 21:13:46 +08:00
|
|
|
const string winName = "correspondences";
|
|
|
|
|
|
|
|
enum { NONE_FILTER = 0, CROSS_CHECK_FILTER = 1 };
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static int getMatcherFilterType( const string& str )
|
2010-10-29 21:13:46 +08:00
|
|
|
{
|
|
|
|
if( str == "NoneFilter" )
|
|
|
|
return NONE_FILTER;
|
|
|
|
if( str == "CrossCheckFilter" )
|
|
|
|
return CROSS_CHECK_FILTER;
|
2010-11-26 00:55:46 +08:00
|
|
|
CV_Error(CV_StsBadArg, "Invalid filter name");
|
2010-10-29 21:13:46 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static void simpleMatching( Ptr<DescriptorMatcher>& descriptorMatcher,
|
2010-10-29 21:13:46 +08:00
|
|
|
const Mat& descriptors1, const Mat& descriptors2,
|
|
|
|
vector<DMatch>& matches12 )
|
|
|
|
{
|
|
|
|
vector<DMatch> matches;
|
|
|
|
descriptorMatcher->match( descriptors1, descriptors2, matches12 );
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static void crossCheckMatching( Ptr<DescriptorMatcher>& descriptorMatcher,
|
2010-10-29 21:13:46 +08:00
|
|
|
const Mat& descriptors1, const Mat& descriptors2,
|
|
|
|
vector<DMatch>& filteredMatches12, int knn=1 )
|
|
|
|
{
|
|
|
|
filteredMatches12.clear();
|
|
|
|
vector<vector<DMatch> > matches12, matches21;
|
|
|
|
descriptorMatcher->knnMatch( descriptors1, descriptors2, matches12, knn );
|
|
|
|
descriptorMatcher->knnMatch( descriptors2, descriptors1, matches21, knn );
|
|
|
|
for( size_t m = 0; m < matches12.size(); m++ )
|
|
|
|
{
|
|
|
|
bool findCrossCheck = false;
|
|
|
|
for( size_t fk = 0; fk < matches12[m].size(); fk++ )
|
|
|
|
{
|
|
|
|
DMatch forward = matches12[m][fk];
|
|
|
|
|
|
|
|
for( size_t bk = 0; bk < matches21[forward.trainIdx].size(); bk++ )
|
|
|
|
{
|
|
|
|
DMatch backward = matches21[forward.trainIdx][bk];
|
|
|
|
if( backward.trainIdx == forward.queryIdx )
|
|
|
|
{
|
|
|
|
filteredMatches12.push_back(forward);
|
|
|
|
findCrossCheck = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( findCrossCheck ) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG& rng )
|
2010-05-26 23:45:32 +08:00
|
|
|
{
|
2010-06-10 20:13:35 +08:00
|
|
|
H.create(3, 3, CV_32FC1);
|
2010-07-23 00:30:42 +08:00
|
|
|
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);
|
2010-05-26 23:45:32 +08:00
|
|
|
|
|
|
|
warpPerspective( src, dst, H, src.size() );
|
|
|
|
}
|
|
|
|
|
2012-06-15 21:04:17 +08:00
|
|
|
static void doIteration( const Mat& img1, Mat& img2, bool isWarpPerspective,
|
2010-08-05 21:34:53 +08:00
|
|
|
vector<KeyPoint>& keypoints1, const Mat& descriptors1,
|
2010-06-10 20:13:35 +08:00
|
|
|
Ptr<FeatureDetector>& detector, Ptr<DescriptorExtractor>& descriptorExtractor,
|
2010-10-29 21:13:46 +08:00
|
|
|
Ptr<DescriptorMatcher>& descriptorMatcher, int matcherFilter, bool eval,
|
2010-07-23 00:30:42 +08:00
|
|
|
double ransacReprojThreshold, RNG& rng )
|
2010-05-26 23:45:32 +08:00
|
|
|
{
|
|
|
|
assert( !img1.empty() );
|
|
|
|
Mat H12;
|
|
|
|
if( isWarpPerspective )
|
2010-07-23 00:30:42 +08:00
|
|
|
warpPerspectiveRand(img1, img2, H12, rng );
|
2010-05-26 23:45:32 +08:00
|
|
|
else
|
2010-06-11 04:56:40 +08:00
|
|
|
assert( !img2.empty()/* && img2.cols==img1.cols && img2.rows==img1.rows*/ );
|
2010-05-26 23:45:32 +08:00
|
|
|
|
|
|
|
cout << endl << "< Extracting keypoints from second image..." << endl;
|
|
|
|
vector<KeyPoint> keypoints2;
|
|
|
|
detector->detect( img2, keypoints2 );
|
2010-08-05 21:34:53 +08:00
|
|
|
cout << keypoints2.size() << " points" << endl << ">" << endl;
|
|
|
|
|
2010-10-29 21:13:46 +08:00
|
|
|
if( !H12.empty() && eval )
|
2010-08-05 21:34:53 +08:00
|
|
|
{
|
|
|
|
cout << "< Evaluate feature detector..." << endl;
|
|
|
|
float repeatability;
|
|
|
|
int correspCount;
|
|
|
|
evaluateFeatureDetector( img1, img2, H12, &keypoints1, &keypoints2, repeatability, correspCount );
|
|
|
|
cout << "repeatability = " << repeatability << endl;
|
|
|
|
cout << "correspCount = " << correspCount << endl;
|
|
|
|
cout << ">" << endl;
|
|
|
|
}
|
2010-05-26 23:45:32 +08:00
|
|
|
|
2010-06-10 20:13:35 +08:00
|
|
|
cout << "< Computing descriptors for keypoints from second image..." << endl;
|
|
|
|
Mat descriptors2;
|
|
|
|
descriptorExtractor->compute( img2, keypoints2, descriptors2 );
|
2010-08-05 21:34:53 +08:00
|
|
|
cout << ">" << endl;
|
2010-06-10 20:13:35 +08:00
|
|
|
|
|
|
|
cout << "< Matching descriptors..." << endl;
|
2010-10-29 21:13:46 +08:00
|
|
|
vector<DMatch> filteredMatches;
|
|
|
|
switch( matcherFilter )
|
|
|
|
{
|
|
|
|
case CROSS_CHECK_FILTER :
|
|
|
|
crossCheckMatching( descriptorMatcher, descriptors1, descriptors2, filteredMatches, 1 );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
simpleMatching( descriptorMatcher, descriptors1, descriptors2, filteredMatches );
|
|
|
|
}
|
2010-05-26 23:45:32 +08:00
|
|
|
cout << ">" << endl;
|
|
|
|
|
2010-10-29 21:13:46 +08:00
|
|
|
if( !H12.empty() && eval )
|
2010-08-05 21:34:53 +08:00
|
|
|
{
|
2011-05-20 20:14:35 +08:00
|
|
|
cout << "< Evaluate descriptor matcher..." << endl;
|
2010-08-05 21:34:53 +08:00
|
|
|
vector<Point2f> curve;
|
2010-10-29 16:44:42 +08:00
|
|
|
Ptr<GenericDescriptorMatcher> gdm = new VectorDescriptorMatcher( descriptorExtractor, descriptorMatcher );
|
2010-09-24 00:17:48 +08:00
|
|
|
evaluateGenericDescriptorMatcher( img1, img2, H12, keypoints1, keypoints2, 0, 0, curve, gdm );
|
2011-05-20 20:14:35 +08:00
|
|
|
|
2011-05-20 20:46:05 +08:00
|
|
|
Point2f firstPoint = *curve.begin();
|
|
|
|
Point2f lastPoint = *curve.rbegin();
|
|
|
|
int prevPointIndex = -1;
|
|
|
|
cout << "1-precision = " << firstPoint.x << "; recall = " << firstPoint.y << endl;
|
2011-05-20 20:30:47 +08:00
|
|
|
for( float l_p = 0; l_p <= 1 + FLT_EPSILON; l_p+=0.05f )
|
2011-05-20 20:14:35 +08:00
|
|
|
{
|
|
|
|
int nearest = getNearestPoint( curve, l_p );
|
2011-05-20 20:30:47 +08:00
|
|
|
if( nearest >= 0 )
|
2011-05-20 20:46:05 +08:00
|
|
|
{
|
|
|
|
Point2f curPoint = curve[nearest];
|
|
|
|
if( curPoint.x > firstPoint.x && curPoint.x < lastPoint.x && nearest != prevPointIndex )
|
|
|
|
{
|
|
|
|
cout << "1-precision = " << curPoint.x << "; recall = " << curPoint.y << endl;
|
|
|
|
prevPointIndex = nearest;
|
|
|
|
}
|
|
|
|
}
|
2011-05-20 20:14:35 +08:00
|
|
|
}
|
2011-05-20 20:46:05 +08:00
|
|
|
cout << "1-precision = " << lastPoint.x << "; recall = " << lastPoint.y << endl;
|
2010-08-05 21:34:53 +08:00
|
|
|
cout << ">" << endl;
|
|
|
|
}
|
|
|
|
|
2010-10-29 21:13:46 +08:00
|
|
|
vector<int> queryIdxs( filteredMatches.size() ), trainIdxs( filteredMatches.size() );
|
|
|
|
for( size_t i = 0; i < filteredMatches.size(); i++ )
|
|
|
|
{
|
|
|
|
queryIdxs[i] = filteredMatches[i].queryIdx;
|
|
|
|
trainIdxs[i] = filteredMatches[i].trainIdx;
|
|
|
|
}
|
2010-08-24 20:52:29 +08:00
|
|
|
|
2010-05-26 23:45:32 +08:00
|
|
|
if( !isWarpPerspective && ransacReprojThreshold >= 0 )
|
|
|
|
{
|
|
|
|
cout << "< Computing homography (RANSAC)..." << endl;
|
2010-10-29 21:13:46 +08:00
|
|
|
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1, queryIdxs);
|
2010-08-24 20:52:29 +08:00
|
|
|
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs);
|
2010-05-26 23:45:32 +08:00
|
|
|
H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold );
|
|
|
|
cout << ">" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat drawImg;
|
2010-06-15 23:19:11 +08:00
|
|
|
if( !H12.empty() ) // filter outliers
|
2010-06-12 01:17:47 +08:00
|
|
|
{
|
2010-10-29 21:13:46 +08:00
|
|
|
vector<char> matchesMask( filteredMatches.size(), 0 );
|
|
|
|
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1, queryIdxs);
|
2010-08-24 20:52:29 +08:00
|
|
|
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs);
|
2010-06-15 23:19:11 +08:00
|
|
|
Mat points1t; perspectiveTransform(Mat(points1), points1t, H12);
|
2011-05-31 00:20:17 +08:00
|
|
|
|
|
|
|
double maxInlierDist = ransacReprojThreshold < 0 ? 3 : ransacReprojThreshold;
|
2010-06-15 23:19:11 +08:00
|
|
|
for( size_t i1 = 0; i1 < points1.size(); i1++ )
|
2010-06-12 01:17:47 +08:00
|
|
|
{
|
2011-05-31 00:20:17 +08:00
|
|
|
if( norm(points2[i1] - points1t.at<Point2f>((int)i1,0)) <= maxInlierDist ) // inlier
|
2010-06-12 18:11:57 +08:00
|
|
|
matchesMask[i1] = 1;
|
2010-06-12 01:17:47 +08:00
|
|
|
}
|
|
|
|
// draw inliers
|
2010-10-29 21:13:46 +08:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, filteredMatches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask
|
2010-08-04 00:28:52 +08:00
|
|
|
#if DRAW_RICH_KEYPOINTS_MODE
|
|
|
|
, DrawMatchesFlags::DRAW_RICH_KEYPOINTS
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
#if DRAW_OUTLIERS_MODE
|
|
|
|
// draw outliers
|
2010-06-15 23:19:11 +08:00
|
|
|
for( size_t i1 = 0; i1 < matchesMask.size(); i1++ )
|
2010-06-12 18:11:57 +08:00
|
|
|
matchesMask[i1] = !matchesMask[i1];
|
2010-10-29 21:13:46 +08:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, filteredMatches, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0), matchesMask,
|
2010-08-04 00:28:52 +08:00
|
|
|
DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
|
2010-06-15 23:19:11 +08:00
|
|
|
#endif
|
2010-06-12 01:17:47 +08:00
|
|
|
}
|
|
|
|
else
|
2010-10-29 21:13:46 +08:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, filteredMatches, drawImg );
|
2010-06-12 01:17:47 +08:00
|
|
|
|
2010-05-26 23:45:32 +08:00
|
|
|
imshow( winName, drawImg );
|
|
|
|
}
|
|
|
|
|
2010-11-25 10:41:22 +08:00
|
|
|
|
2010-05-26 23:45:32 +08:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2010-10-29 21:13:46 +08:00
|
|
|
if( argc != 7 && argc != 8 )
|
2010-05-26 23:45:32 +08:00
|
|
|
{
|
2012-10-17 07:18:30 +08:00
|
|
|
help(argv);
|
2010-06-10 20:13:35 +08:00
|
|
|
return -1;
|
2010-05-26 23:45:32 +08:00
|
|
|
}
|
2012-05-30 16:04:37 +08:00
|
|
|
|
|
|
|
cv::initModule_nonfree();
|
|
|
|
|
2010-10-29 21:13:46 +08:00
|
|
|
bool isWarpPerspective = argc == 7;
|
2010-05-26 23:45:32 +08:00
|
|
|
double ransacReprojThreshold = -1;
|
|
|
|
if( !isWarpPerspective )
|
2010-10-29 21:13:46 +08:00
|
|
|
ransacReprojThreshold = atof(argv[7]);
|
2010-05-26 23:45:32 +08:00
|
|
|
|
2010-06-10 20:13:35 +08:00
|
|
|
cout << "< Creating detector, descriptor extractor and descriptor matcher ..." << endl;
|
2010-11-25 23:59:37 +08:00
|
|
|
Ptr<FeatureDetector> detector = FeatureDetector::create( argv[1] );
|
|
|
|
Ptr<DescriptorExtractor> descriptorExtractor = DescriptorExtractor::create( argv[2] );
|
|
|
|
Ptr<DescriptorMatcher> descriptorMatcher = DescriptorMatcher::create( argv[3] );
|
2010-10-29 21:13:46 +08:00
|
|
|
int mactherFilterType = getMatcherFilterType( argv[4] );
|
|
|
|
bool eval = !isWarpPerspective ? false : (atoi(argv[6]) == 0 ? false : true);
|
2010-05-26 23:45:32 +08:00
|
|
|
cout << ">" << endl;
|
2010-06-10 20:13:35 +08:00
|
|
|
if( detector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() )
|
2010-05-26 23:45:32 +08:00
|
|
|
{
|
2010-06-10 20:13:35 +08:00
|
|
|
cout << "Can not create detector or descriptor exstractor or descriptor matcher of given types" << endl;
|
|
|
|
return -1;
|
2010-10-29 21:13:46 +08:00
|
|
|
}
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2010-05-26 23:45:32 +08:00
|
|
|
cout << "< Reading the images..." << endl;
|
2010-10-29 21:13:46 +08:00
|
|
|
Mat img1 = imread( argv[5] ), img2;
|
2010-05-26 23:45:32 +08:00
|
|
|
if( !isWarpPerspective )
|
2010-10-29 21:13:46 +08:00
|
|
|
img2 = imread( argv[6] );
|
2010-05-26 23:45:32 +08:00
|
|
|
cout << ">" << endl;
|
|
|
|
if( img1.empty() || (!isWarpPerspective && img2.empty()) )
|
|
|
|
{
|
|
|
|
cout << "Can not read images" << endl;
|
2010-06-10 20:13:35 +08:00
|
|
|
return -1;
|
2010-05-26 23:45:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << endl << "< Extracting keypoints from first image..." << endl;
|
|
|
|
vector<KeyPoint> keypoints1;
|
|
|
|
detector->detect( img1, keypoints1 );
|
2010-08-05 21:34:53 +08:00
|
|
|
cout << keypoints1.size() << " points" << endl << ">" << endl;
|
2010-05-26 23:45:32 +08:00
|
|
|
|
2010-06-10 20:13:35 +08:00
|
|
|
cout << "< Computing descriptors for keypoints from first image..." << endl;
|
|
|
|
Mat descriptors1;
|
|
|
|
descriptorExtractor->compute( img1, keypoints1, descriptors1 );
|
2010-08-05 21:34:53 +08:00
|
|
|
cout << ">" << endl;
|
2010-06-10 20:13:35 +08:00
|
|
|
|
2010-05-26 23:45:32 +08:00
|
|
|
namedWindow(winName, 1);
|
2010-08-05 21:34:53 +08:00
|
|
|
RNG rng = theRNG();
|
2010-06-10 20:13:35 +08:00
|
|
|
doIteration( img1, img2, isWarpPerspective, keypoints1, descriptors1,
|
2010-10-29 21:13:46 +08:00
|
|
|
detector, descriptorExtractor, descriptorMatcher, mactherFilterType, eval,
|
2010-07-23 00:30:42 +08:00
|
|
|
ransacReprojThreshold, rng );
|
2010-05-26 23:45:32 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
2010-10-29 21:13:46 +08:00
|
|
|
char c = (char)waitKey(0);
|
2010-05-26 23:45:32 +08:00
|
|
|
if( c == '\x1b' ) // esc
|
|
|
|
{
|
|
|
|
cout << "Exiting ..." << endl;
|
2011-05-17 22:15:23 +08:00
|
|
|
break;
|
2010-05-26 23:45:32 +08:00
|
|
|
}
|
|
|
|
else if( isWarpPerspective )
|
|
|
|
{
|
2010-06-10 20:13:35 +08:00
|
|
|
doIteration( img1, img2, isWarpPerspective, keypoints1, descriptors1,
|
2010-10-29 21:13:46 +08:00
|
|
|
detector, descriptorExtractor, descriptorMatcher, mactherFilterType, eval,
|
2010-07-23 00:30:42 +08:00
|
|
|
ransacReprojThreshold, rng );
|
2010-05-26 23:45:32 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-10 20:13:35 +08:00
|
|
|
return 0;
|
2010-05-26 23:45:32 +08:00
|
|
|
}
|