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 * project ....: LTI Digital Image/Signal Processing Library 00025 * file .......: ltiSHClassifier.h 00026 * authors ....: Jochen Wickel 00027 * organization: LTI, RWTH Aachen 00028 * creation ...: 30.10.2001 00029 * revisions ..: $Id: ltiSHClassifier.h,v 1.7 2006/02/07 18:23:32 ltilib Exp $ 00030 */ 00031 00032 #ifndef _LTI_SHCLASSIFIER_H_ 00033 #define _LTI_SHCLASSIFIER_H_ 00034 00035 #include "ltiSupervisedInstanceClassifier.h" 00036 #include "ltiSparseHistogram.h" 00037 00038 namespace lti { 00039 /** 00040 * Implements a sparse histogram-based classifier, such as the one 00041 * described in 00042 * B. Schiele, J. L. Crowley: Recognition without Correspondence 00043 * using Multidimensional Receptive Field Histograms. 00044 */ 00045 class shClassifier : public supervisedInstanceClassifier { 00046 public: 00047 00048 /** 00049 * the parameters for the class shClassifier 00050 */ 00051 class parameters : public supervisedInstanceClassifier::parameters { 00052 public: 00053 00054 /** 00055 * default constructor 00056 */ 00057 parameters(); 00058 00059 /** 00060 * copy constructor 00061 * @param other the parameters object to be copied 00062 */ 00063 parameters(const parameters& other); 00064 00065 /** 00066 * destructor 00067 */ 00068 virtual ~parameters(); 00069 00070 /** 00071 * returns name of this type. 00072 */ 00073 const char* getTypeName() const; 00074 00075 /** 00076 * copy the contents of a parameters object 00077 * @param other the parameters object to be copied 00078 * @return a reference to this parameters object 00079 */ 00080 parameters& copy(const parameters& other); 00081 00082 /** 00083 * copy the contents of a parameters object 00084 * @param other the parameters object to be copied 00085 * @return a reference to this parameters object 00086 */ 00087 parameters& operator=(const parameters& other); 00088 00089 00090 /** 00091 * returns a pointer to a clone of the parameters 00092 */ 00093 virtual classifier::parameters* clone() const; 00094 00095 /** 00096 * write the parameters in the given ioHandler 00097 * @param handler the ioHandler to be used 00098 * @param complete if true (the default) the enclosing begin/end will 00099 * be also written, otherwise only the data block will be written. 00100 * @return true if write was successful 00101 */ 00102 virtual bool write(ioHandler& handler,const bool complete=true) const; 00103 00104 /** 00105 * read the parameters from the given ioHandler 00106 * @param handler the ioHandler to be used 00107 * @param complete if true (the default) the enclosing begin/end will 00108 * be also written, otherwise only the data block will be written. 00109 * @return true if write was successful 00110 */ 00111 virtual bool read(ioHandler& handler,const bool complete=true); 00112 00113 # ifdef _LTI_MSC_6 00114 /** 00115 * this function is required by MSVC only, as a workaround for a 00116 * very awful bug, which exists since MSVC V.4.0, and still by 00117 * V.6.0 with all bugfixes (so called "service packs") remains 00118 * there... This method is also public due to another bug, so please 00119 * NEVER EVER call this method directly: use read() instead 00120 */ 00121 bool readMS(ioHandler& handler,const bool complete=true); 00122 00123 /** 00124 * this function is required by MSVC only, as a workaround for a 00125 * very awful bug, which exists since MSVC V.4.0, and still by 00126 * V.6.0 with all bugfixes (so called "service packs") remains 00127 * there... This method is also public due to another bug, so please 00128 * NEVER EVER call this method directly: use write() instead 00129 */ 00130 bool writeMS(ioHandler& handler,const bool complete=true) const; 00131 # endif 00132 00133 // ------------------------------------------------ 00134 // the parameters 00135 // ------------------------------------------------ 00136 00137 /** 00138 * The number of bins per axis in the classifier. 00139 */ 00140 int numberOfBins; 00141 00142 /** 00143 * The number of bins per axis in the classifier. The classifier 00144 * checks in training, if the size of this vector is greater 00145 * than zero. Otherwise, numberOfBins is used. 00146 */ 00147 ivector binVector; 00148 00149 /** 00150 * The minimum element to be classified. 00151 */ 00152 dvector minimum; 00153 00154 /** 00155 * The maximum element to be classified. 00156 */ 00157 dvector maximum; 00158 00159 /** 00160 * Should bounds be determined automatically? 00161 */ 00162 bool autoBounds; 00163 00164 }; 00165 00166 /** 00167 * default constructor 00168 */ 00169 shClassifier(); 00170 00171 /** 00172 * copy constructor 00173 * @param other the object to be copied 00174 */ 00175 shClassifier(const shClassifier& other); 00176 00177 /** 00178 * destructor 00179 */ 00180 virtual ~shClassifier(); 00181 00182 /** 00183 * returns the name of this type ("shClassifier") 00184 */ 00185 virtual const char* getTypeName() const; 00186 00187 /** 00188 * copy data of "other" clustering. 00189 * @param other the clustering to be copied 00190 * @return a reference to this clustering object 00191 */ 00192 shClassifier& copy(const shClassifier& other); 00193 00194 /** 00195 * alias for copy member 00196 * @param other the clustering to be copied 00197 * @return a reference to this clustering object 00198 */ 00199 shClassifier& operator=(const shClassifier& other); 00200 00201 /** 00202 * returns a pointer to a clone of this clustering. 00203 */ 00204 virtual classifier* clone() const; 00205 00206 /** 00207 * returns used parameters 00208 */ 00209 const parameters& getParameters() const; 00210 00211 /** 00212 * Supervised training. The vectors in the <code>input</code> matrix 00213 * are arranged row-wise, i.e. each row contains one data vector. 00214 * The <code>ids</code> vector contains the class label for each row. 00215 * This functor implements the 00216 * <a href="http://www.research.microsoft.com/~jplatt/smo.html">SMO algorithm</a> by 00217 * <a href="http://www.research.microsoft.com/~jplatt">J.C. Platt</a>. 00218 * All classes are modeled using the same kernel function, given 00219 * in the parameters object. 00220 * 00221 * @param input the matrix with the input vectors (each row is a training 00222 * vector) 00223 * @param ids vector of class ids for each input point 00224 * @return true if successful, false otherwise. (if false you can check 00225 * the error message with getStatusString()) 00226 */ 00227 virtual bool train(const dmatrix& input, 00228 const ivector& ids); 00229 00230 /** 00231 * Adds an object to this classifier. The id is given automatically 00232 * and returned in the parameter. 00233 */ 00234 virtual bool trainObject(const dmatrix& input, int& id); 00235 00236 00237 //TODO Check whether you really need a new classify method. 00238 // In some cases the superclasses method will suffice. Then just 00239 // delete the declaration and its implementation stump. 00240 00241 /* 00242 * Classification. 00243 * Classifies the feature and returns the output %object with 00244 * the classification result. 00245 * @param feature pattern to be classified 00246 * @return result of the classifications as a classifier::output 00247 */ 00248 /* virtual const output& classify(const dvector& feature); */ 00249 00250 00251 /** 00252 * Classification. 00253 * Classifies the feature and returns the outputVector %object with 00254 * the classification result. 00255 * <p><b>NOTE:</b> This method is NOT really const. Although the main 00256 * members of the shClassifier are not changed some state variables used for 00257 * efficiency are. Thus, it is not save to use the same instance of the 00258 * shClassifier in two different threads. 00259 * @param feature pattern to be classified 00260 * @param result of the classifications as a classifier::outputVector 00261 * @return true if the classification has been successful 00262 */ 00263 virtual bool classify(const dvector& feature, outputVector& result) const; 00264 00265 //TODO: comment the attributes of your clustering 00266 // If you add more attributes manually, do not forget to do following: 00267 // 1. indicate in the default constructor the default values 00268 // 2. make sure that the copy member also copy your new attributes, or 00269 // to ensure there, that these attributes are properly initialized. 00270 00271 00272 /** 00273 * write the classifier 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 classifier 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 protected: 00291 void buildIdMaps(const ivector& ids); 00292 void defineOutputTemplate(); 00293 00294 // map from external id to internal id, used for training 00295 std::map<int,int> idMap; 00296 // map from internal to external id, used for training 00297 std::map<int,int> rIdMap; 00298 00299 int nClasses; 00300 00301 std::vector<sparseHistogram*> models; 00302 }; 00303 00304 } 00305 00306 #endif