本文为特征点匹配学习总结,讲解 OpenCV 3 中的特征点匹配。
特征点匹配
OpenCV 中封装了常用的特征点算法(如 SIFT,SURF,ORB等),提供了统一的接口便于调用。 下面代码是 OpenCV 中使用其 feature 2D 模块的示例代码:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main ( int argc, char** argv )
{
if ( argc != 3 )
{
cout<<"usage: feature_extraction img1 img2"<<endl;
return 1;
}
// 读取图像
Mat img1 = imread ( argv[1], CV_LOAD_IMAGE_COLOR );
Mat img2 = imread ( argv[2], CV_LOAD_IMAGE_COLOR );
// 初始化
std::vector<KeyPoint> keypoints1, keypoints2;
Mat descriptors1, descriptors2;
Ptr<ORB> orb = ORB::create();
// 提取特征点
orb->detect(img1, keypoints1);
orb->detect(img2, keypoints2);
// 绘制特征点
Mat outimg1;
drawKeypoints(img1, keypoints1, outimg1, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("ORB特征点", outimg1);
// 计算 BRIEF 描述子
orb->compute ( img1, keypoints1, descriptors1 );
orb->compute ( img2, keypoints2, descriptors2 );
// 对两幅图像中的描述子进行匹配,使用 Hamming 距离
vector<DMatch> matches;
BFMatcher bfMatcher ( NORM_HAMMING );
bfMatcher.match ( descriptors1, descriptors2, matches );
Mat img_match;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_match);
imshow("所有匹配点对", img_match