PICCANTE  0.4
The hottest HDR imaging library!
k_means_rand.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_K_MEANS_RAND_HPP
19 #define PIC_UTIL_K_MEANS_RAND_HPP
20 
21 #include <vector>
22 #include <set>
23 #include <chrono>
24 
25 #include "../base.hpp"
26 #include "../util/array.hpp"
27 #include "../util/math.hpp"
28 #include "../util/std_util.hpp"
29 #include "../util/k_means.hpp"
30 
31 namespace pic{
32 
33 template<class T>
34 class KMeansRand: public KMeans<T>
35 {
36 protected:
37 
38  T* initCenters(T *samples, int nSamples, int nDim, T* centers)
39  {
40  std::mt19937 m(42);
41  if(centers == NULL) {
42  centers = new T[this->k * nDim];
43  }
44 
45  std::set< uint > chosen;
46  for(uint i = 0; i < this->k; i++) {
47 
48  bool bCheck = true;
49  while(bCheck) {
50  uint index = m() % nSamples;
51  if(chosen.find(index) == chosen.end()) {
52 
53  chosen.insert(index);
54  Array<T>::assign(&samples[index * nDim], nDim, &centers[i * nDim]);
55  bCheck = false;
56  }
57  }
58  }
59  return centers;
60  }
61 
62 public:
63 
65  {
66  }
67 
68  static T* execute(T *samples, int nSamples, int nDim,
69  T* centers, int k,
70  std::vector< std::set<uint> *> &labels,
71  uint maxIter = 100)
72  {
73 
74  KMeansRand<T> km(k, maxIter);
75 
76  return km.Process(samples, nSamples, nDim, centers, labels);
77  }
78 };
79 
80 } //end namespace pic
81 
82 #endif // PIC_UTIL_K_MEANS_RAND_HPP
unsigned int uint
Definition: base.hpp:23
uint k
Definition: k_means.hpp:114
uint maxIter
Definition: k_means.hpp:114
T * initCenters(T *samples, int nSamples, int nDim, T *centers)
Definition: k_means_rand.hpp:38
T * Process(T *samples, int nSamples, int nDim, T *centers, std::vector< std::set< uint > *> &labels)
Definition: k_means.hpp:128
Definition: k_means.hpp:33
Definition: k_means_rand.hpp:34
static T * execute(T *samples, int nSamples, int nDim, T *centers, int k, std::vector< std::set< uint > *> &labels, uint maxIter=100)
Definition: k_means_rand.hpp:68
Definition: bilateral_separation.hpp:25
static T * assign(T *data, int size, T *ret)
assign
Definition: array.hpp:464
KMeansRand(uint k, uint maxIter)
Definition: k_means_rand.hpp:64