LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiColorModelEstimator.h

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 .......: ltiColorModelEstimator.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 12.3.2002
00030  * revisions ..: $Id: ltiColorModelEstimator.h,v 1.14 2006/02/07 18:38:23 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_COLOR_MODEL_ESTIMATOR_H_
00034 #define _LTI_COLOR_MODEL_ESTIMATOR_H_
00035 
00036 #include <list>
00037 #include "ltiImage.h"
00038 #include "ltiHistogram.h"
00039 
00040 #include "ltiStatisticsFunctor.h"
00041 
00042 namespace lti {
00043 
00044   /**
00045    * This functor is used to create three-dimensional histograms for
00046    * the colors of images.  The histograms are used as color models in
00047    * functors like lti::colorModelSelector and lti::probabilityMap.
00048    *
00049    * It is possible to provide whole images (usefull to create histograms
00050    * of backgrounds, if they can be captured as a whole image), or
00051    * images with corresponding masks, that allow to consider only those
00052    * pixels with a mask value different than zero.
00053    *
00054    * It is also possible to provide directly a list of images.
00055    *
00056    * This functor generates also simple statistics for a multivariate gaussian
00057    * model for the color (mean color and covariance matrix)
00058    */
00059   class colorModelEstimator : public statisticsFunctor {
00060   public:
00061     /**
00062      * The parameters for the class colorModelEstimator
00063      */
00064     class parameters : public statisticsFunctor::parameters {
00065     public:
00066       /**
00067        * Default constructor
00068        */
00069       parameters();
00070 
00071       /**
00072        * Copy constructor
00073        * @param other the parameters object to be copied
00074        */
00075       parameters(const parameters& other);
00076 
00077       /**
00078        * Destructor
00079        */
00080       ~parameters();
00081 
00082       /**
00083        * Returns name of this type
00084        */
00085       const char* getTypeName() const;
00086 
00087       /**
00088        * Copy the contents of a parameters object
00089        * @param other the parameters object to be copied
00090        * @return a reference to this parameters object
00091        */
00092       parameters& copy(const parameters& other);
00093 
00094       /**
00095        * Copy the contents of a parameters object
00096        * @param other the parameters object to be copied
00097        * @return a reference to this parameters object
00098        */
00099       parameters& operator=(const parameters& other);
00100 
00101 
00102       /**
00103        * Returns a pointer to a clone of the parameters
00104        */
00105       virtual functor::parameters* clone() const;
00106 
00107       /**
00108        * Write the parameters in the given ioHandler
00109        * @param handler the ioHandler to be used
00110        * @param complete if true (the default) the enclosing begin/end will
00111        *        be also written, otherwise only the data block will be written.
00112        * @return true if write was successful
00113        */
00114       virtual bool write(ioHandler& handler,const bool complete=true) const;
00115 
00116       /**
00117        * Read the parameters from the given ioHandler
00118        * @param handler the ioHandler to be used
00119        * @param complete if true (the default) the enclosing begin/end will
00120        *        be also written, otherwise only the data block will be written.
00121        * @return true if write was successful
00122        */
00123       virtual bool read(ioHandler& handler,const bool complete=true);
00124 
00125 #     ifdef _LTI_MSC_6
00126       /**
00127        * This function is required by MSVC only, as a workaround for a
00128        * very awful bug, which exists since MSVC V.4.0, and still by
00129        * V.6.0 with all bugfixes (so called "service packs") remains
00130        * there...  This method is also public due to another bug, so please
00131        * NEVER EVER call this method directly: use read() instead
00132        */
00133       bool readMS(ioHandler& handler,const bool complete=true);
00134 
00135       /**
00136        * This function is required by MSVC only, as a workaround for a
00137        * very awful bug, which exists since MSVC V.4.0, and still by
00138        * V.6.0 with all bugfixes (so called "service packs") remains
00139        * there...  This method is also public due to another bug, so please
00140        * NEVER EVER call this method directly: use write() instead
00141        */
00142       bool writeMS(ioHandler& handler,const bool complete=true) const;
00143 #     endif
00144 
00145       // ------------------------------------------------
00146       // the parameters
00147       // ------------------------------------------------
00148 
00149       /**
00150        * Dimensions for the 3D color model.
00151        *
00152        * Default: (32,32,32)
00153        */
00154       rgbPixel histogramDimensions;
00155 
00156     };
00157 
00158     /**
00159      * Default constructor
00160      */
00161     colorModelEstimator();
00162 
00163     /**
00164      * Copy constructor
00165      * @param other the object to be copied
00166      */
00167     colorModelEstimator(const colorModelEstimator& other);
00168 
00169     /**
00170      * Destructor
00171      */
00172     virtual ~colorModelEstimator();
00173 
00174     /**
00175      * Returns the name of this type ("colorModelEstimator")
00176      */
00177     virtual const char* getTypeName() const;
00178 
00179     /**
00180      * Reset the internal histogram to consider new elements.
00181      * The internal model will be empty, but with the correct size.
00182      */
00183     bool reset();
00184 
00185     /**
00186      * Consider a pixel for the model.
00187      * @param pix pixel to be considered
00188      * @param times how many times should the pixel be considered
00189      * @return true if successful or false otherwise.
00190      */
00191     bool consider(const rgbPixel& pix,const int times=1);
00192 
00193     /**
00194      * Consider a pixel for the model.
00195      *
00196      * This more time consuming consider method will not only add 1 to
00197      * the "winner" bin, but will also distribute the "1" vote using 
00198      * tri-linear interpolation among the seven neighbors.
00199      *
00200      * @param pix pixel to be considered
00201      * @param times how many times should the pixel be considered
00202      * @return true if successful or false otherwise.
00203      */
00204     bool smoothConsider(const rgbPixel& pix,const int times=1);
00205 
00206     /**
00207      * Consider a three dimensional pixel value given its three components.
00208      * It is assumed that the value range of all channels lies between 0.0 and
00209      * 1.0.
00210      * @param c1 first color component.
00211      * @param c2 second color component.
00212      * @param c3 third color component.
00213      * @param times number of times the given color point must be consider.
00214      */
00215     bool consider(const float& c1,const float& c2,const float& c3,
00216                   const int times=1);
00217 
00218     /**
00219      * Consider a pixel for the model.
00220      * @param pal the color palette to be considered
00221      * @return true if successful or false otherwise.
00222      */
00223     bool consider(const palette& pal);
00224 
00225     /**
00226      * Consider all pixels in the image.
00227      * @param img image with colors to be considered
00228      * @return true if successful or false otherwise.
00229      */
00230     bool consider(const image& img);
00231 
00232     /**
00233      * Consider all pixels defined by the correspondent elements in the three
00234      * given channels.
00235      * @param c1 first channel.
00236      * @param c2 second channel.
00237      * @param c3 third channel.
00238      * @return true if successful or false otherwise.
00239      */
00240     bool consider(const channel& c1,
00241                   const channel& c2,
00242                   const channel& c3);
00243 
00244     /**
00245      * Consider the pixels in the image, for which the corresponding
00246      * pixel in the mask is not zero.  The mask and the image must
00247      * have the same size.
00248      *
00249      * @param img image with colors to be considered
00250      * @param mask channel8 with the mask.  A value of zero means, that the
00251      *             corresponding pixel in the image must be ignored.
00252      * @return true if successful or false otherwise.
00253      */
00254     bool consider(const image& img,const channel8& mask);
00255 
00256     /**
00257      * Consider the pixels in the image, for which the corresponding
00258      * pixel in the mask is not zero.  The mask and the image must
00259      * have the same size.
00260      *
00261      * @param img image with colors to be considered
00262      * @param mask imatrix with the mask.  A value of zero means, that the
00263      *             corresponding pixel in the image must be ignored.
00264      * @return true if successful or false otherwise.
00265      */
00266     bool consider(const image& img,const imatrix& mask);
00267 
00268     /**
00269      * Consider the pixels in the image, for which the corresponding
00270      * pixel in the mask is not zero.  The mask and the image must
00271      * have the same size.
00272      *
00273      * @param c1 first channel.
00274      * @param c2 second channel.
00275      * @param c3 third channel.
00276      * @param mask channel8 with the mask.  A value of zero means, that the
00277      *             corresponding pixel in the image must be ignored.
00278      * @return true if successful or false otherwise.
00279      */
00280     bool consider(const channel& c1,
00281                   const channel& c2,
00282                   const channel& c3,
00283                   const channel8& mask);
00284 
00285     /**
00286      * Consider the pixels in the image, for which the corresponding
00287      * pixel in the mask is not zero.  The mask and the image must
00288      * have the same size.
00289      *
00290      * @param c1 first channel.
00291      * @param c2 second channel.
00292      * @param c3 third channel.
00293      * @param mask imatrix with the mask.  A value of zero means, that the
00294      *             corresponding pixel in the image must be ignored.
00295      * @return true if successful or false otherwise.
00296      */
00297     bool consider(const channel& c1,
00298                   const channel& c2,
00299                   const channel& c3,
00300                   const imatrix& mask);
00301 
00302     /**
00303      * Consider the pixels in the image, for which the corresponding
00304      * pixel in the mask contains the value given by label.
00305      * The mask and the image must have the same size.
00306      *
00307      * This is seldom used.  See lti::computePalette, maybe that is
00308      * what you are looking for!
00309      *
00310      * @param img image with colors to be considered
00311      * @param mask channel8 with the mask.  
00312      * @param label only those pixel whose value in the mask are equal to
00313      *              this parameters will be considered.
00314      * @return true if successful or false otherwise.
00315      */
00316     bool consider(const image& img,const channel8& mask,const ubyte label);
00317 
00318     /**
00319      * Consider the pixels in the image, for which the corresponding
00320      * pixel in the mask contains the value given by label.
00321      * The mask and the image must have the same size.
00322      *
00323      * This is seldom used.  See lti::computePalette, maybe that is
00324      * what you are looking for!
00325      *
00326      * @param img image with colors to be considered
00327      * @param mask imatrix with the mask.  
00328      * @param label only those pixel whose value in the mask are equal to
00329      *              this parameters will be considered.
00330      * @return true if successful or false otherwise.
00331      */
00332     bool consider(const image& img,const imatrix& mask,const int label);
00333 
00334 
00335     /**
00336      * Consider all the images in the given list.
00337      *
00338      * @param imgs list of images to be considered
00339      * @return true if successful or false otherwise.
00340      */
00341     bool consider(const std::list<image>& imgs);
00342 
00343     /**
00344      * Consider all the images in the given list.  The mask list must have
00345      * the same size, and the corresponding images must also have the same
00346      * size.
00347      *
00348      * @param imgs list of images to be considered.
00349      * @param masks list of all masks.
00350      * @return true if successful or false otherwise.
00351      */
00352     bool consider(const std::list<image>& imgs,
00353                   const std::list<channel8>& masks);
00354 
00355     /**
00356      * Get a constant reference to the internal model
00357      */
00358     const thistogram<double>& getColorModel();
00359 
00360     /**
00361      * Get a copy of the internal model in the given %parameter.
00362      * @param model the color model (as 3D histogram) computed until now
00363      * @return true if apply successful or false otherwise.
00364      */
00365     bool apply(thistogram<double>& model) const;
00366 
00367     /**
00368      * Get the mean color and covariance matrix of the internal model
00369      * @param mean the mean color
00370      * @param covar covariance matrix of the color (3x3 matrix)
00371      * @return true if apply successful, false otherwise.
00372      */
00373     bool apply(trgbPixel<double>& mean,
00374                dmatrix& covar) const;
00375 
00376     /**
00377      * Copy data of "other" functor.
00378      * @param other the functor to be copied
00379      * @return a reference to this functor object
00380      */
00381     colorModelEstimator& copy(const colorModelEstimator& other);
00382 
00383     /**
00384      * Alias for copy member
00385      * @param other the functor to be copied
00386      * @return a reference to this functor object
00387      */
00388     colorModelEstimator& operator=(const colorModelEstimator& other);
00389 
00390     /**
00391      * Returns a pointer to a clone of this functor.
00392      */
00393     virtual functor* clone() const;
00394 
00395     /**
00396      * Set functor's parameters.
00397      * This member makes a copy of <em>theParam</em>: the functor
00398      * will keep its own copy of the parameters!
00399      *
00400      * This overloading will initialize the size of the internal color model
00401      * @return true if successful, false otherwise
00402      */
00403     virtual bool updateParameters();
00404 
00405     /**
00406      * Returns used parameters
00407      */
00408     const parameters& getParameters() const;
00409 
00410   protected:
00411     /**
00412      * The color model used.  If empty, it is assumed to be uninitialized.
00413      */
00414     thistogram<double> theModel;
00415 
00416     /**
00417      * Sum of colors until now
00418      */
00419     trgbPixel<double> sum;
00420 
00421     /**
00422      * Sum of outer-product of the colors until now (required for the
00423      * computation of the covariance matrix.
00424      */
00425     dmatrix sumOuter;
00426 
00427     /**
00428      * Number of pixels considered until now
00429      */
00430     int n;
00431 
00432     /**
00433      * Outer product of two colors.  The matrix MUST be 3x3 before calling
00434      * this method.
00435      */
00436     void outer(const trgbPixel<double>& a,
00437                const trgbPixel<double>& b,
00438                dmatrix& mat) const;
00439 
00440   private:
00441     /**
00442      * Iterator for dmatrices
00443      */
00444     dmatrix::iterator omit;
00445 
00446     /**
00447      * Index vector.  Attribute to reduce creation time...
00448      */
00449     ivector index;
00450 
00451     /**
00452      * Dimensions of the histogram (red)
00453      */
00454     int hdimRed;
00455 
00456     /**
00457      * Dimensions of the histogram (green)
00458      */
00459     int hdimGreen;
00460 
00461     /**
00462      * Dimensions of the histogram (blue)
00463      */
00464     int hdimBlue;
00465 
00466     /**
00467      * Equivalent to hdimRed-1;
00468      */
00469     int lastIdxRed;
00470 
00471     /**
00472      * Equivalent to hdimGreen-1;
00473      */
00474     int lastIdxGreen;
00475 
00476     /**
00477      * Equivalent to hdimBlue-1;
00478      */
00479     int lastIdxBlue;
00480   };
00481 }
00482 
00483 #endif

Generated on Sat Apr 10 15:25:15 2010 for LTI-Lib by Doxygen 1.6.1