概述
AKAZE local features matching
一.知识补充
1.KAZE是日语音译过来的 , KAZE与SIFT、SURF最大的区别在于构造尺度空间,KAZE是利用非线性方式构造,得到的关键点也就更准确(尺度不变性 );
AKAZE局特征点检测与匹配 A表示Accelerated(加速的) ,与SIFT、 SUFR比较,更加稳定;相比比非线性尺度空间KAZE,AKAZE速度更加快。
2.homography详解
What is Homography?
Two images of a scene are related by a homography under two conditions.
The two images are that of a plane (e.g. sheet of paper, credit card etc.).
The two images were acquired by rotating the camera about its optical axis. We take such images while generating panoramas.
As mentioned earlier, a homography is nothing but a 3×3 matrix as shown below.
Let be a point in the first image and be the coordinates of the same physical point in the second image. Then, the Homography relates them in the following way
If we knew the homography, we could apply it to all the pixels of one image to obtain a warped image that is aligned with the second image.
二.代码实现
示例代码:
#include <opencv2/features2d.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
const float inlier_threshold = 2.5f; // Distance threshold to identify inliers with homography check
const float nn_match_ratio = 0.8f;
// Nearest neighbor matching ratio
int main(int argc, char* argv[])
{
CommandLineParser parser(argc, argv,
"{@img1 | ../data/graf1.png | input image 1}"
"{@img2 | ../data/graf3.png | input image 2}"
"{@homography | ../data/H1to3p.xml | homography matrix}");
Mat img1 = imread(parser.get<String>("@img1"), IMREAD_GRAYSCALE);
Mat img2 = imread(parser.get<String>("@img2"), IMREAD_GRAYSCALE);
Mat homography;
FileStorage fs(parser.get<String>("@homography"), FileStorage::READ);
fs.getFirstTopLevelNode() >> homography;
vector<KeyPoint> kpts1, kpts2;
Mat desc1, desc2;
Ptr<AKAZE> akaze = AKAZE::create();
akaze->detectAndCompute(img1, noArray(), kpts1, desc1);
akaze->detectAndCompute(img2, noArray(), kpts2, desc2);
BFMatcher matcher(NORM_HAMMING);
vector< vector<DMatch> > nn_matches;
matcher.knnMatch(desc1, desc2, nn_matches, 2);
vector<KeyPoint> matched1, matched2;
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]);
}
}
vector<DMatch> good_matches;
vector<KeyPoint> inliers1, inliers2;
for(size_t i = 0; i < matched1.size(); i++) {
Mat col = Mat::ones(3, 1, CV_64F);
col.at<double>(0) = matched1[i].pt.x;
col.at<double>(1) = matched1[i].pt.y;
col = homography * col;
col /= col.at<double>(2);
double dist = sqrt( pow(col.at<double>(0) - matched2[i].pt.x, 2) +
pow(col.at<double>(1) - matched2[i].pt.y, 2));
if(dist < inlier_threshold) {
int new_i = static_cast<int>(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() / (double) 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;
}
运行截图:
![](/home/mazh/Pictures/Screenshot from 2019-05-28 02-06-18.png)
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
Mat src1,src2, dst;
int main(int arc, char** argv) {
src1 = imread("graf1.png",IMREAD_GRAYSCALE);
src2 = imread("graf3.png", IMREAD_GRAYSCALE);
namedWindow("input1", CV_WINDOW_AUTOSIZE);
imshow("input1", src1);
/*double t1 = getTickCount();
kaze->detect(src1, keypoints1, Mat());
double t2 = getTickCount();
double time = (t2 - t1) / getTickFrequency();
KAZE检测需要2点多秒,如果使用AKAZE即accleration加速的KAZE只需要0点几秒*/
//AKAZE detect
Ptr<AKAZE>akaze = AKAZE::create();
vector<KeyPoint> keypoints1, keypoints2;
Mat descriptors1, descriptors2;
akaze->detectAndCompute(src1, Mat(), keypoints1, descriptors1);
akaze->detectAndCompute(src2, Mat(), keypoints2, descriptors2);
//BruteForce match
BFMatcher matcher;
vector<DMatch>matches;
matcher.match(descriptors1, descriptors2, matches);
Mat akaze_match_img;
drawMatches(src1, keypoints1, src2, keypoints2, matches, akaze_match_img);
imshow("output1", akaze_match_img);
//good matches
double minDist = 1000;
double maxDist = 0;
for (int i = 0; i < descriptors1.rows; i++)
{
double dist = matches[i].distance;
if (dist < minDist)
{
minDist = dist;
}
}
printf("%fn", minDist);
vector<DMatch>goodMatches;
for (int i = 0; i < descriptors1.rows; i++)
{
double dist = matches[i].distance;
if (dist < max(1.5 * minDist, 0.02))
{
goodMatches.push_back(matches[i]);
}
}
//draw good_match_img
Mat good_match_img;
drawMatches(src1, keypoints1, src2, keypoints2, goodMatches, good_match_img, Scalar::all(-1),Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("output2", good_match_img);
//perspective transform
vector<Point2f>src1GoodPoints;
vector<Point2f>src2GoodPoints;
for (int i = 0; i < goodMatches.size(); i++)
{
src1GoodPoints.push_back(keypoints1[goodMatches[i].queryIdx].pt);
src2GoodPoints.push_back(keypoints2[goodMatches[i].trainIdx].pt);
}
Mat P = findHomography(src1GoodPoints, src2GoodPoints,RANSAC);//有不良匹配点时用RANSAC
vector<Point2f>src1corner(4);
vector<Point2f>src2corner(4);
src1corner[0] = Point(0, 0);
src1corner[1] = Point(src1.cols, 0);
src1corner[2] = Point(src1.cols, src1.rows);
src1corner[3] = Point(0,src1.rows);
perspectiveTransform(src1corner, src2corner, P);
//在匹配图上画
line(good_match_img, Point(src2corner[0].x + src1.cols, src2corner[0].y), Point(src2corner[1].x + src1.cols, src2corner[1].y), Scalar(0, 0,255),2);
line(good_match_img, Point(src2corner[1].x + src1.cols, src2corner[1].y), Point(src2corner[2].x + src1.cols, src2corner[2].y), Scalar(0, 0, 255),2);
line(good_match_img, Point(src2corner[2].x + src1.cols, src2corner[2].y), Point(src2corner[3].x + src1.cols, src2corner[3].y), Scalar(0, 0, 255), 2);
line(good_match_img, Point(src2corner[3].x + src1.cols, src2corner[3].y), Point(src2corner[0].x + src1.cols, src2corner[0].y), Scalar(0, 0, 255),2);
/*
//在原图上画
line(src2, src2corner[0], src2corner[1], Scalar(0, 0, 255),2);
line(src2, src2corner[1], src2corner[2], Scalar(0, 0, 255), 2);
line(src2, src2corner[2], src2corner[3], Scalar(0, 0, 255), 2);
line(src2, src2corner[3], src2corner[0], Scalar(0, 0, 255), 2);
*/
imshow("result", good_match_img);
waitKey(0);
return 0;
}
运行截图:
![](/home/mazh/Pictures/Screenshot from 2019-05-28 15-31-03.png)
最后
以上就是独特人生为你收集整理的AKAZE local features matchingAKAZE local features matching的全部内容,希望文章能够帮你解决AKAZE local features matchingAKAZE local features matching所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复