opencv/samples/cpp/descriptor_extractor_matcher.cpp

262 lines
9.7 KiB
C++
Raw Normal View History

#include <cvaux.h>
#include <highgui.h>
#include "opencv2/features2d/features2d.hpp"
#include <iostream>
using namespace cv;
using namespace std;
inline Point2f applyHomography( const Mat_<double>& H, const Point2f& pt )
{
double z = H(2,0)*pt.x + H(2,1)*pt.y + H(2,2);
if( z )
{
double w = 1./z;
return Point2f( (H(0,0)*pt.x + H(0,1)*pt.y + H(0,2))*w, (H(1,0)*pt.x + H(1,1)*pt.y + H(1,2))*w );
}
return Point2f( numeric_limits<double>::max(), numeric_limits<double>::max() );
}
2010-06-10 20:13:35 +08:00
void warpPerspectiveRand( const Mat& src, Mat& dst, Mat& H, RNG* rng )
{
2010-06-10 20:13:35 +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);
2010-06-10 20:13:35 +08:00
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);
2010-06-10 20:13:35 +08:00
H.at<float>(2,2) = rng->uniform( 0.8f, 1.2f);
warpPerspective( src, dst, H, src.size() );
}
FeatureDetector* createDetector( const string& detectorType )
{
FeatureDetector* fd = 0;
if( !detectorType.compare( "FAST" ) )
{
fd = new FastFeatureDetector( 10/*threshold*/, true/*nonmax_suppression*/ );
}
else if( !detectorType.compare( "STAR" ) )
{
fd = new StarFeatureDetector( 16/*max_size*/, 5/*response_threshold*/, 10/*line_threshold_projected*/,
8/*line_threshold_binarized*/, 5/*suppress_nonmax_size*/ );
}
else if( !detectorType.compare( "SIFT" ) )
{
fd = new SiftFeatureDetector(SIFT::DetectorParams::GET_DEFAULT_THRESHOLD(),
SIFT::DetectorParams::GET_DEFAULT_EDGE_THRESHOLD());
}
else if( !detectorType.compare( "SURF" ) )
{
fd = new SurfFeatureDetector( 100./*hessian_threshold*/, 3 /*octaves*/, 4/*octave_layers*/ );
}
else if( !detectorType.compare( "MSER" ) )
{
fd = new MserFeatureDetector( 5/*delta*/, 60/*min_area*/, 14400/*_max_area*/, 0.25f/*max_variation*/,
0.2/*min_diversity*/, 200/*max_evolution*/, 1.01/*area_threshold*/, 0.003/*min_margin*/,
5/*edge_blur_size*/ );
}
else if( !detectorType.compare( "GFTT" ) )
{
fd = new GoodFeaturesToTrackDetector( 1000/*maxCorners*/, 0.01/*qualityLevel*/, 1./*minDistance*/,
3/*int _blockSize*/, true/*useHarrisDetector*/, 0.04/*k*/ );
}
else
2010-06-10 20:13:35 +08:00
{
//CV_Error( CV_StsBadArg, "unsupported feature detector type");
}
return fd;
}
2010-06-10 20:13:35 +08:00
DescriptorExtractor* createDescriptorExtractor( const string& descriptorExtractorType )
{
2010-06-10 20:13:35 +08:00
DescriptorExtractor* de = 0;
if( !descriptorExtractorType.compare( "SIFT" ) )
{
2010-06-10 20:13:35 +08:00
de = new SiftDescriptorExtractor/*( double magnification=SIFT::DescriptorParams::GET_DEFAULT_MAGNIFICATION(),
bool isNormalize=true, bool recalculateAngles=true,
int nOctaves=SIFT::CommonParams::DEFAULT_NOCTAVES,
int nOctaveLayers=SIFT::CommonParams::DEFAULT_NOCTAVE_LAYERS,
int firstOctave=SIFT::CommonParams::DEFAULT_FIRST_OCTAVE,
int angleMode=SIFT::CommonParams::FIRST_ANGLE )*/;
}
2010-06-10 20:13:35 +08:00
else if( !descriptorExtractorType.compare( "SURF" ) )
{
2010-06-10 20:13:35 +08:00
de = new SurfDescriptorExtractor/*( int nOctaves=4, int nOctaveLayers=2, bool extended=false )*/;
}
else
2010-06-10 20:13:35 +08:00
{
//CV_Error( CV_StsBadArg, "unsupported descriptor extractor type");
}
return de;
}
2010-06-10 20:13:35 +08:00
DescriptorMatcher* createDescriptorMatcher( const string& descriptorMatcherType )
{
DescriptorMatcher* dm = 0;
if( !descriptorMatcherType.compare( "BruteForce" ) )
{
dm = new BruteForceMatcher<L2<float> >();
}
else
{
//CV_Error( CV_StsBadArg, "unsupported descriptor matcher type");
}
return dm;
}
const string winName = "correspondences";
2010-06-10 20:13:35 +08:00
void doIteration( const Mat& img1, Mat& img2, bool isWarpPerspective,
const vector<KeyPoint>& keypoints1, const Mat& descriptors1,
Ptr<FeatureDetector>& detector, Ptr<DescriptorExtractor>& descriptorExtractor,
Ptr<DescriptorMatcher>& descriptorMatcher,
double ransacReprojThreshold = -1, RNG* rng = 0 )
{
assert( !img1.empty() );
Mat H12;
if( isWarpPerspective )
{
assert( rng );
2010-06-10 20:13:35 +08:00
warpPerspectiveRand(img1, img2, H12, rng);
}
else
2010-06-11 04:56:40 +08:00
assert( !img2.empty()/* && img2.cols==img1.cols && img2.rows==img1.rows*/ );
cout << endl << "< Extracting keypoints from second image..." << endl;
vector<KeyPoint> keypoints2;
detector->detect( img2, keypoints2 );
cout << keypoints2.size() << " >" << endl;
2010-06-10 20:13:35 +08:00
cout << "< Computing descriptors for keypoints from second image..." << endl;
Mat descriptors2;
descriptorExtractor->compute( img2, keypoints2, descriptors2 );
cout << " >" << endl;
cout << "< Matching descriptors..." << endl;
vector<int> matches;
2010-06-10 20:13:35 +08:00
descriptorMatcher->clear();
descriptorMatcher->add( descriptors2 );
descriptorMatcher->match( descriptors1, matches );
cout << ">" << endl;
if( !isWarpPerspective && ransacReprojThreshold >= 0 )
{
cout << "< Computing homography (RANSAC)..." << endl;
vector<Point2f> points1(matches.size()), points2(matches.size());
2010-06-10 20:13:35 +08:00
for( size_t i = 0; i < matches.size(); i++ )
{
points1[i] = keypoints1[i].pt;
points2[i] = keypoints2[matches[i]].pt;
}
H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold );
cout << ">" << endl;
}
Mat drawImg;
if( !H12.empty() )
{
vector<char> mask( matches.size(), 0 );
vector<int>::const_iterator mit = matches.begin();
for( size_t i1 = 0; mit != matches.end(); ++mit, i1++ )
{
Point2f pt1 = keypoints1[i1].pt,
pt2 = keypoints2[*mit].pt;
if( norm(pt2 - applyHomography(H12, pt1)) < 4 ) // inlier
mask[i1] = 1;
}
// draw inliers
drawMatches( img1, img2, keypoints1, keypoints2, matches, mask, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255) );
// draw outliers
/*for( size_t i1 = 0; i1 < mask.size(); i1++ )
mask[i1] = !mask[i1];
drawMatches( img1, img2, keypoints1, keypoints2, matches, mask, drawImg, CV_RGB(0, 0, 255), CV_RGB(255, 0, 0),
DrawMatchesFlags::DRAW_OVER_OUTIMG | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );*/
}
else
{
drawMatches( img1, img2, keypoints1, keypoints2, matches, vector<char>(), drawImg, CV_RGB(0, 255, 0) );
}
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 20:13:35 +08:00
return -1;
}
bool isWarpPerspective = argc == 4;
double ransacReprojThreshold = -1;
if( !isWarpPerspective )
ransacReprojThreshold = atof(argv[5]);
2010-06-10 20:13:35 +08:00
cout << "< Creating detector, descriptor extractor and descriptor matcher ..." << endl;
Ptr<FeatureDetector> detector = createDetector( argv[1] );
Ptr<DescriptorExtractor> descriptorExtractor = createDescriptorExtractor( argv[2] );
Ptr<DescriptorMatcher> descriptorMatcher = createDescriptorMatcher( "BruteForce" );
cout << ">" << endl;
2010-06-10 20:13:35 +08:00
if( detector.empty() || descriptorExtractor.empty() || descriptorMatcher.empty() )
{
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;
}
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 20:13:35 +08:00
return -1;
}
cout << endl << "< Extracting keypoints from first image..." << endl;
vector<KeyPoint> keypoints1;
detector->detect( img1, keypoints1 );
cout << keypoints1.size() << " >" << endl;
2010-06-10 20:13:35 +08:00
cout << "< Computing descriptors for keypoints from first image..." << endl;
Mat descriptors1;
descriptorExtractor->compute( img1, keypoints1, descriptors1 );
cout << " >" << endl;
namedWindow(winName, 1);
RNG rng;
2010-06-10 20:13:35 +08:00
doIteration( img1, img2, isWarpPerspective, keypoints1, descriptors1,
detector, descriptorExtractor, descriptorMatcher,
ransacReprojThreshold, &rng );
for(;;)
{
char c = (char)cvWaitKey(0);
if( c == '\x1b' ) // esc
{
cout << "Exiting ..." << endl;
return 0;
}
else if( isWarpPerspective )
{
2010-06-10 20:13:35 +08:00
doIteration( img1, img2, isWarpPerspective, keypoints1, descriptors1,
detector, descriptorExtractor, descriptorMatcher,
ransacReprojThreshold, &rng );
}
}
waitKey(0);
2010-06-10 20:13:35 +08:00
return 0;
}