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 .......: ltiSOFM2D.h 00027 * authors ....: Peter Doerfler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 20.8.2002 00030 * revisions ..: $Id: ltiSOFM2D.h,v 1.6 2006/02/07 18:24:07 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_S_O_F_M2_D_H_ 00034 #define _LTI_S_O_F_M2_D_H_ 00035 00036 #include "ltiSOFM.h" 00037 #include "ltiLinearKernels.h" 00038 //#include "ltiImage.h" 00039 00040 namespace lti { 00041 /** 00042 * This is a Self-Organizing Feature Map with a 2-D grid of neurons. 00043 */ 00044 class SOFM2D : public SOFM { 00045 public: 00046 /** 00047 * the parameters for the class SOFM2D 00048 */ 00049 class parameters : public SOFM::parameters { 00050 public: 00051 /** 00052 * default constructor 00053 */ 00054 parameters(); 00055 00056 /** 00057 * copy constructor 00058 * @param other the parameters object to be copied 00059 */ 00060 parameters(const parameters& other); 00061 00062 /** 00063 * destructor 00064 */ 00065 virtual ~parameters(); 00066 00067 /** 00068 * returns name of this type 00069 */ 00070 const char* getTypeName() const; 00071 00072 /** 00073 * copy the contents of a parameters object 00074 * @param other the parameters object to be copied 00075 * @return a reference to this parameters object 00076 */ 00077 parameters& copy(const parameters& other); 00078 00079 /** 00080 * copy the contents of a parameters object 00081 * @param other the parameters object to be copied 00082 * @return a reference to this parameters object 00083 */ 00084 parameters& operator=(const parameters& other); 00085 00086 00087 /** 00088 * returns a pointer to a clone of the parameters 00089 */ 00090 virtual classifier::parameters* clone() const; 00091 00092 /** 00093 * write the parameters in the given ioHandler 00094 * @param handler the ioHandler to be used 00095 * @param complete if true (the default) the enclosing begin/end will 00096 * be also written, otherwise only the data block will be written. 00097 * @return true if write was successful 00098 */ 00099 virtual bool write(ioHandler& handler,const bool complete=true) const; 00100 00101 /** 00102 * read the parameters from the given ioHandler 00103 * @param handler the ioHandler to be used 00104 * @param complete if true (the default) the enclosing begin/end will 00105 * be also written, otherwise only the data block will be written. 00106 * @return true if write was successful 00107 */ 00108 virtual bool read(ioHandler& handler,const bool complete=true); 00109 00110 # ifdef _LTI_MSC_6 00111 /** 00112 * this function is required by MSVC only, as a workaround for a 00113 * very awful bug, which exists since MSVC V.4.0, and still by 00114 * V.6.0 with all bugfixes (so called "service packs") remains 00115 * there... This method is also public due to another bug, so please 00116 * NEVER EVER call this method directly: use read() instead 00117 */ 00118 bool readMS(ioHandler& handler,const bool complete=true); 00119 00120 /** 00121 * this function is required by MSVC only, as a workaround for a 00122 * very awful bug, which exists since MSVC V.4.0, and still by 00123 * V.6.0 with all bugfixes (so called "service packs") remains 00124 * there... This method is also public due to another bug, so please 00125 * NEVER EVER call this method directly: use write() instead 00126 */ 00127 bool writeMS(ioHandler& handler,const bool complete=true) const; 00128 # endif 00129 00130 // ------------------------------------------------ 00131 // the parameters 00132 // ------------------------------------------------ 00133 00134 /** 00135 * The dimensions of the grid can be given explicitely 00136 * (calculateSize=false) by setting sizeX and sizeY or 00137 * calculated from the distribution of the training points. In 00138 * the latter case, the two highest eigenvalues are calculated 00139 * and multiplied by factor that after rounding the error to the 00140 * given area is as small as possible. This method yields better 00141 * unfolding of the grid at the cost of more calculations and 00142 * possibly failure due to singularity of the covariance matrix 00143 * of the training samples.<p> 00144 * The default is false. 00145 */ 00146 bool calculateSize; 00147 00148 /** 00149 * size of the grid of neurons in the first dimension. Default 0. 00150 */ 00151 int sizeX; 00152 00153 /** 00154 * size of the grid of neurons in the second dimension. Default 0. 00155 */ 00156 int sizeY; 00157 00158 /** 00159 * approximate area of the grid when the dimension's sizes are 00160 * calculated from the distribution of training points. Default 0. 00161 */ 00162 int area; 00163 00164 }; 00165 00166 /** 00167 * default constructor 00168 */ 00169 SOFM2D(); 00170 00171 /** 00172 * copy constructor 00173 * @param other the object to be copied 00174 */ 00175 SOFM2D(const SOFM2D& other); 00176 00177 /** 00178 * destructor 00179 */ 00180 virtual ~SOFM2D(); 00181 00182 /** 00183 * returns the name of this type ("SOFM2D") 00184 */ 00185 virtual const char* getTypeName() const; 00186 00187 /** 00188 * copy data of "other" classifier. 00189 * @param other the classifier to be copied 00190 * @return a reference to this classifier object 00191 */ 00192 SOFM2D& copy(const SOFM2D& other); 00193 00194 /** 00195 * alias for copy member 00196 * @param other the classifier to be copied 00197 * @return a reference to this classifier object 00198 */ 00199 SOFM2D& operator=(const SOFM2D& other); 00200 00201 /** 00202 * returns a pointer to a clone of this classifier. 00203 */ 00204 virtual classifier* clone() const; 00205 00206 /** 00207 * returns used parameters 00208 */ 00209 const parameters& getParameters() const; 00210 00211 /** 00212 * Unsupervised training. The vectors in the <code>input</code> 00213 * matrix will be put into groups according to the training 00214 * algorithm. Additionally, an integer indicating the class each 00215 * point belongs to is returned. <p> By default this method uses 00216 * the other train method train(const dmatrix&) and then 00217 * calls classify(const dvector&) to get the ids for each 00218 * trainvector. These ids are then returned. 00219 * @param input the matrix with the input vectors (each row is a training 00220 * vector) 00221 * @param ids vector of class ids for each input point 00222 * @return true if successful, false otherwise. (if false you can check 00223 * the error message with getStatusString()) 00224 */ 00225 virtual bool train(const dmatrix& input, 00226 ivector& ids); 00227 00228 /** 00229 * Unsupervised training. 00230 * The row vectors in the <code>input</code> matrix 00231 * are used for training of the classifier. 00232 * @param input the matrix with the input vectors (each row is a training 00233 * vector) 00234 * @return true if successful, false otherwise. (if false you can check 00235 * the error message with getStatusString()) 00236 */ 00237 virtual bool train(const dmatrix& input); 00238 00239 //TODO Check whether you really need a new classify method. 00240 // In some cases the superclasses method will suffice. Then just 00241 // delete the declaration and its implementation stump. 00242 00243 /** 00244 * Classification. 00245 * Classifies the feature and returns the outputVector with 00246 * the classification result. 00247 * @param feature the %vector to be classified 00248 * @param result the result of the classification 00249 * @return false if an error occurred during classification else true 00250 */ 00251 virtual bool 00252 classify(const dvector& feature, outputVector& result) const; 00253 00254 /** 00255 * returns the size (x,y) of the feature map. 00256 */ 00257 inline point size() const { 00258 point p(sizeX,sizeY); 00259 return p; 00260 }; 00261 00262 /** 00263 * Returns the size of the map in x-direction (number of columns) 00264 */ 00265 inline int xSize() const {return sizeX;}; 00266 00267 /** 00268 * Returns the size of the map in y-direction (number of rows) 00269 */ 00270 inline int ySize() const {return sizeY;}; 00271 00272 /** 00273 * write the SOFM2D in the given ioHandler 00274 * @param handler the ioHandler to be used 00275 * @param complete if true (the default) the enclosing begin/end will 00276 * be also written, otherwise only the data block will be written. 00277 * @return true if write was successful 00278 */ 00279 virtual bool write(ioHandler& handler,const bool complete=true) const; 00280 00281 /** 00282 * read the SOFM2D from the given ioHandler 00283 * @param handler the ioHandler to be used 00284 * @param complete if true (the default) the enclosing begin/end will 00285 * be also written, otherwise only the data block will be written. 00286 * @return true if write was successful 00287 */ 00288 virtual bool read(ioHandler& handler,const bool complete=true); 00289 00290 00291 protected: 00292 00293 /** size of the first dimension */ 00294 int sizeX; 00295 /** size of the second dimension */ 00296 int sizeY; 00297 00298 /** the highest eigenvalue of the train data */ 00299 double eva1; 00300 /** the second highest eigenvalue of the train data */ 00301 double eva2; 00302 /** the the eigenvector corresponding to eva1 */ 00303 dvector eve1; 00304 /** the the eigenvector corresponding to eva2 */ 00305 dvector eve2; 00306 00307 /** calculate sizeX and sizeY in case of param.calulateSize is true */ 00308 bool calcSize(const dmatrix& data); 00309 00310 /** initializes the grid according to parameters.initType */ 00311 bool initGrid(const dmatrix& data); 00312 00313 /** calculate the neigborhood kernel */ 00314 void getNeighborhoodKernel(const int& maxN, kernel2D<double>& facN); 00315 00316 /** train the SOFM using L1- or L2-Norm */ 00317 bool trainDist(const dmatrix& data); 00318 00319 /** train the SOFM using dot product */ 00320 bool trainDot(const dmatrix& data); 00321 }; 00322 } 00323 00324 #endif