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 .......: ltiColorModelSelector.h 00027 * authors ....: Axel Berner, Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 15.2.2002 00030 * revisions ..: $Id: ltiColorModelSelector.h,v 1.10 2006/02/07 18:38:41 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_COLOR_MODEL_SELECTOR_H_ 00034 #define _LTI_COLOR_MODEL_SELECTOR_H_ 00035 00036 #include "ltiTransform.h" 00037 #include "ltiHistogram.h" 00038 #include "ltiProbabilityMap.h" 00039 #include "ltiPCA.h" 00040 #include <list> 00041 00042 namespace lti { 00043 /** 00044 * This functor selects one color model from a set, which elements 00045 * were given by addColorModel. The selected model (a 00046 * lti::probabilityMap functor) will be the one which maximizes the 00047 * likelihood in the given image window, i.e. the model which achieves the 00048 * maximal average probability in the given window. 00049 * 00050 * The window size and position can be defined once in the parameters, or 00051 * can be be given directly to the required apply methods. 00052 * 00053 * It is also possible to get the lti::principalComponents %object that 00054 * will zoom into the model colors. This is mainly used to zoom into 00055 * skin colors, to allow the segmentation between skin-similar and skin 00056 * colors. The computation of the PCA will be delayed until the 00057 * first apply requires it, or when the user explicitelly calls 00058 * initPCA. This is done this way to avoid waiting times in those 00059 * cases where the color zoom is not required. 00060 */ 00061 class colorModelSelector : public transform { 00062 public: 00063 /** 00064 * the parameters for the class colorModelSelector 00065 */ 00066 class parameters : public transform::parameters { 00067 public: 00068 /** 00069 * default constructor 00070 */ 00071 parameters(); 00072 00073 /** 00074 * copy constructor 00075 * @param other the parameters object to be copied 00076 */ 00077 parameters(const parameters& other); 00078 00079 /** 00080 * destructor 00081 */ 00082 ~parameters(); 00083 00084 /** 00085 * returns name of this type 00086 */ 00087 const char* getTypeName() const; 00088 00089 /** 00090 * copy the contents of a parameters object 00091 * @param other the parameters object to be copied 00092 * @return a reference to this parameters object 00093 */ 00094 parameters& copy(const parameters& other); 00095 00096 /** 00097 * copy the contents of a parameters object 00098 * @param other the parameters object to be copied 00099 * @return a reference to this parameters object 00100 */ 00101 parameters& operator=(const parameters& other); 00102 00103 /** 00104 * returns a pointer to a clone of the parameters 00105 */ 00106 virtual functor::parameters* clone() const; 00107 00108 /** 00109 * write the parameters in the given ioHandler 00110 * @param handler the ioHandler to be used 00111 * @param complete if true (the default) the enclosing begin/end will 00112 * be also written, otherwise only the data block will be written. 00113 * @return true if write was successful 00114 */ 00115 virtual bool write(ioHandler& handler,const bool complete=true) const; 00116 00117 /** 00118 * read the parameters from 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 read(ioHandler& handler,const bool complete=true); 00125 00126 # ifdef _LTI_MSC_6 00127 /** 00128 * this function is required by MSVC only, as a workaround for a 00129 * very awful bug, which exists since MSVC V.4.0, and still by 00130 * V.6.0 with all bugfixes (so called "service packs") remains 00131 * there... This method is also public due to another bug, so please 00132 * NEVER EVER call this method directly: use read() instead 00133 */ 00134 bool readMS(ioHandler& handler,const bool complete=true); 00135 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 write() instead 00142 */ 00143 bool writeMS(ioHandler& handler,const bool complete=true) const; 00144 # endif 00145 00146 // ------------------------------------------------ 00147 // the parameters 00148 // ------------------------------------------------ 00149 00150 /** 00151 * rectangle that defines the window in the given image 00152 * for which the color probability is calculated 00153 * and finally the colorModel is selected by. 00154 * Some apply methods accept this parameter directly. In these cases, 00155 * the value of <code>window</code> will be ignored. 00156 * 00157 * Default value: (0,0,10000,10000) 00158 */ 00159 rectangle window; 00160 00161 /** 00162 * filename of the default color model (with path). 00163 * @see lti::colorModelSelector::addColorModel 00164 * 00165 * Default value: "../skin/skin-32-32-32.hist" 00166 */ 00167 std::string colorHistFile; 00168 00169 /** 00170 * filename of the default non-color histogram (with path) 00171 * @see lti::colorModelSelector::addColorModel 00172 * 00173 * Default value: "../skin/nonskin-32-32-32.hist" 00174 */ 00175 std::string nonColorHistFile; 00176 00177 /** 00178 * default %parameters for the probabilityMap functors. This will be 00179 * taken into account only for the models added after setting the 00180 * parameters object. 00181 * 00182 * Default values: see probabilityMap::parameters 00183 */ 00184 probabilityMap::parameters mapParameters; 00185 00186 /** 00187 * \b Deprecated 00188 * 00189 * To compute the PCA used for the color zooming, a specific 00190 * number of data points is required, generated from the color 00191 * model used. The number of data points used can be specified here. 00192 * 00193 * This parameter is not required any more and will be ignore. 00194 * The covariance and mean values for the PCA functor are 00195 * computed directly from the histogram. 00196 * 00197 * Default value 500000 00198 */ 00199 int pcaDataPoints; 00200 }; 00201 00202 /** 00203 * default constructor 00204 */ 00205 colorModelSelector(); 00206 00207 /** 00208 * copy constructor 00209 * @param other the object to be copied 00210 */ 00211 colorModelSelector(const colorModelSelector& other); 00212 00213 /** 00214 * destructor 00215 */ 00216 virtual ~colorModelSelector(); 00217 00218 /** 00219 * returns the name of this type ("colorModelSelector") 00220 */ 00221 virtual const char* getTypeName() const; 00222 00223 /** 00224 * copy into colorMap and pca the internal functors for the color models 00225 * that maximize the likelihood for the image window specified in the 00226 * parameters %object. 00227 * 00228 * @param img image with the source data. 00229 * @param colorMap the internal model which maximizes the color 00230 * probability for the given image window will be 00231 * copied here. 00232 * @param pca functor with the whitening transform used to zoom 00233 * the color model into the whole RGB color space. 00234 * @return true if apply successful or false otherwise. 00235 * 00236 * Note that this member will copy the models and pca transformation 00237 * functors, and this takes some time. If you can ensure that 00238 * this object will exist while using the models and pca 00239 * functors, you better get just a pointer to the internal objects. 00240 */ 00241 bool apply(const image& img, 00242 probabilityMap& colorMap, 00243 principalComponents<float>& pca); 00244 00245 /** 00246 * get a pointer into the internal stored model and transformation 00247 * functors that maximize the likelihood for the image window 00248 * specified in the parameters %object. Note that if this object 00249 * is destroyed, the pointers returned will be invalid. 00250 * 00251 * @param img image with the source data. 00252 * @param pColorMap pointer to the internal model which maximizes the color 00253 * probability for the given image window. 00254 * @param pPca pointer to the internal PCA functor (whitening transform) 00255 * computed from the colorMap to be used in color zooming. 00256 * @return true if apply successful or false otherwise. 00257 */ 00258 bool apply(const image& img, 00259 const probabilityMap* &pColorMap, 00260 const principalComponents<float>* &pPca); 00261 00262 /** 00263 * copy into colorMap and pca the internal functors for the color models 00264 * that maximize the likelihood for the image window specified in the 00265 * %parameters. 00266 * 00267 * @param img image with the source data. 00268 * @param window the region of the image to be considered. 00269 * @param colorMap the internal model which maximizes the color 00270 * probability for the given image window will be 00271 * copied here. 00272 * @param pca functor with the whitening transform used to zoom 00273 * the color model into the whole RGB color space. 00274 * @return true if apply successful or false otherwise. 00275 * 00276 * Note that this member will copy the models and pca transformation 00277 * functors, and this takes some time. If you can ensure that 00278 * this object will exist while using the models and pca 00279 * functors, you better get just a pointer to the internal objects. 00280 */ 00281 bool apply(const image& img, 00282 const rectangle& window, 00283 probabilityMap& colorMap, 00284 principalComponents<float>& pca); 00285 00286 /** 00287 * get a pointer to the internal stored model and transformation 00288 * functors that maximize the likelihood for the image window 00289 * specified in the %parameters. Note that if this object 00290 * is destroyed, the pointers returned will be invalid. 00291 * 00292 * @param img image with the source data. 00293 * @param window the region of the image to be considered. 00294 * @param pColorMap pointer to the internal model which maximizes the color 00295 * probability for the given image window. 00296 * @param pPca pointer to the internal PCA functor (whitening transform) 00297 * computed from the colorMap to be used in color zooming. 00298 * @return true if apply successful or false otherwise. 00299 */ 00300 bool apply(const image& img, 00301 const rectangle& window, 00302 const probabilityMap* &pColorMap, 00303 const principalComponents<float>* &pPca); 00304 00305 /** 00306 * copy into colorMap the internal functor for the color model 00307 * that maximizes the likelihood for the image window specified in the 00308 * parameters %object. 00309 * 00310 * @param img image with the source data. 00311 * @param colorMap the internal model which maximizes the color 00312 * probability for the given image window will be 00313 * copied here. 00314 * @return true if apply successful or false otherwise. 00315 * 00316 * Note that this member will copy the color model functor, and 00317 * this takes some time. If you can ensure that this object will 00318 * exist while using the model functor, you better get 00319 * just a pointer to the internal objects. 00320 */ 00321 bool apply(const image& img, 00322 probabilityMap& colorMap) const; 00323 00324 /** 00325 * get a pointer to the internal stored color model functor that 00326 * maximizes the likelihood for the image window specified in the 00327 * parameters %object. Note that if this object is destroyed, the 00328 * pointers returned will be invalid. 00329 * 00330 * @param img image with the source data. 00331 * @param pColorMap pointer to the internal model which maximizes the color 00332 * probability for the given image window. 00333 * @return true if apply successful or false otherwise. 00334 */ 00335 bool apply(const image& img, 00336 const probabilityMap* &pColorMap) const; 00337 00338 /** 00339 * get a pointer to the internal stored color model functor that 00340 * maximizes the likelihood for the image window specified in the 00341 * parameters %object. Note that if this object is destroyed, the 00342 * pointers returned will be invalid. 00343 * 00344 * @param img image with the source data. 00345 * @param pColorMap pointer to the internal model which maximizes the color 00346 * probability for the given image window. 00347 * @param modelName the name of the model used 00348 * @return true if apply successful or false otherwise. 00349 */ 00350 bool apply(const image& img, 00351 const probabilityMap* &pColorMap, 00352 std::string& modelName) const; 00353 00354 /** 00355 * Get a pointer to the internal stored model and transformation functors 00356 * that maximize the likelihood for the image window specified in the 00357 * arguments. Note that if this object is destroyed, the pointers 00358 * returned will be invalid. 00359 * 00360 * @param img image with the source data. 00361 * @param window the region of the image to be considered. 00362 * @param pColorMap pointer to the internal model which maximizes the color 00363 * probability for the given image window. 00364 * @param pPca pointer to the internal PCA functor (whitening transform) 00365 * computed from the colorMap to be used in color zooming. 00366 * @param modelName the name of the model used 00367 * @return true if apply successful or false otherwise. 00368 */ 00369 bool apply(const image& img, 00370 const rectangle& window, 00371 const probabilityMap* &pColorMap, 00372 const principalComponents<float>* &pPca, 00373 std::string& modelName); 00374 00375 /** 00376 * Copy into colorMap the internal functor for the color model 00377 * that maximizes the likelihood for the image window specified in the 00378 * parameters %object. 00379 * 00380 * @param img image with the source data. 00381 * @param window the region of the image to be considered. 00382 * @param colorMap the internal model which maximizes the color 00383 * probability for the given image window will be 00384 * copied here. 00385 * @return true if apply successful or false otherwise. 00386 * 00387 * Note that this member will copy the color model functor, and 00388 * this takes some time. If you can ensure that this object will 00389 * exist while using the model functor, you better get 00390 * just a pointer to the internal objects. 00391 */ 00392 bool apply(const image& img, 00393 const rectangle& window, 00394 probabilityMap& colorMap) const; 00395 00396 /** 00397 * get a pointer to the internal stored color model functor that 00398 * maximizes the likelihood for the image window specified in the 00399 * parameters %object. Note that if this object is destroyed, the 00400 * pointers returned will be invalid. 00401 * 00402 * @param img image with the source data. 00403 * @param window the region of the image to be considered. 00404 * @param pColorMap pointer to the internal model which maximizes the color 00405 * probability for the given image window. 00406 * @return true if apply successful or false otherwise. 00407 */ 00408 bool apply(const image& img, 00409 const rectangle& window, 00410 const probabilityMap* &pColorMap) const; 00411 00412 /** 00413 * get a pointer to the internal stored color model functor that 00414 * maximizes the likelihood for the image window specified in the 00415 * parameters %object. Note that if this object is destroyed, the 00416 * pointers returned will be invalid. 00417 * 00418 * @param img image with the source data. 00419 * @param window the region of the image to be considered. 00420 * @param pColorMap pointer to the internal model which maximizes the color 00421 * probability for the given image window. 00422 * @param modelName name of the model used 00423 * @return true if apply successful or false otherwise. 00424 */ 00425 bool apply(const image& img, 00426 const rectangle& window, 00427 const probabilityMap* &pColorMap, 00428 std::string& modelName) const; 00429 00430 /** 00431 * add a further color model to a intern list. 00432 * 00433 * The model is assumed to be stored in a file of a 00434 * thistogram<double> written with a lispStreamHandler. 00435 * 00436 * If you want to write the histograms by yourself do something like: 00437 * \code 00438 * thistogram<double> myHist; // the histogram to be stored in a file 00439 * 00440 * std::ofstream out("myFile.hist"); // the file stream 00441 * lti::lispStreamHandler lsh(out); // the lisp stream handler 00442 * myHist.write(lsh); // write to the lisp stream handler 00443 * out.close(); 00444 * 00445 * \endcode 00446 * 00447 * Please note that you can also add all histogram models directly, and 00448 * at the end you can write this functor with all its models (and pca) to 00449 * be readed later. This can save lots of time, specially, because 00450 * the pca do not need to be computed again. 00451 * 00452 * @param name the name for this model 00453 * @param colorHistFileName complete file name (path+filename) for the 00454 * thistogram<double> file with the color model 00455 * @param nonColorHistFileName complete file name (path+filename) for the 00456 * thistogram<double> file with the non-color model. 00457 */ 00458 bool addColorModel(const std::string& name, 00459 const std::string& colorHistFileName, 00460 const std::string& nonColorHistFileName); 00461 00462 /** 00463 * add a further color model to a intern list 00464 * 00465 * @param name the name for this model 00466 * @param colorHist thistogram<double> with the color model 00467 * @param nonColorHist thistogram<double> with the non-color model. 00468 */ 00469 bool addColorModel(const std::string& name, 00470 const thistogram<double>& colorHist, 00471 const thistogram<double>& nonColorHist); 00472 00473 /** 00474 * add a further color model to a intern list 00475 * 00476 * @param name the name for this model 00477 * @param colorMap a probabilityMap functor with the color and non-color 00478 * models. 00479 */ 00480 bool addColorModel(const std::string& name, 00481 const probabilityMap& colorMap); 00482 00483 /** 00484 * Update parameters for all models. 00485 * 00486 * The color models are considered here as state, and not as part of the 00487 * probabilityMap::parameters. I.e. all attributes except the histograms 00488 * themselves will be updated in each of the availabe functors. 00489 */ 00490 bool updateModels(const probabilityMap::parameters& newAttribs); 00491 00492 /** 00493 * call if you want to precompute the PCA (Whitening 00494 * Transformation) for the color models, before calling the 00495 * corresponding apply methods. Otherwise, the pca will be 00496 * initialized on-the-fly (when required). 00497 */ 00498 bool initPCA(); 00499 00500 /** 00501 * remove all color models 00502 */ 00503 bool clearColorModels(); 00504 00505 /** 00506 * remove only the color models with the given name. 00507 */ 00508 bool removeColorModel(const std::string& name); 00509 00510 /** 00511 * copy data of "other" functor. 00512 * @param other the functor to be copied 00513 * @return a reference to this functor object 00514 */ 00515 colorModelSelector& copy(const colorModelSelector& other); 00516 00517 /** 00518 * alias for copy member 00519 * @param other the functor to be copied 00520 * @return a reference to this functor object 00521 */ 00522 colorModelSelector& operator=(const colorModelSelector& other); 00523 00524 /** 00525 * read the colorModelSelector from the given ioHandler 00526 * @param handler the ioHandler to be used 00527 * @param complete if true (the default) the enclosing begin/end will 00528 * be also written, otherwise only the data block will be written. 00529 * @return true if write was successful 00530 */ 00531 bool read(ioHandler& handler,const bool complete=true); 00532 00533 /** 00534 * write the colorModelSelector in the given ioHandler 00535 * @param handler the ioHandler to be used 00536 * @param complete if true (the default) the enclosing begin/end will 00537 * be also written, otherwise only the data block will be written. 00538 * @return true if write was successful 00539 */ 00540 bool write(ioHandler& handler,const bool complete=true) const; 00541 00542 /** 00543 * returns a pointer to a clone of this functor. 00544 */ 00545 virtual functor* clone() const; 00546 00547 /** 00548 * returns used parameters 00549 */ 00550 const parameters& getParameters() const; 00551 00552 protected: 00553 /** 00554 * Internal data type to facilitate the administration of 00555 * named color models. 00556 */ 00557 class namedModel { 00558 public: 00559 /** 00560 * a free defineable name for a model 00561 */ 00562 std::string name; 00563 00564 /** 00565 * default contructor 00566 */ 00567 namedModel(); 00568 00569 /** 00570 * read the namedModel from the given ioHandler 00571 * @param handler the ioHandler to be used 00572 * @param complete if true (the default) the enclosing begin/end will 00573 * be also written, otherwise only the data block will be written. 00574 * @return true if write was successful 00575 */ 00576 bool read (ioHandler& handler,const bool complete = true); 00577 00578 /** 00579 * write the namedModel in the given ioHandler 00580 * @param handler the ioHandler to be used 00581 * @param complete if true (the default) the enclosing begin/end will 00582 * be also written, otherwise only the data block will be written. 00583 * @return true if write was successful 00584 */ 00585 bool write(ioHandler& handler,const bool complete = true) const; 00586 00587 /** 00588 * copy the contents of a namedModel object 00589 * @param other the namedModel object to be copied 00590 * @return a reference to this parameters object 00591 */ 00592 namedModel& copy(const namedModel& other); 00593 00594 /** 00595 * copy the contents of a namedModel object 00596 * @param other the namedModel object to be copied 00597 * @return a reference to this parameters object 00598 */ 00599 namedModel& operator=(const namedModel& other); 00600 00601 /** 00602 * set the color map to be used. The pca will be marked as uninitialized 00603 */ 00604 void setColorMap(const probabilityMap& colMap); 00605 00606 /** 00607 * get color map being used 00608 */ 00609 const probabilityMap& getColorMap() const; 00610 00611 /** 00612 * get color map being used 00613 */ 00614 probabilityMap& getColorMap(); 00615 00616 /** 00617 * get color map being used 00618 */ 00619 principalComponents<float>& getPCA(); 00620 00621 /** 00622 * returns true if the pca was already initialized 00623 */ 00624 bool pcaInitialized() const; 00625 00626 /** 00627 * compute the pca considering the histogram model given in the 00628 * colorMap 00629 * @return true if successful, false otherwise 00630 */ 00631 bool computePCA(); 00632 00633 protected: 00634 /** 00635 * if true, the pca was already computed from the color model. 00636 * if false, it still needs to be computed 00637 */ 00638 bool pcaComputed; 00639 00640 /** 00641 * principalComponents class of the model 00642 */ 00643 lti::principalComponents<float> pca; 00644 00645 /** 00646 * probabilityMap class of the model 00647 */ 00648 lti::probabilityMap colorMap; 00649 }; 00650 00651 /** 00652 * list with all color models 00653 */ 00654 std::list<namedModel> modelList; 00655 }; 00656 00657 } 00658 00659 #endif