iRoCS Toolbox  1.1.0
kmeans.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Margret Keuper, Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 #ifndef LIBMARGRET_SRC_KMEANS_HH
24 #define LIBMARGRET_SRC_KMEANS_HH
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.hh>
28 #endif
29 
30 #include <vector>
31 #include <blitz/array.h>
32 
33 namespace segmentation
34 {
35 
36 
37 template<class DataT>
38 class _KMeans
39 {
40  typedef typename blitz::Array<DataT, 1> PointT;
41  typedef typename blitz::Array<DataT, 2> ArrayPointT;
42 public:
43  struct Distance
44  {
45  virtual ~Distance();
46  virtual double operator()(const PointT &p1, const PointT &p2) const = 0;
47  };
48  struct EuclideanDistance : public Distance
49  {
50  virtual double operator()(const PointT &p1, const PointT &p2) const;
51  };
52 
53  _KMeans(const unsigned int k, const Distance &distance);
54  _KMeans(const unsigned int k);
55  virtual ~_KMeans();
56  double cluster(const ArrayPointT &points,
57  ArrayPointT &means,
58  std::vector<int> &labels);
59 
64  void setMaxRestarts(int max_restarts)
65  {
66  max_restarts_ = max_restarts;
67  };
72  void setMaxIterations(int max_iterations)
73  {
74  max_iterations_ = max_iterations;
75  };
80  void setMaxIterations(double threshold)
81  {
82  variance_threshold_ = threshold;
83  };
87  void setRestartStopThreshold(int iter)
88  {
89  variance_iter_no_improvement_ = iter;
90  };
91 private:
92  /*===========================================================================*/
101  /*===========================================================================*/
102  void iterateUntilConvergence();
103  void initializeMeans();
104  bool assignLabels();
105  void updateMeans();
106  double totalVariance();
107 
108  unsigned int k_;
109  ptrdiff_t num_points_;
110  ptrdiff_t point_dim_;
112  int max_iterations_;
114  int max_restarts_;
115  double variance_threshold_;
117  int variance_iter_no_improvement_;
118  const Distance* distance_;
119  ArrayPointT points_;
120  ArrayPointT means_;
121  std::vector<int> labels_;
122  blitz::Range all;
123 
124  //upper and lower boundary of points
125  //PointT ubound_;
126  //PointT lbound_;
127 
128 
129 };
130 
131 #define KMeans _KMeans
132 
133 
134 template<class DataT, int Dim>
135 double kmeans(const unsigned int k,
136  const blitz::Array<DataT, 2 > &points,
137  blitz::Array<DataT, 2 > &means,
138  std::vector<int> &labels
139  );
140 }
141 
142 //#include "kmeans-inl.hh"
143 #endif
void setMaxRestarts(int max_restarts)
set maximum number of restarts to perform for variance minimization
Definition: kmeans.hh:64
void setRestartStopThreshold(int iter)
stop restarts if variance didn&#39;t drop for iter iterations
Definition: kmeans.hh:87
double cluster(const ArrayPointT &points, ArrayPointT &means, std::vector< int > &labels)
Definition: kmeans-inl.hh:74
_KMeans(const unsigned int k, const Distance &distance)
double distance(BSpline< double > const &spline, double x, double &u)
Compute the distance and the corresponding curve position of the given point to the spline...
virtual double operator()(const PointT &p1, const PointT &p2) const =0
void setMaxIterations(double threshold)
Set variance threshold.
Definition: kmeans.hh:80
void setMaxIterations(int max_iterations)
set maximum number of iterations
Definition: kmeans.hh:72
double kmeans(const unsigned int k, const blitz::Array< DataT, 2 > &points, blitz::Array< DataT, 2 > &means, std::vector< int > &labels)
Definition: kmeans-inl.hh:216