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 /*---------------------------------------------------------------- 00025 * project ....: LTI Digital Image/Signal Processing Library 00026 * file .......: ltiCsPresegmentation.h 00027 * authors ....: Pablo Alvarado 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 8.11.2001 00030 * revisions ..: $Id: ltiCsPresegmentation.h,v 1.7 2006/02/07 18:43:31 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_CS_PRESEGMENTATION_H_ 00034 #define _LTI_CS_PRESEGMENTATION_H_ 00035 00036 #include "ltiImage.h" 00037 #include "ltiSegmentation.h" 00038 #include "ltiKMColorQuantization.h" 00039 00040 namespace lti { 00041 /** 00042 * The csPresegmentation functor (states for color space presegmentation) 00043 * tries to detect which pixels belong to the background and which not. 00044 * For its task there are some assumtions/requirements: 00045 * 00046 * - The background must be homogeneous, i.e. no textures are allowed. 00047 * The best results are obtained with a black background, but this is 00048 * not necessary. 00049 * - The number of "perceivable" colors in the image is less than 00050 * the number of quantization colors given in the parameters. 00051 * - There is a high probability that the border of the image 00052 * contains only background pixels. 00053 * 00054 * These conditions are not met in many applications, for which other 00055 * segmentation approaches have to be found. 00056 * 00057 * The algorithm works as follows: 00058 * 00059 * First, a color quantization using k-Means is done. This produces 00060 * usually a "noisy" index mask and a palette. A median filter or a 00061 * k-Nearest-Neighbor Filter smooths this mask. Assuming that the border 00062 * of the image contains mostly background pixels, some simple 00063 * statistics are done, to decide which palette entries belong to 00064 * the background. Pixels belonging to other palette entries are 00065 * taken as the object pixels. 00066 * 00067 * See the lti::csPresegmentation::parameters for more information. 00068 * */ 00069 class csPresegmentation : public segmentation { 00070 public: 00071 /** 00072 * the parameters for the class csPresegmentation 00073 */ 00074 class parameters : public segmentation::parameters { 00075 public: 00076 /** 00077 * default constructor 00078 */ 00079 parameters(); 00080 00081 /** 00082 * copy constructor 00083 * @param other the parameters object to be copied 00084 */ 00085 parameters(const parameters& other); 00086 00087 /** 00088 * destructor 00089 */ 00090 ~parameters(); 00091 00092 /** 00093 * returns name of this type 00094 */ 00095 const char* getTypeName() const; 00096 00097 /** 00098 * copy the contents of a parameters object 00099 * @param other the parameters object to be copied 00100 * @return a reference to this parameters object 00101 */ 00102 parameters& copy(const parameters& other); 00103 00104 /** 00105 * copy the contents of a parameters object 00106 * @param other the parameters object to be copied 00107 * @return a reference to this parameters object 00108 */ 00109 parameters& operator=(const parameters& other); 00110 00111 00112 /** 00113 * returns a pointer to a clone of the parameters 00114 */ 00115 virtual functor::parameters* clone() const; 00116 00117 /** 00118 * write the parameters in 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 write(ioHandler& handler,const bool complete=true) const; 00125 00126 /** 00127 * read the parameters from 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 read(ioHandler& handler,const bool complete=true); 00134 00135 # ifdef _LTI_MSC_6 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 read() instead 00142 */ 00143 bool readMS(ioHandler& handler,const bool complete=true); 00144 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 write() instead 00151 */ 00152 bool writeMS(ioHandler& handler,const bool complete=true) const; 00153 # endif 00154 00155 // ------------------------------------------------ 00156 // the parameters 00157 // ------------------------------------------------ 00158 00159 /** 00160 * Size of the median filter kernel or k-Nearest-Neighbor filter 00161 * used to smooth the quantization mask. 00162 * 00163 * Default value: 5 00164 */ 00165 int smoothingKernelSize; 00166 00167 /** 00168 * Parameters for the k-means color quantization. 00169 * 00170 * Default values: quantParameters.numberOfColors=12 00171 * quantParameters.thresholdDeltaPalette = 0.5f 00172 * quantParameters.maximalNumberOfIterations = 50 00173 */ 00174 kMColorQuantization::parameters quantParameters; 00175 00176 /** 00177 * If false, the quantization algorithms uses the last palette as 00178 * preinitialization, to speed up the segmentation of several images 00179 * showing the same object from different perspectives. If true, 00180 * the last quantization will be always ignored. 00181 * 00182 * Default value: false 00183 */ 00184 bool useAlwaysNewPalette; 00185 00186 /** 00187 * Maximal size of the border of the image to be considered to 00188 * contain almost background pixels. The real border will have the 00189 * smallest number between this parameter and one half of the 00190 * rows or columns of the image being used. 00191 * 00192 * Default value: 16 00193 */ 00194 int borderSize; 00195 00196 /** 00197 * constant to define all border elements 00198 */ 00199 static const int All; 00200 00201 /** 00202 * constant to define the top border element 00203 */ 00204 static const int Top; 00205 00206 /** 00207 * constant to define the bottom border element 00208 */ 00209 static const int Bottom; 00210 00211 /** 00212 * constant to define the left border element 00213 */ 00214 static const int Left; 00215 00216 /** 00217 * constant to define the right border element 00218 */ 00219 static const int Right; 00220 00221 /** 00222 * Decides which parts of the border should be considered for 00223 * the statistics. You can use combinations of constants Top, 00224 * Bottom, Left and Right or All to produce the wished results. 00225 * 00226 * Default value: lti::csPresegmentation::parameters::All 00227 * 00228 * Examples: 00229 * 00230 * \code 00231 * lti::csPresegmentation::parameters param; 00232 * 00233 * // to consider all but the bottom of the image: 00234 * param.borderParts = lti::csPresegmentation::parameters::All & 00235 * ~lti::csPresegmentation::parameters::Bottom; 00236 * 00237 * 00238 * // to consider only the left and right borders 00239 * param.borderParts = lti::csPresegmentation::parameters::Left | 00240 * lti::csPresegmentation::parameters::Right; 00241 * \endcode 00242 * 00243 * Bottom and Top parts have the whole width of the image. Left 00244 * and Right parts have a height equal to the height of the image minus 00245 * twice the borderSize: 00246 * 00247 * \code 00248 * _____________________ 00249 * | Top | 00250 * |___________________| 00251 * | L| |R | 00252 * | e| |i | 00253 * | f| |g | 00254 * | t| |h | 00255 * | | |t | 00256 * |__|_____________|__| 00257 * | Bottom | 00258 * |___________________| 00259 * 00260 * \endcode 00261 * 00262 */ 00263 int borderParts; 00264 00265 /** 00266 * If true, all color entries found withing the given border will be 00267 * assumed to be background, undependently of their values. If false, 00268 * only the most representative colors in the border will be assumed 00269 * to be background. 00270 * 00271 * Default value: false; 00272 */ 00273 bool forceBorderToBackground; 00274 00275 /** 00276 * If true, the value in the segmentation mask for each objekt will 00277 * correspond to the detected color palette entry in the color 00278 * quantization. If false, the value in the mask for the non-background 00279 * pixels will be 255 00280 * 00281 * Default value: false; 00282 */ 00283 bool labelObjects; 00284 00285 /** 00286 * Tolerance for background colors. 00287 * 00288 * Mean and Variance values will be computed for each 00289 * RGB-component of all palette entries that are candidates to 00290 * be background (at the beginning all entries in the palette 00291 * are background candidates). Let t be this tolerance value. 00292 * All entries in the palette, which are candidates to be 00293 * background must satisfy for each color component c in RGB: 00294 * \f$(c-mean)^2 \le t \cdot variance\f$ in order to remain 00295 * as background candidates. 00296 * 00297 * Default value: 9; 00298 */ 00299 float backgroundTolerance; 00300 00301 /** 00302 * type to specify the smoothing filter to be used 00303 */ 00304 enum eFilterType { 00305 Nothing = 0, /**< No smoothing filter should be used */ 00306 Median = 1, /**< Median Filter */ 00307 KNearest = 2 /**< K-Nearest-Neighbour Filter */ 00308 }; 00309 00310 /** 00311 * Smoothing filter to be used. 00312 * 00313 * Default: Median 00314 */ 00315 eFilterType filterType; 00316 }; 00317 00318 /** 00319 * default constructor 00320 */ 00321 csPresegmentation(); 00322 00323 /** 00324 * copy constructor 00325 * @param other the object to be copied 00326 */ 00327 csPresegmentation(const csPresegmentation& other); 00328 00329 /** 00330 * destructor 00331 */ 00332 virtual ~csPresegmentation(); 00333 00334 /** 00335 * returns the name of this type ("csPresegmentation") 00336 */ 00337 virtual const char* getTypeName() const; 00338 00339 /** 00340 * apply the presegmentation algorithm to the given image and 00341 * return a mask where the background pixels will be mark with the 00342 * value zero and the non-background pixels with a non-zero value. 00343 * 00344 * Note that some information of this segmentation is "stored" in 00345 * the internal state, which affects the next segmentation steps. 00346 * (see lti::csPresegmentation::parameters::useAlwaysNewPalette) 00347 * 00348 * @param src image with the source data. 00349 * @param mask the mask with the found objects. 00350 * @return true if apply successful or false otherwise. 00351 */ 00352 bool apply(const image& src,channel8& mask); 00353 00354 /** 00355 * copy data of "other" functor. 00356 * @param other the functor to be copied 00357 * @return a reference to this functor object 00358 */ 00359 csPresegmentation& copy(const csPresegmentation& other); 00360 00361 /** 00362 * alias for copy member 00363 * @param other the functor to be copied 00364 * @return a reference to this functor object 00365 */ 00366 csPresegmentation& operator=(const csPresegmentation& other); 00367 00368 /** 00369 * returns a pointer to a clone of this functor. 00370 */ 00371 virtual functor* clone() const; 00372 00373 /** 00374 * returns used parameters 00375 */ 00376 const parameters& getParameters() const; 00377 00378 protected: 00379 /** 00380 * last used palette 00381 */ 00382 palette lastPalette; 00383 00384 /** 00385 * valid background returns true if the given pixel lies between 00386 * the mean value and the given variance. 00387 */ 00388 inline bool validBackground(const rgbPixel& p, 00389 const trgbPixel<float>& mean, 00390 const trgbPixel<float>& var, 00391 const float& tolerance) const; 00392 }; 00393 } 00394 00395 #endif