2010-05-26 17:45:32 +02:00
|
|
|
#include <highgui.h>
|
2010-06-11 20:44:22 +02:00
|
|
|
#include "opencv2/core/core.hpp"
|
|
|
|
#include "opencv2/calib3d/calib3d.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2010-06-11 19:17:47 +02:00
|
|
|
#include "opencv2/features2d/features2d.hpp"
|
2010-05-26 17:45:32 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2010-08-03 18:28:52 +02:00
|
|
|
#define DRAW_RICH_KEYPOINTS_MODE 0
|
|
|
|
#define DRAW_OUTLIERS_MODE 0
|
|
|
|
|
2010-07-22 18:30:42 +02:00
|
|
|
void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG& rng )
|
2010-05-26 17:45:32 +02:00
|
|
|
{
|
2010-06-10 14:13:35 +02:00
|
|
|
H.create(3, 3, CV_32FC1);
|
2010-07-22 18:30:42 +02: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 17:45:32 +02:00
|
|
|
|
|
|
|
warpPerspective( src, dst, H, src.size() );
|
|
|
|
}
|
|
|
|
|
|
|
|
const string winName = "correspondences";
|
|
|
|
|
2010-06-10 14:13:35 +02:00
|
|
|
void doIteration( const Mat& img1, Mat& img2, bool isWarpPerspective,
|
2010-08-05 15:34:53 +02:00
|
|
|
vector<KeyPoint>& keypoints1, const Mat& descriptors1,
|
2010-06-10 14:13:35 +02:00
|
|
|
Ptr<FeatureDetector>& detector, Ptr<DescriptorExtractor>& descriptorExtractor,
|
|
|
|
Ptr<DescriptorMatcher>& descriptorMatcher,
|
2010-07-22 18:30:42 +02:00
|
|
|
double ransacReprojThreshold, RNG& rng )
|
2010-05-26 17:45:32 +02:00
|
|
|
{
|
|
|
|
assert( !img1.empty() );
|
|
|
|
Mat H12;
|
|
|
|
if( isWarpPerspective )
|
2010-07-22 18:30:42 +02:00
|
|
|
warpPerspectiveRand(img1, img2, H12, rng );
|
2010-05-26 17:45:32 +02:00
|
|
|
else
|
2010-06-10 22:56:40 +02:00
|
|
|
assert( !img2.empty()/* && img2.cols==img1.cols && img2.rows==img1.rows*/ );
|
2010-05-26 17:45:32 +02:00
|
|
|
|
|
|
|
cout << endl << "< Extracting keypoints from second image..." << endl;
|
|
|
|
vector<KeyPoint> keypoints2;
|
|
|
|
detector->detect( img2, keypoints2 );
|
2010-08-05 15:34:53 +02:00
|
|
|
cout << keypoints2.size() << " points" << endl << ">" << endl;
|
|
|
|
|
|
|
|
if( !H12.empty() )
|
|
|
|
{
|
|
|
|
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 17:45:32 +02:00
|
|
|
|
2010-06-10 14:13:35 +02:00
|
|
|
cout << "< Computing descriptors for keypoints from second image..." << endl;
|
|
|
|
Mat descriptors2;
|
|
|
|
descriptorExtractor->compute( img2, keypoints2, descriptors2 );
|
2010-08-05 15:34:53 +02:00
|
|
|
cout << ">" << endl;
|
2010-06-10 14:13:35 +02:00
|
|
|
|
|
|
|
cout << "< Matching descriptors..." << endl;
|
2010-08-24 14:52:29 +02:00
|
|
|
vector<DMatch> matches;
|
|
|
|
descriptorMatcher->match( descriptors1, descriptors2, matches, Mat() );
|
2010-05-26 17:45:32 +02:00
|
|
|
cout << ">" << endl;
|
|
|
|
|
2010-08-05 15:34:53 +02:00
|
|
|
if( !H12.empty() )
|
|
|
|
{
|
|
|
|
cout << "< Evaluate descriptor match..." << endl;
|
|
|
|
vector<Point2f> curve;
|
|
|
|
Ptr<GenericDescriptorMatch> gdm = new VectorDescriptorMatch( descriptorExtractor, descriptorMatcher );
|
|
|
|
evaluateDescriptorMatch( img1, img2, H12, keypoints1, keypoints2, 0, 0, curve, gdm );
|
|
|
|
for( float l_p = 0; l_p < 1 - FLT_EPSILON; l_p+=0.1 )
|
|
|
|
cout << "1-precision = " << l_p << "; recall = " << getRecall( curve, l_p ) << endl;
|
|
|
|
cout << ">" << endl;
|
|
|
|
}
|
|
|
|
|
2010-08-24 14:52:29 +02:00
|
|
|
vector<int> trainIdxs( matches.size() );
|
|
|
|
for( size_t i = 0; i < matches.size(); i++ )
|
|
|
|
trainIdxs[i] = matches[i].indexTrain;
|
|
|
|
|
2010-05-26 17:45:32 +02:00
|
|
|
if( !isWarpPerspective && ransacReprojThreshold >= 0 )
|
|
|
|
{
|
|
|
|
cout << "< Computing homography (RANSAC)..." << endl;
|
2010-06-15 17:19:11 +02:00
|
|
|
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1);
|
2010-08-24 14:52:29 +02:00
|
|
|
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs);
|
2010-05-26 17:45:32 +02:00
|
|
|
H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold );
|
|
|
|
cout << ">" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat drawImg;
|
2010-06-15 17:19:11 +02:00
|
|
|
if( !H12.empty() ) // filter outliers
|
2010-06-11 19:17:47 +02:00
|
|
|
{
|
2010-06-12 12:11:57 +02:00
|
|
|
vector<char> matchesMask( matches.size(), 0 );
|
2010-06-15 17:19:11 +02:00
|
|
|
vector<Point2f> points1; KeyPoint::convert(keypoints1, points1);
|
2010-08-24 14:52:29 +02:00
|
|
|
vector<Point2f> points2; KeyPoint::convert(keypoints2, points2, trainIdxs);
|
2010-06-15 17:19:11 +02:00
|
|
|
Mat points1t; perspectiveTransform(Mat(points1), points1t, H12);
|
|
|
|
for( size_t i1 = 0; i1 < points1.size(); i1++ )
|
2010-06-11 19:17:47 +02:00
|
|
|
{
|
2010-06-15 17:19:11 +02:00
|
|
|
if( norm(points2[i1] - points1t.at<Point2f>(i1,0)) < 4 ) // inlier
|
2010-06-12 12:11:57 +02:00
|
|
|
matchesMask[i1] = 1;
|
2010-06-11 19:17:47 +02:00
|
|
|
}
|
|
|
|
// draw inliers
|
2010-08-03 18:28:52 +02:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask
|
|
|
|
#if DRAW_RICH_KEYPOINTS_MODE
|
|
|
|
, DrawMatchesFlags::DRAW_RICH_KEYPOINTS
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
|
|
|
|
#if DRAW_OUTLIERS_MODE
|
|
|
|
// draw outliers
|
2010-06-15 17:19:11 +02:00
|
|
|
for( size_t i1 = 0; i1 < matchesMask.size(); i1++ )
|
2010-06-12 12:11:57 +02:00
|
|
|
matchesMask[i1] = !matchesMask[i1];
|
2010-06-12 16:39:56 +02:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0), matchesMask,
|
2010-08-03 18:28:52 +02:00
|
|
|
DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
|
2010-06-15 17:19:11 +02:00
|
|
|
#endif
|
2010-06-11 19:17:47 +02:00
|
|
|
}
|
|
|
|
else
|
2010-06-15 17:19:11 +02:00
|
|
|
drawMatches( img1, keypoints1, img2, keypoints2, matches, drawImg );
|
2010-06-11 19:17:47 +02:00
|
|
|
|
2010-05-26 17:45:32 +02:00
|
|
|
imshow( winName, drawImg );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if( argc != 4 && argc != 6 )
|
|
|
|
{
|
|
|
|
cout << "Format:" << endl;
|
|
|
|
cout << "case1: second image is obtained from the first (given) image using random generated homography matrix" << endl;
|
|
|
|
cout << argv[0] << " [detectorType] [descriptorType] [image1]" << endl;
|
|
|
|
cout << "case2: both images are given. If ransacReprojThreshold>=0 then homography matrix are calculated" << endl;
|
|
|
|
cout << argv[0] << " [detectorType] [descriptorType] [image1] [image2] [ransacReprojThreshold]" << endl;
|
|
|
|
cout << endl << "Mathes are filtered using homography matrix in case1 and case2 (if ransacReprojThreshold>=0)" << endl;
|
2010-06-10 14:13:35 +02:00
|
|
|
return -1;
|
2010-05-26 17:45:32 +02:00
|
|
|
}
|
|
|
|
bool isWarpPerspective = argc == 4;
|
|
|
|
double ransacReprojThreshold = -1;
|
|
|
|
if( !isWarpPerspective )
|
|
|
|
ransacReprojThreshold = atof(argv[5]);
|
|
|
|
|
2010-06-10 14:13:35 +02:00
|
|
|
cout << "< Creating detector, descriptor extractor and descriptor matcher ..." << endl;
|
2010-09-17 13:26:58 +02:00
|
|
|
Ptr<FeatureDetector> detector = createFeatureDetector( argv[1] );
|
2010-06-10 14:13:35 +02:00
|
|
|
Ptr<DescriptorExtractor> descriptorExtractor = createDescriptorExtractor( argv[2] );
|
|
|
|
Ptr<DescriptorMatcher> descriptorMatcher = createDescriptorMatcher( "BruteForce" );
|
2010-05-26 17:45:32 +02:00
|
|
|
cout << ">" << endl;
|
2010-06-10 14:13:35 +02:00
|
|
|
if( detector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() )
|
2010-05-26 17:45:32 +02:00
|
|
|
{
|
2010-06-10 14:13:35 +02:00
|
|
|
cout << "Can not create detector or descriptor exstractor or descriptor matcher of given types" << endl;
|
|
|
|
return -1;
|
2010-05-26 17:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << "< Reading the images..." << endl;
|
|
|
|
Mat img1 = imread( argv[3], CV_LOAD_IMAGE_GRAYSCALE), img2;
|
|
|
|
if( !isWarpPerspective )
|
|
|
|
img2 = imread( argv[4], CV_LOAD_IMAGE_GRAYSCALE);
|
|
|
|
cout << ">" << endl;
|
|
|
|
if( img1.empty() || (!isWarpPerspective && img2.empty()) )
|
|
|
|
{
|
|
|
|
cout << "Can not read images" << endl;
|
2010-06-10 14:13:35 +02:00
|
|
|
return -1;
|
2010-05-26 17:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cout << endl << "< Extracting keypoints from first image..." << endl;
|
|
|
|
vector<KeyPoint> keypoints1;
|
|
|
|
detector->detect( img1, keypoints1 );
|
2010-08-05 15:34:53 +02:00
|
|
|
cout << keypoints1.size() << " points" << endl << ">" << endl;
|
2010-05-26 17:45:32 +02:00
|
|
|
|
2010-06-10 14:13:35 +02:00
|
|
|
cout << "< Computing descriptors for keypoints from first image..." << endl;
|
|
|
|
Mat descriptors1;
|
|
|
|
descriptorExtractor->compute( img1, keypoints1, descriptors1 );
|
2010-08-05 15:34:53 +02:00
|
|
|
cout << ">" << endl;
|
2010-06-10 14:13:35 +02:00
|
|
|
|
2010-05-26 17:45:32 +02:00
|
|
|
namedWindow(winName, 1);
|
2010-08-05 15:34:53 +02:00
|
|
|
RNG rng = theRNG();
|
2010-06-10 14:13:35 +02:00
|
|
|
doIteration( img1, img2, isWarpPerspective, keypoints1, descriptors1,
|
|
|
|
detector, descriptorExtractor, descriptorMatcher,
|
2010-07-22 18:30:42 +02:00
|
|
|
ransacReprojThreshold, rng );
|
2010-05-26 17:45:32 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
char c = (char)cvWaitKey(0);
|
|
|
|
if( c == '\x1b' ) // esc
|
|
|
|
{
|
|
|
|
cout << "Exiting ..." << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if( isWarpPerspective )
|
|
|
|
{
|
2010-06-10 14:13:35 +02:00
|
|
|
doIteration( img1, img2, isWarpPerspective, keypoints1, descriptors1,
|
|
|
|
detector, descriptorExtractor, descriptorMatcher,
|
2010-07-22 18:30:42 +02:00
|
|
|
ransacReprojThreshold, rng );
|
2010-05-26 17:45:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
waitKey(0);
|
2010-06-10 14:13:35 +02:00
|
|
|
return 0;
|
2010-05-26 17:45:32 +02:00
|
|
|
}
|