opencv - 如何在opencv中使用GenericIndex类

标签 opencv feature-detection flann

由于 Index_ 是 flann 库中的弃用类,我正在尝试使用 GenericIndex 类,它是一个模板类。我不知道如何为该类创建对象。

flann.hpp中的Class定义如下:

template <typename Distance>
class GenericIndex
{
public:
        typedef typename Distance::ElementType ElementType;
        typedef typename Distance::ResultType DistanceType;

        GenericIndex(const Mat& features, const ::cvflann::IndexParams& params, Distance distance = Distance());

        ~GenericIndex();

        void knnSearch(const vector<ElementType>& query, vector<int>& indices,
                       vector<DistanceType>& dists, int knn, const ::cvflann::SearchParams& params);
        void knnSearch(const Mat& queries, Mat& indices, Mat& dists, int knn, const ::cvflann::SearchParams& params);

        int radiusSearch(const vector<ElementType>& query, vector<int>& indices,
                         vector<DistanceType>& dists, DistanceType radius, const ::cvflann::SearchParams& params);
        int radiusSearch(const Mat& query, Mat& indices, Mat& dists,
                         DistanceType radius, const ::cvflann::SearchParams& params);

        void save(std::string filename) { nnIndex->save(filename); }

        int veclen() const { return nnIndex->veclen(); }

        int size() const { return nnIndex->size(); }

        ::cvflann::IndexParams getParameters() { return nnIndex->getParameters(); }

        FLANN_DEPRECATED const ::cvflann::IndexParams* getIndexParameters() { return nnIndex->getIndexParameters(); }

private:
        ::cvflann::Index<Distance>* nnIndex;
};

最佳答案

要使用 GenericIndex,您必须在模板实例化时指定距离仿函数,而不是像使用 Index_ 那样指定要素类型。 flann/dist.h 中定义了一些距离仿函数:L1L2MinkowskyDistance 等。这些本身就是模板,按特征类型参数化。

因此,在使用 Index_ 的地方您要声明:

cv::flann::Index_<int> index;

使用 GenericIndex 你可以(例如):

cv::flann::GenericIndex<cvflann::L2<int> > index;

cvflann::L2 是实现 L2 norm 的仿函数基于距离度量。请注意,仿函数的命名空间是 cvflann,而不是 cv::flannGenericIndex 一样(为什么开发人员决定让这两个完全相同 -但不完全是命名空间超出了我的范围)。

Index_GenericIndex 在其他方面具有非常相似的接口(interface),因此除此之外您可能无需更改任何内容。

关于opencv - 如何在opencv中使用GenericIndex类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189345/

相关文章:

c - 非常大的图像中的内存高效行拼接

matlab - OpenCV 中是否有与 Matlab Vision.blobAnalysis() 功能相同的函数?

opencv - SiftFeatureDetector()和Ptr <FeatureDetector>之间的区别

c++ - 错误 :.。 'va_list' 尚未声明

c# - emgu cv 能做到普通 open CV 能做到的一切吗?

python - OpenCV 合成 2 张不同大小的图像

android - 调试时查看 Mat 内容

python - 使用 Python 测量 OpenCv 上像素之间的距离

opencv - opencv 中的 FLANN 运行速度太慢

python-2.7 - 与opencv 3 ORB中的匹配错误