理论知识参考于基于图像的三维重建-特征点检测与匹配
在这里用的opencv是440版本的,接下来就是相应的代码
#include<opencv2/xfeatures2d/nonfree.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/calib3d/calib3d.hpp>
#include<iostream>
#include<tinydir.h>
using namespace cv;
using namespace std;
void extract_features(vector<string>& image_names, vector<vector<KeyPoint>>& key_point_for_all,
vector<Mat>& descriptor_for_all, vector<vector<Vec3b>>& colors_for_all)
{
key_point_for_all.clear();
descriptor_for_all.clear();
Mat image;
//读取图像,获取图像特征点,并保存
Ptr<Feature2D> sift = cv::SIFT::create(0, 3, 0.04, 10.0);
for (auto it = image_names.begin(); it != image_names.end(); ++it)
{
image = imread(*it);
if (image.empty())
continue;
cout << "Extracing features: " << *it << endl;
vector<KeyPoint> key_points;
Mat descriptor;
//检测特征点并计算描述符
sift->detectAndCompute(image, noArray(), key_points, descriptor);
//特征点过少,则排除该图像
if (key_points.size() <= 10)
continue;
key_point_for_all.push_back(key_points);
descriptor_for_all.push_back(descriptor);
vector<Vec3b> colors(key_points.size());
for (int i = 0; i < key_points.size(); ++i)
{
Point2f& p = key_points[i].pt;
colors[i] = image.at<Vec3b>(p.y, p.x);
}
colors_for_all.push_back(colors);
}
}
void match_features(Mat& query, Mat& train, vector<DMatch>& matches)
{
vector<vector<DMatch>> knn_matches;
BFMatcher matcher(NORM_L2);
matcher.knnMatch(query, train, knn_matches, 2);
//获取满足Ratio Test的最小匹配的距离
float min_dist = FLT_MAX;
for (int r = 0; r < knn_matches.size(); ++r)
{
//Ratio Test
if (knn_matches[r][0].distance > 0.6*knn_matches[r][1].distance)
continue;
float dist = knn_matches[r][0].distance;
if (dist < min_dist)
min_dist = dist;
}
matches.clear();
for (size_t r = 0; r < knn_matches.size(); ++r)
{
//排除不满足Ratio Test的点和匹配距离过大的点
if (knn_matches[r][0].distance > 0.6*knn_matches[r][1].distance ||
knn_matches[r][0].distance > 5 * max(min_dist, 10.0f))
continue;
//保存匹配点
matches.push_back(knn_matches[r][0]);
}
}
void match_features(vector<Mat>& descriptor_for_all, vector<vector<DMatch>>& matches_for_all)
{
matches_for_all.clear();
// n个图像,两两顺次有 n-1 对匹配
// 1与2匹配,2与3匹配,3与4匹配,以此类推
for (int i = 0; i < descriptor_for_all.size() - 1; ++i)
{
cout << "Matching images " << i << " - " << i + 1 << endl;
vector<DMatch> matches;
match_features(descriptor_for_all[i], descriptor_for_all[i + 1], matches);
matches_for_all.push_back(matches);
}
}
void get_file_names(string dir_name, vector<string> & names)
{
names.clear();
tinydir_dir dir;
tinydir_open(&dir, dir_name.c_str());
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if (!file.is_dir)
{
names.push_back(file.path);
}
tinydir_next(&dir);
}
tinydir_close(&dir);
}
void show(vector<string>& image_names, vector<vector<KeyPoint>>& key_point_for_all, vector<vector<DMatch>>& matches_for_all)
{
for (int i = 0; i < image_names.size()-1; i++)
{
Mat imageMatches;
Mat image1 = imread(image_names[i]);
Mat image2 = imread(image_names[i+1]);
if (image1.empty() || image2.empty())
continue;
drawMatches(image1, key_point_for_all[i], image2, key_point_for_all[i + 1], matches_for_all[i], imageMatches, Scalar(255, 0, 0));
imwrite(to_string(i) + "-" + to_string(i + 1)+".jpg", imageMatches);
}
}
int main(int argc, char **argv)
{
if (argc < 2)
return -1;
vector<string> img_names;
get_file_names("images", img_names);
if (img_names.size() == 0)
{
cout << "图片路径error" << endl;
return -2;
}
vector<vector<KeyPoint>> key_points_for_all;
vector<Mat> descriptor_for_all;
vector<vector<Vec3b>> colors_for_all;
vector<vector<DMatch>> matches_for_all;
//提取所有图像的特征
extract_features(img_names, key_points_for_all, descriptor_for_all, colors_for_all);
//对所有图像进行顺次的特征匹配
match_features(descriptor_for_all, matches_for_all);
show(img_names, key_points_for_all, matches_for_all);
return 0;
}
对应的资源下载为代码文件下载
得到的结果图如下: