openMVG例子学习之features_image_matching
写在前面
这个功能opencv也可以实现,原理与流程我自前实现了出来,不过这里的描述子选项更多,学习的化csdn该类文章满大街,也容易理解。
/*
功能:
两幅图像查找特征点并且特征点的匹配与绘制
本文给出了三种描述子SIFT、AKAZE、AKAZE_MLDB
输入:
两幅图像,描述子种类
输出:
IndMatches:vector,存储的类型为IndMatch
IndMatch的数据为uint32_t的x和y
附加:
执行匹配使用的是查找最近邻,用距离比过滤
*/
#include "openMVG/image/image_io.hpp"
#include "openMVG/image/image_concat.hpp"
#include "openMVG/features/akaze/image_describer_akaze.hpp"
#include "openMVG/features/sift/SIFT_Anatomy_Image_Describer.hpp"
#include "openMVG/features/svg_features.hpp"
#include "openMVG/matching/regions_matcher.hpp"
#include "openMVG/matching/svg_matches.hpp"
#include "third_party/stlplus3/filesystemSimplified/file_system.hpp"
#include "third_party/cmdLine/cmdLine.h"
#include <memory>
#include <string>
int main()
{
// 选择所需的图像描述符
std::string sImage_describer_type = "SIFT";
//路径需要修改
const std::string jpg_filenameL = stlplus::folder_up(std::string(/*这里填写绝对路径*/))
+ "/imageData/StanfordMobileVisualSearch/Ace_0.png";
const std::string jpg_filenameR = stlplus::folder_up(std::string(/*这里填写绝对路径*/))
+ "/imageData/StanfordMobileVisualSearch/Ace_1.png";
openMVG::image::Image<unsigned char> imageL, imageR;
openMVG::image::ReadImage(jpg_filenameL.c_str(), &imageL);
openMVG::image::ReadImage(jpg_filenameR.c_str(), &imageR);
assert(imageL.data() && imageR.data());
// 查找关键点
using namespace openMVG::features;
std::unique_ptr<Image_describer> image_describer;
//例子给出了三种描述子
if (sImage_describer_type == "SIFT")
image_describer.reset(new SIFT_Anatomy_Image_describer
(SIFT_Anatomy_Image_describer::Params(/*这里设置参数*/)));
else if (sImage_describer_type == "AKAZE")
image_describer = AKAZE_Image_describer::create
(AKAZE_Image_describer::Params(AKAZE::Params(), AKAZE_MSURF));
else if (sImage_describer_type == "AKAZE_MLDB")
image_describer = AKAZE_Image_describer::create
(AKAZE_Image_describer::Params(AKAZE::Params(), AKAZE_MLDB));
if (!image_describer)
{
std::cerr << "Invalid Image_describer type" << std::endl;
return 1;
}
//--
// 使用图像描述符检测区域
//--
std::map<openMVG::IndexT, std::unique_ptr<openMVG::features::Regions>> regions_perImage;
image_describer->Describe(imageL, regions_perImage[0]);
image_describer->Describe(imageR, regions_perImage[1]);
//--
// 显示使用过的图像和特征
//--
{
//- Show images side by side
openMVG::image::Image<unsigned char> concat;
openMVG::image::ConcatH(imageL, imageR, concat);
const std::string out_filename = "00_images.jpg";
openMVG::image::WriteImage(out_filename.c_str(), concat);
}
{
//- 在图像上绘制特征(并排)
Features2SVG
(
jpg_filenameL,
{ imageL.Width(), imageL.Height() },
regions_perImage.at(0)->GetRegionsPositions(),
jpg_filenameR,
{ imageR.Width(), imageR.Height() },
regions_perImage.at(1)->GetRegionsPositions(),
"01_features.svg"
);
}
//--
// 计算匹配点
//--
//-- 执行匹配->查找最近邻,用距离比过滤
openMVG::matching::IndMatches vec_PutativeMatches; //匹配的特征点对vector
openMVG::matching::DistanceRatioMatch(
0.8, openMVG::matching::BRUTE_FORCE_L2,
*regions_perImage.at(0).get(),
*regions_perImage.at(1).get(),
vec_PutativeMatches);
// 绘制对应关系
{
const bool bVertical = true;
Matches2SVG
(
jpg_filenameL,
{ imageL.Width(), imageL.Height() },
regions_perImage.at(0)->GetRegionsPositions(),
jpg_filenameR,
{ imageR.Width(), imageR.Height() },
regions_perImage.at(1)->GetRegionsPositions(),
vec_PutativeMatches,
"02_Matches.svg",
bVertical
);
}
//显示一些统计信息
std::cout
<< regions_perImage.at(0)->RegionCount() << " #Features on image A" << std::endl
<< regions_perImage.at(1)->RegionCount() << " #Features on image B" << std::endl
<< vec_PutativeMatches.size() << " #matches with Distance Ratio filter" << std::endl;
}