Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLMath/src/ICLMath/KMeans.h ** 00010 ** Module : ICLMath ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/Random.h> 00034 #include <algorithm> 00035 00036 namespace icl{ 00037 namespace math{ 00038 00039 00041 00055 template<class Vector, class Scalar> 00056 class KMeans{ 00057 00059 std::vector<Vector> m_centers; 00060 00062 std::vector<Vector> m_means; 00063 00065 std::vector<int> m_nums; 00066 00068 std::vector<Scalar> m_errors; 00069 00071 static Scalar diff_power_two(const Scalar &a, const Scalar &b){ 00072 Scalar d = a-b; 00073 return d*d; 00074 } 00075 00077 static inline Scalar dist(const Vector &a, const Vector &b){ 00078 return ::sqrt( std::inner_product(a.begin(),a.end(),b.begin(),Scalar(0), std::plus<Scalar>(), diff_power_two) ); 00079 } 00080 00082 static inline void setVectorNull(Vector &v){ 00083 std::fill(v.begin(),v.end(),Scalar(0)); 00084 } 00085 00086 public: 00087 00089 struct Result{ 00090 const std::vector<Vector> ¢ers; 00091 const std::vector<int> &nums; 00092 const std::vector<Scalar> &errors; 00093 }; 00094 00096 inline KMeans(int numCenters=0){ 00097 init(numCenters); 00098 } 00099 00101 inline void init(int numCenters){ 00102 m_centers.resize(numCenters); 00103 m_means.resize(numCenters); 00104 m_nums.resize(numCenters); 00105 m_errors.resize(numCenters); 00106 } 00107 00109 inline int findNN(const Vector &v, Scalar &minDist){ 00110 int minIdx = 0; 00111 minDist = dist(v,m_centers[0]); 00112 for(size_t i=1;i<m_centers.size();++i){ 00113 Scalar currDist = dist(v,m_centers[i]); 00114 if(currDist < minDist){ 00115 minDist = currDist; 00116 minIdx = i; 00117 } 00118 } 00119 return minIdx; 00120 } 00121 00123 00125 template<class RandomAcessIterator> 00126 Result apply(RandomAcessIterator begin, RandomAcessIterator end, int numSteps = 1000, bool reinitCenters=true){ 00127 Scalar minDist = 0; 00128 00129 if(reinitCenters){ 00130 URandI r((int)(end-begin)-1); 00131 for(size_t i=0;i<m_centers.size();++i){ 00132 int ri = r; 00133 m_centers[i] = *(begin+ri); 00134 } 00135 } 00136 00137 for(int step=0;step<numSteps;++step){ 00138 // empty accumulators 00139 for(size_t i=0;i<m_means.size();++i){ 00140 setVectorNull(m_means[i]); 00141 m_nums[i] = Scalar(0); 00142 m_errors[i] = Scalar(0); 00143 } 00144 00145 // estimate means 00146 for(RandomAcessIterator it=begin;it != end; ++it){ 00147 const int nn = findNN(*it,minDist); 00148 m_means[nn] += *it; 00149 m_nums[nn] ++; 00150 m_errors[nn] += minDist; 00151 } 00152 00153 for(size_t i=0;i<m_means.size();++i){ 00154 if(m_nums[i]){ 00155 m_centers[i] = m_means[i] * (Scalar(1.0)/m_nums[i]); 00156 m_errors[i] /= m_nums[i]; 00157 }// empty voronoi tessels are not moved 00158 } 00159 } 00160 00161 Result r= { m_centers, m_nums, m_errors }; 00162 return r; 00163 } 00164 }; 00165 00167 template<> 00168 float KMeans<Point32f,float>::dist(const Point32f &a, const Point32f &b){ 00169 return a.distanceTo(b); 00170 } 00171 00172 template<> 00173 void KMeans<Point32f,float>::setVectorNull(Point32f &p){ 00174 p = Point32f::null; 00175 } 00179 } 00180 }