Tips on using hierarchicalClustering in OpenCV
2013-05-23 17:31 smyb000 阅读(2228) 评论(0) 收藏 举报Function hiererchicalClustering is an implementation of algorithm: “Scalable Recognition with a Vocabulary Tree”, the method clusters the given feature vectors by constructing a hierarchical k-means tree and choosing a cut in the tree that minimizes the cluster’s variance. It returns the number of clusters found.
Some tips need to be known when using this function:
1.DO Check whether you have correctly included the header file “opencv2/flann/flann.hpp”, it’s not included in “opencv2/opencv.hpp” of version 2.4.5, so we should manually modify the problem.
2. Note that the third parameter is const cvflann::KMeansIndexParams& params, not cv:flann. So define var. like this:
cvflann::KMeansIndexParams kmeans_param;
3. Make sure that the Mat & centers in
template<typename Distance> int flann::hierarchicalClustering(const Mat& features, Mat& centers, const cvflann::KMeansIndexParams& params, Distance d=Distance())
have the same rows as you desire to be.
For example, do this before clustering:
Mat centers(100,128,CV_32F);
4. Now you can successfully cluster data, two forms are available(assuming data type is float and using L2-Distance):
flann::hierarchicalClustering<float,float>(features, centers, kmeans_param);
or
flann::hierarchicalClustering<flann::L2<float>>(features, centers, kmeans_param);
it returns the number of clustering centers.
I have to emphasize that whatever the features type is, the centers type must be float according to dist.h:
template<typename T>
struct Accumulator { typedef T Type; };
template<>
struct Accumulator<unsigned char> { typedef float Type; };
template<>
struct Accumulator<unsigned short> { typedef float Type; };
template<>
struct Accumulator<unsigned int> { typedef float Type; };
template<>
struct Accumulator<char> { typedef float Type; };
template<>
struct Accumulator<short> { typedef float Type; };
template<>
struct Accumulator<int> { typedef float Type; };
5. Don’t forget to remove the last redundant rows because the centers are sometimes(maybe always
) less than you desire. As the function returns the actual numbers of centers, it’s easy to delete undesired data .
Enjoy it!
Reference:
1. D. Nistér and H. Stewénius. Scalable recognition with a vocabulary tree. In IEEE Conference on Computer Vision and Pattern Recognition (CVPR), volume 2, pages 2161-2168, June 2006. oral presentation.
2. OpenCV online documentation-flann::hierarchicalClustering<Distance>.
Some tips on using hierarchicalClustering in OpenCV 2.4.5
浙公网安备 33010602011771号