void generate_random_clusters( const std::vector<sample_type>& samples) { typedef boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random_type; random_type random_( random_generator_, boost::uniform_int<>(0, samples.size() - 1)); for( int i = 0; i < K_; ++i) { while( 1) { int indx = random_(); if( labels_[indx] == 0) { clusters_.push_back( cluster_t( samples[indx])); labels_[indx] = &(clusters_.back()); break; } } } }
void clusterHistograms(const std::vector<Mat> &hists, std::vector<cluster_t> &clusters, const cluster_vars &vars) { // calculate hist distances Mat dists = Mat::zeros((int)hists.size(), (int)hists.size(), CV_64FC1); tbb::parallel_for(0, (int)hists.size()-1, 1, [&hists, &dists](int i) { tbb::parallel_for(i, (int)hists.size(), 1, [&hists, &dists, &i](int j) { double dist = compareHist(hists.at(i), hists.at(j), COMP_ALG); dists.at<double>(i, j) = dist; dists.at<double>(j, i) = dist; } ); } ); // find optimal clusters std::vector<idx_cluster_t> idxClusters; find_optimal_clustering(dists, idxClusters, vars); // calculate the cluster average for each cluster for(size_t i = 0; i < idxClusters.size(); i++) { idx_cluster_t cluster = idxClusters.at((int)i); std::vector<Mat> clusterHists; Mat clusterAvg; for(size_t i = 0; i < cluster.size(); i++) { //for(idx_cluster_t::const_iterator it = cluster.begin(); it != cluster.end(); it++) { clusterHists.push_back(hists.at(cluster.at(i))); } averageHistograms(clusterHists, clusterAvg); clusters.push_back(cluster_t(clusterAvg, cluster)); } }