开发者

Hierarchical k-Means in OpenCV without knowledge of "k"

开发者 https://www.devze.com 2023-02-20 12:45 出处:网络
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 clu

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 g开发者_开发技巧reat). 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!


"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));


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号