#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
static void ShowHelpText();
int main(int argc, char **argv) {
system("color 4F");
ShowHelpText();
Mat img_1 = imread("1.jpg", 1);
Mat img_2 = imread("2.jpg", 1);
if (!img_1.data || !img_2.data) {
printf("读取图片image0错误~! \n");
return false;
}
int minHessian = 300;
SURF detector(minHessian);
std::vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect(img_1, keypoints_1);
detector.detect(img_2, keypoints_2);
SURF extractor;
Mat descriptors_1, descriptors_2;
extractor.compute(img_1, keypoints_1, descriptors_1);
extractor.compute(img_2, keypoints_2, descriptors_2);
FlannBasedMatcher matcher;
std::vector<DMatch> matches;
matcher.match(descriptors_1, descriptors_2, matches);
double max_dist = 0;
double min_dist = 100;
for (int i = 0; i < descriptors_1.rows; i++) {
double dist = matches[ i ].distance;
if (dist < min_dist)
min_dist = dist;
if (dist > max_dist)
max_dist = dist;
}
printf("> 最大距离(Max dist) : %f \n", max_dist);
printf("> 最小距离(Min dist) : %f \n", min_dist);
std::vector<DMatch> good_matches;
for (int i = 0; i < descriptors_1.rows; i++) {
if (matches[ i ].distance < 2 * min_dist) {
good_matches.push_back(matches[ i ]);
}
}
Mat img_matches;
drawMatches(img_1, keypoints_1, img_2, keypoints_2, good_matches, img_matches, Scalar::all(-1),
Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
for (int i = 0; i < good_matches.size(); i++) {
printf(">符合条件的匹配点 [%d] 特征点1: %d -- 特征点2: %d \n", i,
good_matches[ i ].queryIdx, good_matches[ i ].trainIdx);
}
imshow("匹配效果图", img_matches);
waitKey(0);
return 0;
}
void ShowHelpText() {
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV2版的第91个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION);
printf("\n\n ----------------------------------------------------------------------------\n");
printf("\n\t欢迎来到【FLNN特征匹配】示例程序\n\n");
printf("\n\n\t按键操作说明: \n\n"
"\t\t键盘按键任意键- 退出程序\n\n");
}