OpenCV 中的分层 k 均值无需了解“k”

发布于 2024-10-27 19:17:01 字数 1620 浏览 3 评论 0原文

我正在尝试对一组 4D 向量进行聚类,但事先不知道应该有多少个聚类。过去,在了解集群数量的情况下,我已经能够使用 cvKmeans2 进行集群。我在 API 中搜寻并发现了 cv:: flann::hierarchicalClustering。这看起来它会做我需要的事情(即,执行 k 均值,在必要时分割集群,迭代直到分割使结果恶化),但我真的在“索引参数”上苦苦挣扎。

我发现我需要创建一个 索引结构 作为第二个参数,但我从以下代码中收到错误:

cv::flann::Index fln_idx = cv::flann::KMeansIndexParams::createIndex( framePoints );

错误是:

../src/segmentation_1.cpp:592: 错误:无法调用成员函数 'virtual flann::Index* cv::flann::KMeansIndexParams::createIndex(const cv::Mat&) const' without object

framePoints 定义如下:

CvMat *framePoints = cvCreateMat(frameTracklets.size( ), 4, CV_32FC1 );

我相当确定我正在做一些非常愚蠢的事情(我的 C++ 知识还可以,但不是很好)。我想我已经发布了所有相关的代码,但如果没有,请告诉我,我将发布更多内容。

提前致谢!

更新

我遵循 LumpN 的建议并使用以下内容创建了一个 Kmeans 对象:

cv::Mat centres;
cv::flann::KMeansIndexParams fln_idx = cv::flann::KMeansIndexParams();
fln_idx.createIndex( framePoints );

int numClust;
numClust = hierarchicalClustering(framePoints, centres, fln_idx);

现在,当我运行它时,我收到来自 hierarchicalClustering() 的错误消息,内容类似于“the所需集群的数量应为 >= 1”(我需要在开始工作时进行检查 - 然后我将更新实际错误)。我假设 createIndex() 给了它起点,然后 hierarchicalClustering() 分割集群,直到找到好的结果(不确定这是否是最佳的)。我是否需要使用一些参数来调用 cv::flann::KMeansIndexParams() ?我查看了 api,完全困惑了! 再次感谢!

I'm trying to cluster a set of 4D vectors, without knowing how many clusters there should be in advance. In the past, I've been able to use cvKmeans2 to cluster, given knowledge of the number of clusters. I was trawling through the API and came across cv::flann::hierarchicalClustering. This looks like it will do what I need (namely, perform k-means, split clusters where necessary, iterate until splitting worsens the result), but I'm really struggling with the "index parameters".

I've figured out I need to create an index structure which goes in as the second parameter, but I'm getting an error from the following code:

cv::flann::Index fln_idx = cv::flann::KMeansIndexParams::createIndex( framePoints );

The error being:

../src/segmentation_1.cpp:592: error: cannot call member function ‘virtual flann::Index* cv::flann::KMeansIndexParams::createIndex(const cv::Mat&) const’ without object

framePoints is defined as below:

CvMat *framePoints = cvCreateMat( frameTracklets.size( ), 4, CV_32FC1 );

I'm fairly sure I'm doing something pretty stupid (my C++ knowledge is ok, but not great). I think I've posted all the relevant bits of code but if not, let me know and I'll post more.

Thanks in advance!

UPDATE

I've followed LumpN's advice and created a Kmeans object, using the following:

cv::Mat centres;
cv::flann::KMeansIndexParams fln_idx = cv::flann::KMeansIndexParams();
fln_idx.createIndex( framePoints );

int numClust;
numClust = hierarchicalClustering(framePoints, centres, fln_idx);

Now when I run it I get an error message from hierarchicalClustering() saying something like "the number of desired clusters should be >= 1" (I need to check when I get to work - I'll update then with the actual error). I assumed that the createIndex() gave it starting point, then hierarchicalClustering() split clusters until a good result was found (not sure if this is optimal or not). Do I need to call cv::flann::KMeansIndexParams() with some arguments? I've looked at the api and am thoroughly confused!
Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-11-03 19:17:01

“所需簇的数量必须至少为 1”。

所需的簇计数由 Centers.rows 确定。所以你必须首先调整中心的大小。
例如:

Mat centers (clusterCount,DESCRIPTOR_SIZE,cv_32FC1);
int count = cv::flann::hierarchicalClustering<cvflann::L2<float> >(descriptors,centers,cvflann::KMeansIndexParams(32,11,cvflann::FLANN_CENTERS_KMEANSPP));

"number of desired clusters must be at least 1".

Desired cluster count is determinated by centers.rows . So You have to resize centers first.
For Example:

Mat centers (clusterCount,DESCRIPTOR_SIZE,cv_32FC1);
int count = cv::flann::hierarchicalClustering<cvflann::L2<float> >(descriptors,centers,cvflann::KMeansIndexParams(32,11,cvflann::FLANN_CENTERS_KMEANSPP));
记忆里有你的影子 2024-11-03 19:17:01

您必须传递对createIndex 的引用,即createIndex(*framePoints)(注意星号!)。另一个错误可能是 createIndex 是一个非静态(成员)函数。在这种情况下,您必须创建一个 KMeansIndexParams 对象并对其调用 createIndex

You have to pass a reference to createIndex, i.e. createIndex(*framePoints) (note the asterix!). Another error might be createIndex being a non-static (member) function. In that case you'd have to create a KMeansIndexParams object and call createIndex on that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文