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 .......: ltiClassifier2DVisualizer.h 00027 * authors ....: Jochen Wickel 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 15.5.2002 00030 * revisions ..: $Id: ltiClassifier2DVisualizer.h,v 1.8 2006/02/07 18:13:52 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_CLASSIFIER_2D_VISUALIZER_H_ 00034 #define _LTI_CLASSIFIER_2D_VISUALIZER_H_ 00035 00036 #include "ltiTypes.h" 00037 #include "ltiColors.h" 00038 #include "ltiFunctor.h" 00039 #include "ltiImage.h" 00040 #include "ltiGenericVector.h" 00041 #include "ltiSupervisedInstanceClassifier.h" 00042 00043 namespace lti { 00044 /** 00045 * This functor creates beautiful pictures. They additionally 00046 * have the property that they show the classification properties 00047 * of a supervisedInstanceClassifier for a 2D feature space. 00048 * Its application is rather simple: You have to create a 00049 * classifier for a 2D space, then you give it to one of the 00050 * apply methods, together with the target image. 00051 * 00052 * Example: 00053 * \code 00054 // number of training samples 00055 * const int N=150; 00056 * // number of classes 00057 * const int K=3; 00058 * const int width=400; 00059 * const int height=200; 00060 * 00061 * // fill data and label matrix 00062 * lti::dmatrix data(N,2); 00063 * lti::ivector labels(N); 00064 * int j=0; 00065 * 00066 * std::cerr << "Generating data..."; 00067 * for (int k=0; k<K; k++) { 00068 * // generate gaussian 00069 * double sigma=double(k)/(K)*20.0+10; 00070 * double mx=double(k)/K*(width-sigma)+sigma; 00071 * double my=double(k)/K*(height-sigma)+sigma; 00072 * lti::gaussianDistribution px(mx,sigma); 00073 * lti::gaussianDistribution py(my,sigma); 00074 * for (int i=0; i<N/K; i++) { 00075 * data[j][0]=px.draw(); 00076 * data[j][1]=py.draw(); 00077 * labels[j]=k; 00078 * j++; 00079 * } 00080 * } 00081 * 00082 * std::cerr << "\nTraining MLP..."; 00083 * // train MLP 00084 * lti::MLP s; 00085 * lti::streamProgressInfo inf(std::cerr,"MLP"); 00086 * s.setProgressObject(inf); 00087 * lti::MLP::parameters p=s.getParameters(); 00088 * 00089 * s.setParameters(p); 00090 * if (!s.train(data,labels)) { 00091 * std::cerr << "Error: " << s.getStatusString() << "\n"; 00092 * //return; 00093 * } 00094 00095 * std::cerr << "\nVisualizing..."; 00096 * // NOW COMES THE INTERESTING PART: 00097 * // we need an image 00098 * lti::image panel; 00099 * lti::classifier2DVisualizer visual; 00100 * lti::classifier2DVisualizer::parameters vp; 00101 * // set the parameters 00102 * vp.showBoundaries=true; 00103 * vp.imgSize=point(width,height); 00104 * vp.upperRight=dpoint(width,height); 00105 * visual.setParameters(vp); 00106 * // create the image 00107 * if (!visual.apply(s,data,panel,labels)) { 00108 * std::cerr << "Error: " << s.getStatusString() << "\n"; 00109 * return; 00110 * } 00111 * // enjoy! 00112 * \endcode 00113 * The visualization result of this example is shown in \image html svmvis.png 00114 */ 00115 class classifier2DVisualizer : public functor { 00116 public: 00117 /** 00118 * the parameters for the class classifier2DVisualizer 00119 */ 00120 class parameters : public functor::parameters { 00121 public: 00122 /** 00123 * default constructor 00124 */ 00125 parameters(); 00126 00127 /** 00128 * copy constructor 00129 * @param other the parameters object to be copied 00130 */ 00131 parameters(const parameters& other); 00132 00133 /** 00134 * destructor 00135 */ 00136 ~parameters(); 00137 00138 /** 00139 * returns name of this type 00140 */ 00141 const char* getTypeName() const; 00142 00143 /** 00144 * copy the contents of a parameters object 00145 * @param other the parameters object to be copied 00146 * @return a reference to this parameters object 00147 */ 00148 parameters& copy(const parameters& other); 00149 00150 /** 00151 * copy the contents of a parameters object 00152 * @param other the parameters object to be copied 00153 * @return a reference to this parameters object 00154 */ 00155 parameters& operator=(const parameters& other); 00156 00157 00158 /** 00159 * returns a pointer to a clone of the parameters 00160 */ 00161 virtual functor::parameters* clone() const; 00162 00163 /** 00164 * write the parameters in the given ioHandler 00165 * @param handler the ioHandler to be used 00166 * @param complete if true (the default) the enclosing begin/end will 00167 * be also written, otherwise only the data block will be written. 00168 * @return true if write was successful 00169 */ 00170 virtual bool write(ioHandler& handler,const bool complete=true) const; 00171 00172 /** 00173 * read the parameters from the given ioHandler 00174 * @param handler the ioHandler to be used 00175 * @param complete if true (the default) the enclosing begin/end will 00176 * be also written, otherwise only the data block will be written. 00177 * @return true if write was successful 00178 */ 00179 virtual bool read(ioHandler& handler,const bool complete=true); 00180 00181 # ifdef _LTI_MSC_6 00182 /** 00183 * this function is required by MSVC only, as a workaround for a 00184 * very awful bug, which exists since MSVC V.4.0, and still by 00185 * V.6.0 with all bugfixes (so called "service packs") remains 00186 * there... This method is also public due to another bug, so please 00187 * NEVER EVER call this method directly: use read() instead 00188 */ 00189 bool readMS(ioHandler& handler,const bool complete=true); 00190 00191 /** 00192 * this function is required by MSVC only, as a workaround for a 00193 * very awful bug, which exists since MSVC V.4.0, and still by 00194 * V.6.0 with all bugfixes (so called "service packs") remains 00195 * there... This method is also public due to another bug, so please 00196 * NEVER EVER call this method directly: use write() instead 00197 */ 00198 bool writeMS(ioHandler& handler,const bool complete=true) const; 00199 # endif 00200 00201 // ------------------------------------------------ 00202 // the parameters 00203 // ------------------------------------------------ 00204 00205 //TODO: comment the parameters of your functor 00206 // If you add more parameters manually, do not forget to do following: 00207 // 1. indicate in the default constructor the default values 00208 // 2. make sure that the copy member also copy your new parameters 00209 // 3. make sure that the read and write members also read and 00210 // write your parameters 00211 00212 00213 /** 00214 * The lower left corner of the area that is to be plotted. 00215 * Default is (0,0). 00216 */ 00217 dpoint lowerLeft; 00218 00219 /** 00220 * The upper right corner of the area that is to be plotted. 00221 * Default is (1.333,1) 00222 */ 00223 dpoint upperRight; 00224 00225 /** 00226 * The size of the image that is generated. Default is 00227 * (1024,768). 00228 */ 00229 point imgSize; 00230 00231 /** 00232 * The size of each virtual pixel. A virtual pixel is a 00233 * rectangle whose center is classified. Default is 2. 00234 */ 00235 int pixSize; 00236 00237 /** 00238 * The size of a highlight. Default is 6. 00239 */ 00240 int highlightSize; 00241 00242 /** 00243 * If this is true, the visualizer will draw boundaries 00244 * between classes. A pixel is assigned to the class 00245 * with the highest classification output. 00246 * 00247 * Default value: false 00248 */ 00249 bool showBoundaries; 00250 00251 /** 00252 * The color of the drawn boundaries. 00253 * 00254 * Default is Grey75. 00255 */ 00256 rgbColor boundaryColor; 00257 00258 /** 00259 * The color of the drawn highlights. 00260 * 00261 * Default is White. 00262 */ 00263 rgbColor highlightColor; 00264 00265 /** 00266 * The color of the drawn highlights2. 00267 * 00268 * Default is Black. 00269 */ 00270 rgbColor highlightColor2; 00271 00272 /** 00273 * The offset for choosing the class color. If pixels are 00274 * colored, this value is added to the label to get 00275 * the index for the color table. 00276 * 00277 * Default is 0. 00278 */ 00279 int colorOffset; 00280 00281 /** 00282 * The colormap which is to be used for assigning colors 00283 * to labels. Note that the colors are assigned cyclically, 00284 * i.e. if there are more classes than labels, 00285 * there will be different classes with the same colors. 00286 * 00287 * Default value: a palette composed by the values Red, Green, Blue, 00288 * Yellow, Magenta, Cyan, DarkOrange, Fusia, BrightGreen, 00289 * LawnGreen, LightBlue, DarkViolet, Grey75, Grey50 and 00290 * Grey25 00291 */ 00292 palette colorMap; 00293 00294 /** 00295 * Each color channel of the final image will usually have 00296 * values higher than the possible displayable values from 0.0 00297 * to 1.0. To normalize the RGB channels, two options are 00298 * given. If scaleRGB together is true all three channels will 00299 * be normalized by the same value, which will be the maximal 00300 * value between all three channels. If false (default), each 00301 * channel will be normalized by its maximum. 00302 * 00303 * Default value: false 00304 */ 00305 bool scaleRGBTogether; 00306 00307 inline const rgbPixel& getColor(int i) const { 00308 assert(colorMap.size() > 0); 00309 return colorMap.at((i+colorOffset)%colorMap.size()); 00310 } 00311 00312 inline int getNumberOfColors() const { 00313 return colorMap.size(); 00314 } 00315 00316 protected: 00317 // class colors for visualization 00318 static const lti::rgbColor defaultColors[]; 00319 }; 00320 00321 00322 /** 00323 * default constructor 00324 */ 00325 classifier2DVisualizer(); 00326 00327 /** 00328 * copy constructor 00329 * @param other the object to be copied 00330 */ 00331 classifier2DVisualizer(const classifier2DVisualizer& other); 00332 00333 /** 00334 * destructor 00335 */ 00336 virtual ~classifier2DVisualizer(); 00337 00338 /** 00339 * returns the name of this type ("classifier2DVisualizer") 00340 */ 00341 virtual const char* getTypeName() const; 00342 00343 //TODO: comment your apply methods! 00344 00345 /** 00346 * Applies the given classifier to the area defined in the 00347 * parameters object. The image will contain the visualization 00348 * result. 00349 * @param cls supervisedInstanceClassifier with the classifier 00350 * to be visualized. The classifier must be able to 00351 * classify a 2D feature set. 00352 * @param result will receive the visualization result. 00353 * @return true if apply successful or false otherwise. 00354 */ 00355 bool apply(const supervisedInstanceClassifier& cls, 00356 image& result) const; 00357 00358 /** 00359 * Applies the given classifier to the area defined in the 00360 * parameters object. The image will contain the visualization 00361 * result. It also highlights the vectors given in highlights. 00362 * @param cls supervisedInstanceClassifier with the classifier 00363 * to be visualized. The classifier must be able to 00364 * classify a 2D feature set. 00365 * @param highlights matrix with each row containing a vector 00366 * that is to be highlighted. 00367 * @param result will receive the visualization result. 00368 * @return true if apply successful or false otherwise. 00369 */ 00370 bool apply(const supervisedInstanceClassifier& cls, 00371 const lti::dmatrix& highlights, 00372 image& result) const; 00373 00374 /** 00375 * Applies the given classifier to the area defined in the 00376 * parameters object. The image will contain the visualization 00377 * result. It also highlights the vectors given in highlights. 00378 * The color of each highlight is determined by the corresponding 00379 * value in labels. 00380 * @param cls supervisedInstanceClassifier with the classifier 00381 * to be visualized. The classifier must be able to 00382 * classify a 2D feature set. 00383 * @param highlights matrix with each row containing a vector 00384 * that is to be highlighted. 00385 * @param result will receive the visualization result. 00386 * @param labels is a vector with the class labels for each row of 00387 * highlights. 00388 * @return true if apply successful or false otherwise. 00389 */ 00390 bool apply(const supervisedInstanceClassifier& cls, 00391 const lti::dmatrix& highlights, 00392 image& result, 00393 const ivector& labels) const; 00394 00395 /** 00396 * Applies the given classifier to the area defined in the 00397 * parameters object. The image will contain the visualization 00398 * result. It also highlights the vectors given in highlights. 00399 * The color of each highlight is determined by the corresponding 00400 * value in labels. You can also distinguish between two classes 00401 * of labels. The label class is determined by the vector htype. 00402 * @param cls supervisedInstanceClassifier with the classifier 00403 * to be visualized. The classifier must be able to 00404 * classify a 2D feature set. 00405 * @param highlights matrix with each row containing a vector 00406 * that is to be highlighted. 00407 * @param result will receive the visualization result. 00408 * @param labels is a vector with the class labels for each row of 00409 * highlights. 00410 * @param htype is a vector with a flag denoting the highlight 00411 * type. 00412 * @return true if apply successful or false otherwise. 00413 */ 00414 bool apply(const supervisedInstanceClassifier& cls, 00415 const lti::dmatrix& highlights, 00416 image& result, 00417 const ivector& labels, 00418 const genericVector<bool>& htype) const; 00419 00420 /** 00421 * copy data of "other" functor. 00422 * @param other the functor to be copied 00423 * @return a reference to this functor object 00424 */ 00425 classifier2DVisualizer& copy(const classifier2DVisualizer& other); 00426 00427 /** 00428 * alias for copy member 00429 * @param other the functor to be copied 00430 * @return a reference to this functor object 00431 */ 00432 classifier2DVisualizer& operator=(const classifier2DVisualizer& other); 00433 00434 /** 00435 * returns a pointer to a clone of this functor. 00436 */ 00437 virtual functor* clone() const; 00438 00439 /** 00440 * returns used parameters 00441 */ 00442 const parameters& getParameters() const; 00443 }; 00444 } 00445 00446 #endif