latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * This file is part of the LTI-Computer Vision Library (LTI-Lib) 00006 * 00007 * The LTI-Lib is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License (LGPL) 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * The LTI-Lib is distributed in the hope that it will be 00013 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00014 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with the LTI-Lib; see the file LICENSE. If 00019 * not, write to the Free Software Foundation, Inc., 59 Temple Place - 00020 * Suite 330, Boston, MA 02111-1307, USA. 00021 */ 00022 00023 00024 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiKMeansClustering.h 00027 * authors ....: Peter Doerfler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 05.10.2001 00030 * revisions ..: $Id: ltiKMeansClustering.h,v 1.7 2006/02/07 18:18:35 ltilib Exp $ 00031 */ 00032 00033 #include "ltiCentroidClustering.h" 00034 00035 00036 #ifndef _LTI_KMEANSCLUSTERING_H_ 00037 #define _LTI_KMEANSCLUSTERING_H_ 00038 00039 namespace lti { 00040 00041 /** 00042 * This class implements two versions of k-means-clustering: batch 00043 * and sequential. <p> Both methods are initailized by drawing 00044 * numberOfClusters training points and assigning them as 00045 * centroids. The batch version continues by labeling each training 00046 * point with the centroid it belongs to and then calculating new 00047 * centroids as mean average of the data belonging to each 00048 * cluster. This is continued until a convergence criterion is 00049 * met. <p> The sequential algorithm performs one step of batch 00050 * training. After that training points are analyzed in random 00051 * order. Whenever one data point switches clusters the centroids of 00052 * the old and new cluster of that data point are 00053 * recalculated. Calculation is also stoped by a convergence 00054 * criterion. <p> For both methods the convergence criterion is that 00055 * for one run through the training set none of the training points 00056 * switched clusters. 00057 */ 00058 class kMeansClustering : public centroidClustering { 00059 00060 public: 00061 00062 /** 00063 * Parameters for kMeansClustering. Only batch and sequential are allowed 00064 * as values for clusterMode. Only other parameter is numberOfClusters. 00065 */ 00066 class parameters : public centroidClustering::parameters { 00067 00068 public: 00069 /** 00070 * default constructor 00071 */ 00072 parameters(); 00073 00074 /** 00075 * copy constructor 00076 * @param other the parameters %object to be copied 00077 */ 00078 parameters(const parameters& other); 00079 00080 /** 00081 * destructor 00082 */ 00083 virtual ~parameters(); 00084 00085 /** 00086 * returns name of this type 00087 */ 00088 const char* getTypeName() const; 00089 00090 /** 00091 * copy the contents of a parameters %object 00092 * @param other the parameters %object to be copied 00093 * @return a reference to this parameters %object 00094 */ 00095 parameters& copy(const parameters& other); 00096 00097 /** 00098 * returns a pointer to a clone of the parameters 00099 */ 00100 virtual classifier::parameters* clone() const; 00101 00102 /** 00103 * write the parameters in the given ioHandler 00104 * @param handler the ioHandler to be used 00105 * @param complete if true (the default) the enclosing begin/end will 00106 * be also written, otherwise only the data block will be written. 00107 * @return true if write was successful 00108 */ 00109 virtual bool write(ioHandler& handler,const bool complete=true) const; 00110 00111 /** 00112 * read the parameters from the given ioHandler 00113 * @param handler the ioHandler to be used 00114 * @param complete if true (the default) the enclosing begin/end will 00115 * be also written, otherwise only the data block will be written. 00116 * @return true if write was successful 00117 */ 00118 virtual bool read(ioHandler& handler,const bool complete=true); 00119 00120 # ifdef _LTI_MSC_6 00121 /** 00122 * this function is required by MSVC only, as a workaround for a 00123 * very awful bug, which exists since MSVC V.4.0, and still by 00124 * V.6.0 with all bugfixes (so called "service packs") remains 00125 * there... This method is also public due to another bug, so please 00126 * NEVER EVER call this method directly: use read() instead 00127 */ 00128 bool readMS(ioHandler& handler,const bool complete=true); 00129 00130 /** 00131 * this function is required by MSVC only, as a workaround for a 00132 * very awful bug, which exists since MSVC V.4.0, and still by 00133 * V.6.0 with all bugfixes (so called "service packs") remains 00134 * there... This method is also public due to another bug, so please 00135 * NEVER EVER call this method directly: use write() instead 00136 */ 00137 bool writeMS(ioHandler& handler,const bool complete=true) const; 00138 # endif 00139 00140 /** the number of clusters (default 2)*/ 00141 int numberOfClusters; 00142 }; 00143 00144 00145 00146 /** 00147 * default constructor 00148 */ 00149 kMeansClustering(); 00150 00151 /** 00152 * copy constructor 00153 * @param other the %object to be copied 00154 */ 00155 kMeansClustering(const kMeansClustering& other); 00156 00157 /** 00158 * destructor 00159 */ 00160 virtual ~kMeansClustering(); 00161 00162 /** 00163 * returns the name of this type ("kMeansClustering") 00164 */ 00165 virtual const char* getTypeName() const; 00166 00167 /** 00168 * copy data of "other" functor. 00169 * @param other the functor to be copied 00170 * @return a reference to this functor %object 00171 */ 00172 kMeansClustering& copy(const kMeansClustering& other); 00173 00174 /** 00175 * returns a pointer to a clone of this classifier. 00176 */ 00177 virtual classifier* clone() const; 00178 00179 /** 00180 * returns used parameters 00181 */ 00182 const parameters& getParameters() const; 00183 00184 /** 00185 * Performs batch or sequential training according to 00186 * the value of parameters::clusterMode. 00187 * @param data the training points 00188 */ 00189 virtual bool train(const dmatrix& data); 00190 00191 /** calls centroidClustering::train(const dmatrix&, ivector&) */ 00192 virtual bool train(const dmatrix& input, 00193 ivector& ids); 00194 00195 protected: 00196 00197 /** 00198 * returns current parameters. (non const! -> protected) 00199 */ 00200 // parameters& getParameters() {return *params;}; 00201 00202 /** 00203 * Performs batch k-means clustering, see class description. 00204 */ 00205 bool trainBatch(const dmatrix& data); 00206 00207 /** 00208 * Performs sequential k-means clustering, see class description. 00209 */ 00210 bool trainSequential(const dmatrix& data); 00211 00212 }; 00213 00214 } 00215 00216 #endif