OpenCV 的 hierachicalClustering 函数中的参数 'Distance d = Distance()' 是什么?

What is the parameter `Distance d = Distance()` in OpenCV's hierachicalClustering function?

本文关键字:Distance 是什么 函数 OpenCV hierachicalClustering 参数      更新时间:2023-10-16

我已经看过问题、源代码和其他示例,但我一辈子都无法理解Distance d = Distance()参数在函数中的含义

template<typename Distance> int flann::hierarchicalClustering(const Mat& features, Mat& centers, const cvflann::KMeansIndexParams& params, Distance d=Distance())

这个问题的评论中已经提到了,但我在任何地方都找不到"距离"类型。我导入了以下文件:

#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/flann/flann.hpp"

我已经检查了cvcvflanncv::flann名称空间,但找不到任何内容。

这是我的代码:

int tmp = cv::flann::hierarchicalClustering<cv::L2<float>>(descriptors, centers, params, ______ );

___显然是最后一个参数的位置。我试过cv::L2<float>()之类的东西,但也不起作用。

在源代码中,它看起来像一个模板。

我也试过:

int tmp = cv::flann::hierarchicalClustering<float, float>(descriptors, centers, params);

并且我得到错误"没有重载函数的实例"cv::flann::hierachicalClustering"与参数列表匹配。参数类型为:(cv::Mat,cv::Mat,cv::KMeansIndexParams)。

我使用的是OpenCV 2.4.11。

有什么想法吗?

FLANN用户手册中还有更多文档。看起来它是逐字复制到OpenCV中的。

参数Distance d = Distance()是设置距离算法的默认方法参数。通常,这将是FLANN_DIST_L2。为什么在任何地方都找不到Distance"类型",是因为它实际上是一个typename。OpenCV选择使用C++专门化(通过模板)而不是继承,以确保不同的距离函数具有相同的特性。

这是一个不平凡的话题,所以http://www.gotw.ca/publications/mxc++-item-4.htm应该提供一个相当轻松的介绍。如果我能激起你的兴趣,可以看看安德烈·亚历山德雷斯库开创性的现代C++设计。

添加这个以供将来参考,因为我自己只是在为OpenCV FLANN类而挣扎

这对我有效:

// define parameters
cvflann::KMeansIndexParams kmeansParams(10, 100, cvflann::FLANN_CENTERS_KMEANSPP);
// note cvflann - not cv::flann - namespace there
int nClusters = flann::hierarchicalClustering<flann::L2<float>>(samples, centers, kmeansParams );
// note regular flann namespace here

问题是marcman使用的是<cv::L2<float>>而不是<flann::L2<float>>

flann名称空间非常混乱,并且文档中缺少

而且由于模板化,在IDE中很难弄清楚。