使用SURF算法检测两幅图关键点后暴力匹配
SURF特征检测
使用SURF(Speeded Up Robust Features)算法来检测两张图像之间的关键点,并使用FLANN(Fast Library for Approximate Nearest Neighbors)基于特征描述符向量进行匹配
图像特征点检测(SURF)、(FLANN)匹配和目标定位
1.feature_description—SURF_matching_Demo.cpp(Debug模式)使用SURF算法检测两幅图关键点后暴力匹配
box.png
box_in_scene.png
minHessian=400
minHessian=1000
minHessian=10000
#include <iostream> // 包含标准输入输出流库
#include "opencv2/core.hpp" // 包括OpenCV的核心功能头文件
#ifdef HAVE_OPENCV_XFEATURES2D // 如果定义了HAVE_OPENCV_XFEATURES2D
#include "opencv2/highgui.hpp" // 包括用户界面相关功能的头文件
#include "opencv2/features2d.hpp" // 包括特征检测相关功能的头文件
#include "opencv2/xfeatures2d.hpp" // 包括特殊特征检测的头文件(非free模块)
using namespace cv; // 使用cv命名空间
using namespace cv::xfeatures2d; // 使用cv的xfeatures2d命名空间
using std::cout; // 使用标准库中的cout
using std::endl; // 使用标准库中的endl
const char* keys = // 定义命令行参数
"{ help h | | Print help message. }"
"{ input1 | box.png | Path to input image 1. }"
"{ input2 | box_in_scene.png | Path to input image 2. }";
int main( int argc, char* argv[] ) // 主函数
{
CommandLineParser parser( argc, argv, keys ); // 创建命令行解析器
Mat img1 = imread( samples::findFile( parser.get<String>("input1") ), IMREAD_GRAYSCALE ); // 读取第一张图片为灰度图
Mat img2 = imread( samples::findFile( parser.get<String>("input2") ), IMREAD_GRAYSCALE ); // 读取第二张图片为灰度图
if ( img1.empty() || img2.empty() ) // 如果图片读取失败
{
cout << "Could not open or find the image!\n" << endl; // 打印错误信息
parser.printMessage(); // 打印帮助信息
return -1; // 返回错误代码
}
//-- Step 1: Detect the keypoints using SURF Detector, compute the descriptors
int minHessian = 400; // 设置SURF算法的Hessian阈值
Ptr<SURF> detector = SURF::create( minHessian ); // 创建SURF特征检测器
std::vector<KeyPoint> keypoints1, keypoints2; // 存放两张图片的关键点
Mat descriptors1, descriptors2; // 存放两张图片的描述符
detector->detectAndCompute( img1, noArray(), keypoints1, descriptors1 ); // 检测并计算img1的关键点和描述符
detector->detectAndCompute( img2, noArray(), keypo