void Compute(const ImageRGB<byte>& image, const ImageHSV<byte>& imagehsv) { const int& w = image.GetWidth(); const int& h = image.GetHeight(); label_map.Resize(h, w); // Compute pixel-wise features pixel_ftrs.Compute(image, imagehsv); // Run K-means to generate textons vector<VecD> points; const vector<VecD>& ftrs = pixel_ftrs.features; random_sample_n(ftrs.begin(), ftrs.end(), back_inserter(points), 5000); VecI labels(points.size()); KMeans::Estimate(points, 20, textons, labels); // Label the pixels for (int r = 0; r < h; r++) { for (int c = 0; c < w; c++) { const VecD& ftr = (*pixel_ftrs.feature_map)(r, c); double mindist = INFINITY; for (int i = 0; i < textons.size(); i++) { const double dist = VectorSSD(textons[i], ftr); if (dist < mindist) { mindist = dist; label_map[r][c] = i; } } } } }
void Pointcloud::subSampleRandom(unsigned int num_samples, Pointcloud& sample_cloud) { point3d_collection samples; // visual studio does not support random_sample_n #ifdef _MSC_VER samples.reserve(this->size()); samples.insert(samples.end(), this->begin(), this->end()); std::random_shuffle(samples.begin(), samples.end()); samples.resize(num_samples); #else random_sample_n(begin(), end(), std::back_insert_iterator<point3d_collection>(samples), num_samples); for (unsigned int i=0; i<samples.size(); i++) { sample_cloud.push_back(samples[i]); } #endif }
void random_samples(list<string> g,int K, list<string> &sol) { //vector<string> gg(all(g)); vector<string> gg(all(g)); random_shuffle(all(gg)); vector<string> s(K); srand((unsigned)time(0)); random_sample_n(all(gg),s.begin(),K); //random_shuffle(all(s)); for(int i = 0; i < K; i++) { sol.push_back(s[i]); } }
inline OutputIter random_sample_n(InputIter first, InputIter last, OutputIter out, Size n) { std::random_device rd; std::mt19937 rand{rd()}; return random_sample_n(first, last, out, n, rand); } // random_sample_n
inline OutputIter random_sample_n(InputIter first, InputIter last, OutputIter out, Size n) { std::mt19937 rand; rand.seed(std::time(0)); return random_sample_n(first, last, out, n, rand); } // random_sample_n