【opencv】示例-flann_search_dataset.cpp 使用FLANN算法在一个图片数据集中搜索一个查询图像...

420d9936fe89eb7529ee885811779c24.png

// 简单程序,用于展示如何使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值