参考网址:https://www.cnblogs.com/skyfsm/p/7411961.html
Mat img_1 = imread(“left3.jpg”,0);
Mat img_2 = imread( “right3.jpg”,0 );
if( !img_1.data || !img_2.data )
{
cout<< ” –(!) Error reading images ” << endl;
return -1;
}
//– Step 1: Detect the keypoints using ORB Detector
cv::Ptr orb = cv::ORB::create(200);
std::vector keypoints_1, keypoints_2;
orb->detect( img_1, keypoints_1 );
orb->detect( img_2, keypoints_2 );
//– Step 2: Calculate descriptors (feature vectors)
Mat descriptors_1, descriptors_2;
// descriptor is a cv::Mat, with rows the same as nFeatures, and cols as 32 (8UC1)
orb->compute( img_1, keypoints_1, descriptors_1 );
orb->compute( img_2, keypoints_2, descriptors_2 );
cout << type2str(descriptors_1.type()) << ” ” << descriptors_1.rows << “*” << descriptors_1.cols << endl;;
clock_t start,finish;
double totaltime;
start=clock();
//– Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector matches;
// the descriptor for FlannBasedMatcher should has matrix element of CV_32F
if( descriptors_1.type()!=CV_32F )
{
descriptors_1.convertTo( descriptors_1, CV_32F );
descriptors_2.convertTo( descriptors_2, CV_32F );
}
matcher.match( descriptors_1, descriptors_2, matches );
double min_dist = min_element( matches.begin(),
matches.end(),
[]( const DMatch& d1, const DMatch& d2 )->double
{
return d1.distance < d2.distance;
} )->distance;
cout << min_dist << endl;
vector good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{
if( matches[i].distance < max( min_dist*2, 60.0 ) )
{
good_matches.push_back( matches[i]);
}
}
int x = 0;
int y = 0;
for(int i = 0; i < good_matches.size(); i++)
{
x += keypoints_1[good_matches[i].queryIdx].pt.x - keypoints_2[good_matches[i].trainIdx].pt.x;
y += keypoints_1[good_matches[i].queryIdx].pt.y - keypoints_2[good_matches[i].trainIdx].pt.y;
}
cout << “x = ” << x/good_matches.size() << endl;
cout << “Y = ” << y/good_matches.size() << endl;
Mat img_matches;
drawMatches( img_1, keypoints_1, img_2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//– Show detected matches
imshow( “Good Matches”, img_matches );