latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 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-Lib: Image Processing and Computer Vision Library 00026 * file .......: ltiAdaptiveKMeans.h 00027 * authors ....: Jens Paustenbach 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 28.2.2002 00030 * revisions ..: $Id: ltiAdaptiveKMeans.h,v 1.6 2006/02/07 18:11:26 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_ADAPTIVE_K_MEANS_H_ 00034 #define _LTI_ADAPTIVE_K_MEANS_H_ 00035 00036 #include "ltiVector.h" 00037 #include "ltiMatrix.h" 00038 #include "ltiCentroidClustering.h" 00039 00040 namespace lti { 00041 00042 /** 00043 * this class implements a k-means clustering that determines the number of 00044 * clusters by itself <p> The algorithm is based on Kothari R., Pitts D., 00045 * 1998. On finding the number of clusters. Pattern Recognition Letters 20, 00046 * 405-416. <p> The algorithm determines the number of cluster in the given 00047 * data and performs a k means clustering the the detected number of 00048 * clusters on it. When the algorithm is executed, is does a lot of k means 00049 * clusterings, with an additional term that brings the cluster centers 00050 * together. Clusters that have too few points are deleted and clusters 00051 * that are two close to each other are combined, according to the actual 00052 * neighborhood. The neighborhood is varied between the values of the 00053 * parameters minNeighborhood and maxNeighborhood, in as many steps as 00054 * parameter nbNeighborhoods. If parameter detectNeighborhood is true, 00055 * the paramters minNeighborhood and maxNeighborhood are set by the 00056 * algorithm.The Parameter minNumberOfPoints is the minimal number of 00057 * points that must belong to a cluster, otherwise the cluster will be 00058 * deleted. maxIterations is the maximum number of Iterations before the 00059 * clusters are viewed to be stable. maxDistance is the maximal distance 00060 * that each component of the centroids can move before they are viewed as 00061 * stable. If every component moves in one iteration less than this 00062 * parameter, the centroids are set to be stable. 00063 * The Parameter nbClusters is the number of clusters the 00064 * algorithm starts with. It has to be bigger than the real number 00065 * clusters. If the learnRate is too big, so that the centroids don't 00066 * converge, the algorithm halves the learn rate, restarts and sets a 00067 * statusString. 00068 */ 00069 class adaptiveKMeans : public centroidClustering { 00070 public: 00071 /** 00072 * the parameters for the class adaptiveKMeans 00073 */ 00074 class parameters : public centroidClustering::parameters { 00075 public: 00076 /** 00077 * default constructor 00078 */ 00079 parameters(); 00080 00081 /** 00082 * copy constructor 00083 * @param other the parameters object to be copied 00084 */ 00085 parameters(const parameters& other); 00086 00087 /** 00088 * destructor 00089 */ 00090 virtual ~parameters(); 00091 00092 /** 00093 * returns name of this type 00094 */ 00095 const char* getTypeName() const; 00096 00097 /** 00098 * copy the contents of a parameters object 00099 * @param other the parameters object to be copied 00100 * @return a reference to this parameters object 00101 */ 00102 parameters& copy(const parameters& other); 00103 00104 /** 00105 * copy the contents of a parameters object 00106 * @param other the parameters object to be copied 00107 * @return a reference to this parameters object 00108 */ 00109 parameters& operator=(const parameters& other); 00110 00111 00112 /** 00113 * returns a pointer to a clone of the parameters 00114 */ 00115 virtual classifier::parameters* clone() const; 00116 00117 /** 00118 * write the parameters in the given ioHandler 00119 * @param handler the ioHandler to be used 00120 * @param complete if true (the default) the enclosing begin/end will 00121 * be also written, otherwise only the data block will be written. 00122 * @return true if write was successful 00123 */ 00124 virtual bool write(ioHandler& handler,const bool complete=true) const; 00125 00126 /** 00127 * read the parameters from the given ioHandler 00128 * @param handler the ioHandler to be used 00129 * @param complete if true (the default) the enclosing begin/end will 00130 * be also written, otherwise only the data block will be written. 00131 * @return true if write was successful 00132 */ 00133 virtual bool read(ioHandler& handler,const bool complete=true); 00134 00135 # ifdef _LTI_MSC_6 00136 /** 00137 * this function is required by MSVC only, as a workaround for a 00138 * very awful bug, which exists since MSVC V.4.0, and still by 00139 * V.6.0 with all bugfixes (so called "service packs") remains 00140 * there... This method is also public due to another bug, so please 00141 * NEVER EVER call this method directly: use read() instead 00142 */ 00143 bool readMS(ioHandler& handler,const bool complete=true); 00144 00145 /** 00146 * this function is required by MSVC only, as a workaround for a 00147 * very awful bug, which exists since MSVC V.4.0, and still by 00148 * V.6.0 with all bugfixes (so called "service packs") remains 00149 * there... This method is also public due to another bug, so please 00150 * NEVER EVER call this method directly: use write() instead 00151 */ 00152 bool writeMS(ioHandler& handler,const bool complete=true) const; 00153 # endif 00154 00155 // ------------------------------------------------ 00156 // the parameters 00157 // ------------------------------------------------ 00158 00159 00160 /** 00161 * minimum neighborhood 00162 */ 00163 double minNeighborhood; 00164 00165 /** 00166 * maximum neighborhood 00167 */ 00168 double maxNeighborhood; 00169 00170 /** 00171 * the learn rate 00172 */ 00173 double learnRate; 00174 00175 /** 00176 * the increment of the neighborhood 00177 */ 00178 int nbNeighborhoods; 00179 00180 /** 00181 * minimum number of points in one cluster. If a cluster has less than 00182 * this the cluster will be deleted 00183 */ 00184 double minNumberOfPoints; 00185 00186 /** 00187 * number of clusters to start with 00188 */ 00189 int nbClusters; 00190 00191 /** 00192 * if true the parameters minNeighborhood, maxNeighborhood, increment 00193 * and maxDistance will be automaticly be detected 00194 */ 00195 bool detectNeighborhood; 00196 00197 /** 00198 * maximum number of iterations before the next neighborhood considered 00199 */ 00200 int maxIterations; 00201 00202 /** 00203 * if every component of the centroids moves less than this parameter 00204 * the centroids are stable 00205 */ 00206 double maxDistance; 00207 00208 }; 00209 00210 /** 00211 * default constructor 00212 */ 00213 adaptiveKMeans(); 00214 00215 /** 00216 * copy constructor 00217 * @param other the object to be copied 00218 */ 00219 adaptiveKMeans(const adaptiveKMeans& other); 00220 00221 /** 00222 * destructor 00223 */ 00224 virtual ~adaptiveKMeans(); 00225 00226 /** 00227 * returns the name of this type ("adaptiveKMeans") 00228 */ 00229 virtual const char* getTypeName() const; 00230 00231 /** 00232 * copy data of "other" clustering. 00233 * @param other the clustering to be copied 00234 * @return a reference to this clustering object 00235 */ 00236 adaptiveKMeans& copy(const adaptiveKMeans& other); 00237 00238 /** 00239 * alias for copy member 00240 * @param other the clustering to be copied 00241 * @return a reference to this clustering object 00242 */ 00243 adaptiveKMeans& operator=(const adaptiveKMeans& other); 00244 00245 /** 00246 * returns a pointer to a clone of this clustering. 00247 */ 00248 virtual classifier* clone() const; 00249 00250 /** 00251 * returns used parameters 00252 */ 00253 const parameters& getParameters() const; 00254 00255 /** 00256 * Unsupervised training. The vectors in the <code>input</code> 00257 * matrix will be put into groups according to the training 00258 * algorithm. Additionally, an integer indicating the class each 00259 * point belongs to is returned. <p> By default this method uses 00260 * the other train method and then 00261 * calls classify(const dvector&) to get the ids for each 00262 * trainvector. These ids are then returned. 00263 * @param input the matrix with the input vectors (each row is a training 00264 * vector) 00265 * @param ids vector of class ids for each input point 00266 * @return true if successful, false otherwise. (if false you can check 00267 * the error message with getStatusString()) 00268 */ 00269 virtual bool train(const dmatrix& input, 00270 ivector& ids); 00271 00272 /** 00273 * Unsupervised training. 00274 * The vectors in the <code>input</code> matrix 00275 * will be clustered using each specific method. 00276 * @param input the matrix with the input vectors (each row is a training 00277 * vector) 00278 * @return true if successful, false otherwise. (if false you can check 00279 * the error message with getStatusString()) 00280 */ 00281 virtual bool train(const dmatrix& input); 00282 00283 //TODO Check whether you really need a new classify method. 00284 // In some cases the superclasses method will suffice. Then just 00285 // delete the declaration and its implementation stump. 00286 00287 /* 00288 * Classification. 00289 * Classifies the feature and returns the outputVector with 00290 * the classification result. 00291 * @param feature the %vector to be classified 00292 * @param result the result of the classification 00293 * @return false if an error occurred during classification else true 00294 */ 00295 // virtual bool 00296 // classify(const dvector& feature, outputVector& result) const; 00297 00298 virtual const ivector& getClusterSpreading() const; 00299 00300 protected: 00301 ivector clusterSpreading; 00302 }; 00303 } 00304 00305 #endif