Ask Your Question
1

Hierarchical Clustering with Flann

asked 2013-03-02 15:44:19 -0600

Andrea gravatar image

I have a problem, I need to clusterize a set of points in an unknown number of clusters. I use cv::flann::hierarchicalClustering. Someone that use this function could help me? Another question is about the return value that should be the number of clusters, but how could I understand which are the cluster? I created a cv::Mat points with dimensions n x 2 where n is the number of points.

Thanks in advance.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2013-03-03 05:40:06 -0600

Guanta gravatar image

I hope this commented (not tested) code will give you the right ideas how it works:

// create the kmeans parameters structure: branching factor = 32, 
// number of iterations = 100, choose initial centers by PP-algorithm
cv::flann::KMeansIndexParams kmean_params(32, 100, cv::flann::FLANN_CENTERS_KMEANSPP);
// create matrix of samples, this matrix you have to fill with your actual points
cv::Mat1f samples( number_of_points, 2 );

// ... // fill 'samples'-Matrix with your points

// create matrix of centers, the specified rows will be kinda the upper limit of clusters
// you'll get
cv::Mat1f centers( number_of_centers, 2 );
// apply hierarchical clustering which returns the true numbers of clusters ( < centers.rows )
true_number_clusters = cv::flann::hierarchicalClustering<cv::L2<float> >(samples, centers, kmean_params );
// since you get less clusters than you specified we can also truncate our matrix. 
centers = centers.rowRange(cv::Range(0,true_number_clusters));
edit flag offensive delete link more

Comments

This code does not work.How can I use it?

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-07-31 03:14:48 -0600 )edit

Only cause it isn't working for you doesn't mean it doesn't work in general! It works perfectly fine for me, what is your error message?

Guanta gravatar imageGuanta ( 2013-07-31 08:43:04 -0600 )edit

I solve it in http://answers.opencv.org/question/17849/how-can-i-use-hierarchicalclustering/ post.

error : 'int cv::flann::hierarchicalClustering<cv::L2<T>>(const cv::Mat &,cv::Mat &,const cvflann::KMeansIndexParams &,Distance)' : cannot convert parameter 3 from 'cv::flann::KMeansIndexParams' to 'const cvflann::KMeansIndexParams &'

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-07-31 09:30:54 -0600 )edit

1 plus your example helped me. I accept default parameters but it returns only one cluster.What it reason?

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-07-31 23:53:14 -0600 )edit

Maybe you use too few rows for your centers-matrix.

Guanta gravatar imageGuanta ( 2013-08-01 02:51:48 -0600 )edit

I have 500 rows for center.I changed the branching form 32 to 10 then the numbers of the clusters increased. To obtain the clusterID for each rows we need to compute manual cluster id according to distance form nearest center?

Mostafa Sataki gravatar imageMostafa Sataki ( 2013-08-01 03:51:56 -0600 )edit

Seems so, pitty that this information isn't provided as well.

Guanta gravatar imageGuanta ( 2013-08-01 05:16:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-03-02 15:44:19 -0600

Seen: 6,275 times

Last updated: Mar 03 '13