latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2004, 2005, 2006 00003 * Lehrstuhl fuer he 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 .......: ltiChrominanceModelEstimator.h 00027 * authors ....: Claudia Goenner 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 8.2.2004 00030 * revisions ..: $Id: ltiChrominanceModelEstimator.h,v 1.4 2007/11/28 20:50:51 goenner Exp $ 00031 */ 00032 00033 #ifndef _LTI_CHROMINANCE_MODEL_ESTIMATOR_H_ 00034 #define _LTI_CHROMINANCE_MODEL_ESTIMATOR_H_ 00035 00036 #include "ltiMacroSymbols.h" 00037 00038 // only for compilers different than VC++ 6.0 available 00039 #ifndef _LTI_MSC_6 00040 00041 #include "ltiImage.h" 00042 #include "ltiHistogram.h" 00043 #include "ltiOverlappingSets2D.h" 00044 #include "cstdlib" 00045 00046 #include "ltiStatisticsFunctor.h" 00047 00048 namespace lti { 00049 00050 /** 00051 * This class stores a 2d histogram containing all chrominances which 00052 * have been seen on a specific object. The class administers histograms 00053 * for >=1 objects. The histograms may be processed by other classes 00054 * to create probability maps or a look up table containing the most 00055 * likely object for each chrominance. 00056 * 00057 * The class supports a time out of the histogram entries. For robustness 00058 * and run-time efficiency a maximal chrominance set may be specified for 00059 * each object including the background. E.g. if an object is known to be 00060 * red it is advantageous to store only red chrominances inside it's 00061 * histogram. This way no false chrominances are stored if the 00062 * segmentation is errornous. Furthermore, if only red objects are processed 00063 * all other chrominances may be excluded from processing for run time 00064 * efficiency. We do not need to store these non-object chrominances in 00065 * the background histogram neither. 00066 * 00067 * If using fully automatic re-calibration it is 00068 * possible to carve out chrominances that are well known to belong to an 00069 * object from the background. These chrominances will always be segmented 00070 * and the object cannot dissappear due to time-out and wrong maxima. 00071 * 00072 * If you change the maximal chrominance sets online you should implement 00073 * a method that deletes all histogram entries outside the region defined 00074 * by the maximal chrominance sets. This is not done yet. 00075 * 00076 * @see overlappingSets2D, chrominanceMapEstimator, probabilityMap2D 00077 */ 00078 class chrominanceModelEstimator : public statisticsFunctor { 00079 public: 00080 /** 00081 * The parameters for the class chrominanceModelEstimator 00082 */ 00083 class parameters : public statisticsFunctor::parameters { 00084 public: 00085 /** 00086 * Default constructor 00087 */ 00088 parameters(); 00089 00090 /** 00091 * Copy constructor 00092 * @param other the parameters object to be copied 00093 */ 00094 parameters(const parameters& other); 00095 00096 /** 00097 * Destructor 00098 */ 00099 ~parameters(); 00100 00101 /** 00102 * Returns name of this type 00103 */ 00104 const char* getTypeName() const; 00105 00106 /** 00107 * Copy the contents of a parameters object 00108 * @param other the parameters object to be copied 00109 * @return a reference to this parameters object 00110 */ 00111 parameters& copy(const parameters& other); 00112 00113 /** 00114 * Copy the contents of a parameters object 00115 * @param other the parameters object to be copied 00116 * @return a reference to this parameters object 00117 */ 00118 parameters& operator=(const parameters& other); 00119 00120 00121 /** 00122 * Returns a pointer to a clone of the parameters 00123 */ 00124 virtual functor::parameters* clone() const; 00125 00126 /** 00127 * Write the parameters in 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 write(ioHandler& handler,const bool complete=true) const; 00134 00135 /** 00136 * Read the parameters from the given ioHandler 00137 * @param handler the ioHandler to be used 00138 * @param complete if true (the default) the enclosing begin/end will 00139 * be also written, otherwise only the data block will be written. 00140 * @return true if write was successful 00141 */ 00142 virtual bool read(ioHandler& handler,const bool complete=true); 00143 00144 # ifdef _LTI_MSC_6 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 read() instead 00151 */ 00152 bool readMS(ioHandler& handler,const bool complete=true); 00153 00154 /** 00155 * This function is required by MSVC only, as a workaround for a 00156 * very awful bug, which exists since MSVC V.4.0, and still by 00157 * V.6.0 with all bugfixes (so called "service packs") remains 00158 * there... This method is also public due to another bug, so please 00159 * NEVER EVER call this method directly: use write() instead 00160 */ 00161 bool writeMS(ioHandler& handler,const bool complete=true) const; 00162 # endif 00163 00164 // ------------------------------------------------ 00165 // the parameters 00166 // ------------------------------------------------ 00167 00168 /** 00169 * Check if a detected object's chrominance is contained in the maximimal 00170 * set of chrominances that are defined for each object a priori. If 00171 * false the histogram is build for all chrominances. 00172 * 00173 * Default: false. 00174 */ 00175 bool checkIsObject; 00176 00177 /** 00178 * Adapt the size of the table containing the maximal chrominances to 00179 * match the quantization of the histograms automatically. 00180 * 00181 * Default: true. 00182 */ 00183 bool autoIsObjectSize; 00184 00185 /** 00186 * Each color band usually contains 8bit resulting in a 256x256 00187 * histogram. For efficiency the histograms may be downsampled by 00188 * 2 power shrinkFactor. 00189 * 00190 * Default: 0. 00191 */ 00192 int shrinkFactor; 00193 00194 /** 00195 * The number of chrominance histograms which are build. Usually this 00196 * is the number of interesting objects plus the background. 00197 * 00198 * Default: 0. 00199 */ 00200 int numberOfModels; 00201 00202 /** 00203 * The interval after which a histogram is multiplied with the 00204 * decay factor. 00205 * 00206 * Default: 100. 00207 */ 00208 int interval; 00209 00210 /** 00211 * The factor by which the histogram are decay to erase old 00212 * entries. 00213 * 00214 * Default: .7. 00215 */ 00216 float decay; 00217 00218 /** 00219 * A histogram is not decayed if its maximum is below this 00220 * value. 00221 * 00222 * Default: 1000. 00223 */ 00224 int minHits; 00225 00226 /** 00227 * The maximal chrominance sets are coded as a single two dimensional 00228 * look up table. 00229 */ 00230 overlappingSets2D checker; 00231 }; 00232 00233 /** 00234 * Default constructor 00235 */ 00236 chrominanceModelEstimator(); 00237 00238 /** 00239 * Construct a functor using the given parameters 00240 */ 00241 chrominanceModelEstimator(const parameters& par); 00242 00243 /** 00244 * Copy constructor 00245 * @param other the object to be copied 00246 */ 00247 chrominanceModelEstimator(const chrominanceModelEstimator& other); 00248 00249 /** 00250 * Destructor 00251 */ 00252 virtual ~chrominanceModelEstimator(); 00253 00254 /** 00255 * Returns the name of this type ("chrominanceModelEstimator") 00256 */ 00257 virtual const char* getTypeName() const; 00258 00259 //TODO: comment your apply methods! 00260 00261 /** 00262 * Copy data of "other" functor. 00263 * @param other the functor to be copied 00264 * @return a reference to this functor object 00265 */ 00266 chrominanceModelEstimator& copy(const chrominanceModelEstimator& other); 00267 00268 /** 00269 * Alias for copy member 00270 * @param other the functor to be copied 00271 * @return a reference to this functor object 00272 */ 00273 chrominanceModelEstimator& 00274 operator=(const chrominanceModelEstimator& other); 00275 00276 /** 00277 * Returns a pointer to a clone of this functor. 00278 */ 00279 virtual functor* clone() const; 00280 00281 /** 00282 * Returns used parameters 00283 */ 00284 const parameters& getParameters() const; 00285 00286 /** 00287 * Set the parameters and ensure validity. 00288 */ 00289 bool setParameters(const parameters& theParams); 00290 00291 /** 00292 * Initialize the chrominance models. 00293 * @param theModels the histograms to be copied inside the internal state 00294 * of the class. 00295 * @return true on success and false otherwise. 00296 */ 00297 bool setChrominanceModels(const std::vector<histogram2D>& theModels); 00298 00299 /** 00300 * Get the histograms that are build up by this class. 00301 */ 00302 const std::vector<histogram2D>& getChrominanceModels() const; 00303 00304 /** 00305 * Store all chrominance models to disk. 00306 * @param prefix a string containing the path or prefix to the files. If 00307 * you specify a directory, please do not forget the /. It is not added 00308 * automatically. 00309 * @param fileNames a vector with the filename for each histogram. 00310 * @return true on success and false otherwise. 00311 */ 00312 bool writeChrominanceModels(const std::string& prefix, 00313 const std::vector<std::string>& fileNames) 00314 const; 00315 00316 /** 00317 * Initialize the chrominance models from disk. 00318 * @param prefix a string containing the path or prefix to the files. If 00319 * you specify a directory, please do not forget the /. It is not added 00320 * automatically. 00321 * @param fileNames a vector with the filename for each histogram. 00322 * @return true on success and false otherwise. 00323 */ 00324 bool readChrominanceModels(const std::string& prefix, 00325 const std::vector<std::string>& fileNames); 00326 00327 /** 00328 * Consider the chrominances in the supplied channel8. The mask specifies 00329 * which histogram is incremented. Usually the background has the 00330 * index 0. Any pixels that shall not be considered must have a value 00331 * above or equal to the number of models. All histograms will be 00332 * considered as updated, no matter whether they really did change. 00333 * @param uChan refers to the columns of the histogram. 00334 * @param vChan refers to the rows of the histogram. 00335 * @param mask specifies which models will be updated. Tip: If drawing the 00336 * mask from object shape keep in mind that drawing rectangles is 00337 * faster than drawing polygons. 00338 * @return true on success and false otherwise. 00339 */ 00340 virtual bool consider (const channel8& uChan, const channel8& vChan, 00341 const channel8& mask); 00342 00343 /** 00344 * Consider the chrominances in the supplied channel8. The mask specifies 00345 * which histogram is incremented. Usually the background has the 00346 * index 0. Any pixels that shall not be considered must have a value 00347 * above or equal to the number of models. 00348 * @param uChan refers to the columns of the histogram. 00349 * @param vChan refers to the rows of the histogram. 00350 * @param mask specifies which models will be updated. 00351 * @param count specifies how the counters will be incremented. The 00352 * counter determine if a histogram is decayed prior to 00353 * considering the new values. 00354 * @return true on success and false otherwise. 00355 */ 00356 virtual bool consider (const channel8& uChan, const channel8& vChan, 00357 const channel8& mask, const ivector& count); 00358 /** 00359 * Consider the chrominances in the supplied channel8. 00360 * All histograms are incremented. 00361 * All histograms will be 00362 * considered as updated, no matter whether they really did change. 00363 * @param uChan refers to the columns of the histogram. 00364 * @param vChan refers to the rows of the histogram. 00365 * @param trainForeground if true all histogram apart the background 00366 * are trained. If false the background is trained. If you train 00367 * more than two models please use the objectTable to distinguish 00368 * the objects' colors. E.g. the object table provides the rough 00369 * segmentation, which this class refines. 00370 * Of course, this method only works for non-overlapping object colors. 00371 * @return true on success and false otherwise. 00372 */ 00373 virtual bool consider (const channel8& uChan, const channel8& vChan, 00374 const bool trainForeground); 00375 00376 /** 00377 * Consider the chrominances in the supplied channel8. 00378 * All histograms are incremented. 00379 * @param uChan refers to the columns of the histogram. 00380 * @param vChan refers to the rows of the histogram. 00381 * @param count specifies how the counters will be incremented. The 00382 * counter determine if a histogram is decayed prior to 00383 * considering the new values. 00384 * @param trainForeground if true all histogram apart the background 00385 * are trained. If false the background is trained. If you train 00386 * more than two models please use the objectTable to distinguish 00387 * the objects' colors. E.g. the object table provides the rough 00388 * segmentation, which this class refines. 00389 * Of course, this method only works for non-overlapping object colors. 00390 * @return true on success and false otherwise. 00391 */ 00392 virtual bool consider (const channel8& uChan, const channel8& vChan, 00393 const bool trainForeground, 00394 const ivector& count); 00395 00396 /** 00397 * Consider the chrominances in the supplied channel8. 00398 * All histograms are incremented. 00399 * All histograms will be 00400 * considered as updated, no matter whether they really did change. 00401 * @param uChan refers to the columns of the histogram. 00402 * @param vChan refers to the rows of the histogram. 00403 * @param index train chrominance model associated with index i. 00404 * Index 0 refers to the background. 00405 * @return true on success and false otherwise. 00406 */ 00407 virtual bool consider (const channel8& uChan, const channel8& vChan, 00408 const int index); 00409 00410 /** 00411 * Consider the chrominances in the supplied channel8. 00412 * All histograms are incremented. 00413 * @param uChan refers to the columns of the histogram. 00414 * @param vChan refers to the rows of the histogram. 00415 * @param count specifies how the counters will be incremented. The 00416 * counter determine if a histogram is decayed prior to 00417 * considering the new values. 00418 * @param index train chrominance model associated with index i. 00419 * Index 0 refers to the background. 00420 * @return true on success and false otherwise. 00421 */ 00422 virtual bool consider (const channel8& uChan, const channel8& vChan, 00423 const int index, 00424 const ivector& count); 00425 00426 protected: 00427 /** 00428 * Returns used parameters 00429 */ 00430 parameters& getWritableParameters(); 00431 00432 /** 00433 * Check if the histograms are valid, i.e. have the correct size 00434 * and accord to the number of models 00435 * @param theHistos the chrominance models. 00436 * @return true on success and false otherwise. 00437 */ 00438 bool checkHistograms(const std::vector<histogram2D>& theHistos); 00439 00440 /** 00441 * Decrease the histogram's entries. 00442 * @param theHisto a chrominance model. 00443 * @return true on success and false otherwise. 00444 */ 00445 bool scaleValues(histogram2D& theHisto); 00446 00447 /** 00448 * Rescale a histogram, i.e. change it's quantization. 00449 * @param theHisto a chrominance model. The result is left here, too. 00450 * @param cellsInDimension the new number of cells. 00451 * @return true on success and false otherwise. 00452 */ 00453 bool rescale(histogram2D& theHisto, const int cellsInDimension); 00454 00455 /** 00456 * Rescale a histogram. 00457 * @param theHisto a chrominance model. 00458 * @param destHisto scaled chrominance model. 00459 * @param cellsInDimension the new number of cells. 00460 * @return true on success and false otherwise. 00461 */ 00462 bool rescale(const histogram2D& theHisto, histogram2D& destHisto, 00463 const int cellsInDimension); 00464 00465 /** 00466 * Upsample a histogram. 00467 * @param theHisto a chrominance model. 00468 * @param destHisto scaled chrominance model. 00469 * @param factor the upsampling factor. 00470 * @return true on success and false otherwise. 00471 */ 00472 bool upsample(const histogram2D& theHisto, histogram2D& destHisto, 00473 const int factor); 00474 00475 /** 00476 * Downsample a histogram. 00477 * @param theHisto a chrominance model. 00478 * @param destHisto scaled chrominance model. 00479 * @param factor the downsampling factor. 00480 * @return true on success and false otherwise. 00481 */ 00482 bool downsample(const histogram2D& theHisto, histogram2D& destHisto, 00483 const int factor); 00484 00485 /** 00486 * Increase the count and check decay. 00487 * @param count specifies how the counters will be incremented. The 00488 * counter determine if a histogram is decayed prior to 00489 * considering the new values. 00490 * @return true on success and false otherwise. 00491 */ 00492 bool increaseCount(const ivector& count); 00493 00494 /** 00495 * the 2D histograms of the chrominances 00496 */ 00497 std::vector<histogram2D> m_chromHistos; 00498 00499 /** 00500 * counts calls of consider 00501 */ 00502 ivector m_count; 00503 00504 /** 00505 * maximum number of entries in the histograms that make sense (max. ubyte) 00506 */ 00507 int m_maxEntries; 00508 00509 /** 00510 * the scaled number of entries 00511 */ 00512 int m_numEntries; 00513 00514 /** 00515 * Special care with setParameters during construction. 00516 * Historic and complicated code, but it works. 00517 */ 00518 bool m_constructModel; 00519 }; 00520 00521 }//namespace 00522 00523 #endif 00524 00525 #endif