一、程序示例及注释
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main()
{
Mat objImage = imread("curry1.jpg", 0);
Mat sceneImage = imread("curry_dlt.jpg", 0);
// kaze detection
Ptr<AKAZE> detector = AKAZE::create();
vector<KeyPoint> obj_keypoints, scene_keypoints;
Mat obj_descriptor, scene_descriptor;
double t1 = getTickCount();
// 得到keypoints 和 描述子
detector->detectAndCompute(objImage, Mat(), obj_keypoints, obj_descriptor);
detector->detectAndCompute(sceneImage, Mat(), scene_keypoints, scene_descriptor);
double t2 = getTickCount();
// 使用的时间
double d_kazeTime = 1000 * (t2 - t1) / getTickFrequency();
printf("Use time is: %f\n", d_kazeTime);
// matching
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(obj_descriptor, scene_descriptor, matches);
// draw matches -key points
Mat akazeMatchesImage;
drawMatches(objImage, obj_keypoints, sceneImage, scene_keypoints, matches, akazeMatchesImage);
imshow("akazeMatchesIamge", akazeMatchesImage);
// good matches
vector<DMatch> goodMatches;
double minDist = 10000, maxDist = 0;
for (size_t i = 0; i < matches.size(); i++)
{
double dist = matches[i].distance;
if (dist < minDist)
minDist = dist;
if (dist > maxDist)
maxDist = dist;
}
for (size_t i = 0; i < matches.size(); i++)
{
double dist = matches[i].distance;
if (dist < max(3 * minDist, 0.02))
goodMatches.push_back(matches[i]);
}
Mat goodMathcesImage;
drawMatches(objImage, obj_keypoints, sceneImage, scene_keypoints, goodMatches, goodMathcesImage,\
Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("goodMathcesImage", goodMathcesImage);
waitKey(0);
return 0;
}
二、运行结果

