LATCH_match.cpp

这段代码是使用C++以及OpenCV库来实现图像特征匹配的程序。它主要包括了使用ORB算法检测关键点并使用LATCH算法提取特征描述子的步骤,之后使用暴力匹配方法(Brute-Force Matching)和比率测试来筛选出好的匹配点,最后计算匹配点对单应性矩阵(homography)的内点并绘制匹配结果,同时统计并输出关键点数量、匹配点数量、内点数量及内点比例等信息。如果OpenCV没有编译xfeatures2d模块,程序将输出错误信息。

result

#include // 包含输入输出流库

#include "opencv2/opencv_modules.hpp" // 包含OpenCV模块头文件

#ifdef HAVE_OPENCV_XFEATURES2D // 如果定义了HAVE_OPENCV_XFEATURES2D,表示opencv库包含xfeatures2d模块

#include // 包含OpenCV核心功能的头文件

#include // 包含图像处理相关函数的头文件

#include // 包含图形界面功能的头文件

#include // 包含特征点检测和描述子提取功能的头文件

#include // 包含扩展特征点算法的头文件,如SIFT和SURF

#include // 包含图像读写的功能头文件

#include // 包含向量容器类的头文件

// 如果你发现这份代码有用,请在你的工作中加入下面这篇论文的引用:

// Gil Levi and Tal Hassner, "LATCH: Learned Arrangements of Three Patch Codes", arXiv preprint arXiv:1501.03719, 15 Jan. 2015

using namespace std; // 使用标准命名空间

using namespace cv; // 使用OpenCV命名空间

const float inlier_threshold = 2.5f; // 内点距离阈值

const float nn_match_ratio = 0.8f; // 最近邻匹配比

int main(int argc, char* argv[])

{

CommandLineParser parser(argc, argv,

"{@img1 | graf1.png | input image 1}"

"{@img2 | graf3.png | input image 2}"

"{@homography | H1to3p.xml | homography matrix}");

// 解析命令行参数

Mat img1 = imread( samples::findFile( parser.get("@img1") ), IMREAD_GRAYSCALE);

// 读取图像1,转为灰度图

Mat img2 = imread( samples::findFile( parser.get("@img2") ), IMREAD_GRAYSCALE);

// 读取图像2,转为灰度图

Mat homography;

// 创建矩阵用于存储单应性变换矩阵

FileStorage fs( samples::findFile( parser.get("@homography") ), FileStorage::READ);

// 打开文件存储器,读取单应性矩阵

fs.getFirstTopLevelNode() >> homography;

// 从文件中读取单应性矩阵

vector kpts1, kpts2; // 定义存储关键点的向量

Mat desc1, desc2; // 定义存储描述子的矩阵

Ptr orb_detector = cv::ORB::create(10000); // 创建一个ORB特征检测器

Ptr latch = xfeatures2d::LATCH::create(); // 创建一个LATCH描述子提取器

orb_detector->detect(img1, kpts1); // 检测图像1的关键点

latch->compute(img1, kpts1, desc1); // 计算图像1的描述子

orb_detector->detect(img2, kpts2); // 检测图像2的关键点

latch->compute(img2, kpts2, desc2); // 计算图像2的描述子

BFMatcher matcher(NORM_HAMMING); // 创建汉明距离的Brute-Force匹配器

vector< vector > nn_matches; // 匹配结果

matcher.knnMatch(desc1, desc2, nn_matches, 2); // 对图像1和图像2的描述子进行K近邻匹配

vector matched1, matched2, inliers1, inliers2; // 匹配点和内点

vector good_matches; // 好的匹配

for (size_t i = 0; i < nn_matches.size(); i++) { // 遍历所有匹配

DMatch first = nn_matches[i][0]; // 第一个匹配

float dist1 = nn_matches[i][0].distance; // 第一个匹配的距离

float dist2 = nn_matches[i][1].distance; // 第二个匹配的距离

if (dist1 < nn_match_ratio * dist2) { // 如果距离小于设定的比例阈值

matched1.push_back(kpts1[first.queryIdx]); // 添加到匹配点1

matched2.push_back(kpts2[first.trainIdx]); // 添加到匹配点2

}

}

for (unsigned i = 0; i < matched1.size(); i++) { // 计算内点

Mat col = Mat::ones(3, 1, CV_64F); // 创建均为1的3x1矩阵

col.at(0) = matched1[i].pt.x; // 设置x坐标

col.at(1) = matched1[i].pt.y; // 设置y坐标

col = homography * col; // 应用单应性变换

col /= col.at(2); // 归一化

double dist = sqrt(pow(col.at(0) - matched2[i].pt.x, 2) +

pow(col.at(1) - matched2[i].pt.y, 2)); // 计算变换后的距离

if (dist < inlier_threshold) { // 如果距离小于阈值

int new_i = static_cast(inliers1.size()); // 新的索引

inliers1.push_back(matched1[i]); // 添加到内点1

inliers2.push_back(matched2[i]); // 添加到内点2

good_matches.push_back(DMatch(new_i, new_i, 0)); // 添加到好的匹配

}

}

Mat res; // 结果图像

drawMatches(img1, inliers1, img2, inliers2, good_matches, res); // 绘制匹配结果

imwrite("latch_result.png", res); // 保存结果图像

double inlier_ratio = inliers1.size() * 1.0 / matched1.size(); // 计算内点比例

cout << "LATCH Matching Results" << endl; // 输出匹配结果

cout << "*******************************" << endl; // 输出分割线

cout << "# Keypoints 1: \t" << kpts1.size() << endl; // 输出图像1的关键点数量

cout << "# Keypoints 2: \t" << kpts2.size() << endl; // 输出图像2的关键点数量

cout << "# Matches: \t" << matched1.size() << endl; // 输出匹配点数量

cout << "# Inliers: \t" << inliers1.size() << endl; // 输出内点数量

cout << "# Inliers Ratio: \t" << inlier_ratio << endl; // 输出内点比例

cout << endl;

imshow("result", res); // 显示结果图像

waitKey(); // 等待用户按键

return 0;

}

#else

int main()

{

std::cerr << "OpenCV was built without xfeatures2d module" << std::endl;

// 如果没有定义HAVE_OPENCV_XFEATURES2D,则输出错误信息

return 0;

}

#endif

终端输出:

LATCH Matching Results

*******************************

# Keypoints 1: 9105

# Keypoints 2: 9927

# Matches: 144

# Inliers: 81

# Inliers Ratio: 0.5625

推荐阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。