// 简单程序,用于展示如何使用FLANN在数据集中搜索查询图片
#include <iostream>
#include <vector>
#include "opencv2/core.hpp"
#include "opencv2/core/utils/filesystem.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/flann.hpp"
using namespace cv; // 使用OpenCV命名空间
using std::cout; // 使用标准命名空间中的cout
using std::endl; // 使用标准命名空间中的endl
#define _ORB_ // 定义_ORB_,以便在后面选择特征点检测算法时使用
// 命令行参数定义
const char* keys =
"{ help h | | 打印帮助信息。 }"
"{ dataset | | 作为数据集的图片文件夹的路径。 }"
"{ image | | 要在数据集中搜索的图片的路径。 }"
"{ save | | 保存flann结构的路径和文件名。 }"
"{ load | | 加载flann结构的路径和文件名。 }";
// 结构体,用于存储图片信息
struct img_info {
int img_index; // 图片索引
unsigned int nbr_of_matches; // 匹配数量
// 结构体构造函数
img_info(int _img_index, unsigned int _nbr_of_matches)
: img_index(_img_index), nbr_of_matches(_nbr_of_matches) {}
};
// 程序入口点
int main( int argc, char* argv[] )
{
//-- 测试程序选项
CommandLineParser parser( argc, argv, keys ); // 创建命令行解析器实例
if (parser.has("help")) // 如果有help选项
{
parser.printMessage(); // 打印帮助信息
return -1; // 退出程序
}
// 解析图像路径
const cv::String img_path = parser.get<String>("image");
Mat img = imread( samples::findFile( img_path ), IMREAD_GRAYSCALE ); // 读取并转化为灰度图
if (img.empty() ) // 如果图片为空,读取失败
{
cout << "Could not open the image "<< img_path << endl; // 打印错误信息
return -1; // 退出程序
}
// 解析数据库路径
const cv::String db_path = parser.get<String>("dataset");
if (!utils::fs::isDirectory(db_path)) // 如果数据集文件夹不存在
{
cout << "Dataset folder "<< db_path.c_str() <<" doesn't exist!" << endl; // 打印错误信息
return -1; // 退出程序
}
// 解析加载FLANN结构的路径
const cv::String load_db_path = parser.get<String>("load");
if ((load_db_path != String()) && (!utils::fs::exists(load_db_path))) // 如果给定路径且该路径不存在
{
cout << "File " << load_db_path.c_str()
<< " where to load the flann structure fr