1 | initial version |
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));