Advertisement

akaze matlab 代码,AKAZE本地功能匹配

阅读量:

介绍

在本教程中,我们将致力于教授您如何利用AKAZE [5]本地功能来识别并配对两个图像的关键点。我们将在给定的单对应矩阵的一对图像上进行详细分析,并统计这些关键点的数量。

内联数(即适合给定单应性匹配的)。

数据

我们将使用牛津数据集的Graffity序列中的图像1和3 。

85a2ca58ffccf303b4fe2b029affc8d0.png

Homography由3乘3矩阵给出:7.6285898e-01 -2.9922929e-01 2.2567123e + 02

3.3443473e-01 1.0143901e + 00 -7.6999973e + 01

3.4663091e-04 -1.4364524e-05 1.0000000e + 00

您可访问opencv/samples/cpp目录中查找灰度图像文件(gray1.png, gray3.png)以及单色参数文件(H1to3p.xml)。

源代码#include

#include

#include

#include

#include

using namespace std;

using namespace cv;

const float inlier_threshold = 2.5f; // Distance threshold to identify inliers

const float nn_match_ratio = 0.8f; // Nearest neighbor matching ratio

int main(void)

{

Mat img1 = imread("../data/graf1.png", IMREAD_GRAYSCALE);

Mat img2 = imread("../data/graf3.png", IMREAD_GRAYSCALE);

Mat homography;

FileStorage fs("../data/H1to3p.xml", FileStorage::READ);

fs.getFirstTopLevelNode() >> homography;

vector kpts1, kpts2;

Mat desc1, desc2;

Ptr akaze = AKAZE::create();

akaze->detectAndCompute(img1, noArray(), kpts1, desc1);

akaze->detectAndCompute(img2, noArray(), kpts2, desc2);

BFMatcher matcher(NORM_HAMMING);

vector< vector > nn_matches;

matcher.knnMatch(desc1, desc2, nn_matches, 2);

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]);

matched2.push_back(kpts2[first.trainIdx]);

}

}

for(unsigned i = 0; i < matched1.size(); i++) {

Mat col = Mat::ones(3, 1, CV_64F);

col.at(0) = matched1[i].pt.x;

col.at(1) = matched1[i].pt.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]);

inliers2.push_back(matched2[i]);

good_matches.push_back(DMatch(new_i, new_i, 0));

}

}

Mat res;

drawMatches(img1, inliers1, img2, inliers2, good_matches, res);

imwrite("akaze_result.png", res);

double inlier_ratio = inliers1.size() * 1.0 / matched1.size();

cout << "A-KAZE Matching Results" << endl;

cout << "*******************************" << endl;

cout << "# Keypoints 1: \t" << kpts1.size() << endl;

cout << "# Keypoints 2: \t" << kpts2.size() << endl;

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;

}

说明加载图像和单应性Mat img1 = imread("graf1.png", IMREAD_GRAYSCALE);

Mat img2 = imread("graf3.png", IMREAD_GRAYSCALE);

Mat homography;

FileStorage fs("H1to3p.xml", FileStorage::READ);

fs.getFirstTopLevelNode() >> homography;

我们正在获取灰度图像。Homography将被保存在由FileStorage生成的XML文件中。

使用AKAZE检测关键点并计算描述符vector kpts1, kpts2;

Mat desc1, desc2;

AKAZE akaze;

akaze(img1, noArray(), kpts1, desc1);

akaze(img2, noArray(), kpts2, desc2);

我们生成AKAZE对象,并调用其 operator()功能。为避免涉及mask参数的调用选择相应的处理方式。

使用强力匹配器找到2-nn个匹配项BFMatcher matcher(NORM_HAMMING);

vector< vector > nn_matches;

matcher.knnMatch(desc1, desc2, nn_matches, 2);

我们使用汉明距离,因为AKAZE默认使用二进制描述符。

使用2-nn匹配来找到正确的关键点匹配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]);

matched2.push_back(kpts2[first.trainIdx]);

}

}

如果最接近的比例比第二个最接近的比例更接近,则匹配是正确的。

检查我们的匹配是否适合单性模型

for(int i = 0; i < matched1.size(); i++) {

Mat col = Mat::ones(3, 1, CV_64F);

col.at(0) = matched1[i].pt.x;

col.at(1) = matched1[i].pt.y;

col = homography * col;

col /= col.at(2);

float 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 = inliers1.size();

inliers1.push_back(matched1[i]);

inliers2.push_back(matched2[i]);

good_matches.push_back(DMatch(new_i, new_i, 0));

}

}

当两个关键点间的映射距离低于预设阈值时,则满足进行单应性变换。

我们为内部值创建一组新的匹配,因为它是绘制函数所必需的。

输出结果Mat res;

drawMatches(img1, inliers1, img2, inliers2, good_matches, res);

imwrite("res.png", res);

...

这里我们保存生成的图像并打印一些统计信息。

结果

找到匹配

51bb9b4ec0fcc0a0aeefdd45db06c8fe.png

A-KAZE匹配结果Keypoints 1: 2943

Keypoints 2: 3511

Matches: 447

Inliers: 308

Inlier Ratio: 0.689038}

全部评论 (0)

还没有任何评论哟~