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

ltiChrominanceMapEstimator.h

00001 /*
00002  * Copyright (C) 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 .......: ltiChrominanceModelEstimator.h
00027  * authors ....: Claudia Goenner
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 21.9.2003
00030  * revisions ..: $Id: ltiChrominanceMapEstimator.h,v 1.4 2006/02/07 18:35:55 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_CHROMINANCE_MAP_ESTIMATOR_H_
00034 #define _LTI_CHROMINANCE_MAP_ESTIMATOR_H_
00035 
00036 #include "ltiMacroSymbols.h"
00037 
00038 // only for compilers different than VC++ 6.0 available
00039 #ifdef _LTI_MSC_6
00040 
00041 #pragma message("Insufficient C++ Template Support for lti::regionGraphMeans.")
00042 #pragma message("You need a newer compiler")
00043 
00044 #else
00045 
00046 #include "cstdlib"
00047 
00048 #include "ltiImage.h"
00049 #include "ltiChrominanceModelEstimator.h"
00050 
00051 namespace lti {
00052   /**
00053    * This class computes a map between the chrominances and the most
00054    * likely object based on chrominance histograms and the Bayes Theorem.
00055    *
00056    * For details about building the colors models take a look at the
00057    * description of chrominanceModelEstimator.
00058    *
00059    * @see overlappingSets2D, chrominanceModelEstimator, probabilityMap2D,
00060    * chrominanceMapMasking
00061    */
00062   class chrominanceMapEstimator : public chrominanceModelEstimator {
00063   public:
00064     
00065     /**
00066      * the parameters for the class chrominanceModelEstimator
00067      */
00068     class parameters : public chrominanceModelEstimator::parameters {
00069     public:
00070       /**
00071        * default constructor
00072        */
00073       parameters(int dim = 0);
00074 
00075       /**
00076        * copy constructor
00077        * @param other the parameters object to be copied
00078        */
00079       parameters(const parameters& other);
00080 
00081       /**
00082        * destructor
00083        */
00084       ~parameters();
00085 
00086       /**
00087        * returns name of this type
00088        */
00089       const char* getTypeName() const;
00090 
00091       /**
00092        * copy the contents of a parameters object
00093        * @param other the parameters object to be copied
00094        * @return a reference to this parameters object
00095        */
00096       parameters& copy(const parameters& other);
00097 
00098       /**
00099        * copy the contents of a parameters object
00100        * @param other the parameters object to be copied
00101        * @return a reference to this parameters object
00102        */
00103       parameters& operator=(const parameters& other);
00104 
00105 
00106       /**
00107        * returns a pointer to a clone of the parameters
00108        */
00109       virtual functor::parameters* clone() const;
00110 
00111       /**
00112        * write the parameters in the given ioHandler
00113        * @param handler the ioHandler to be used
00114        * @param complete if true (the default) the enclosing begin/end will
00115        *        be also written, otherwise only the data block will be written.
00116        * @return true if write was successful
00117        */
00118       virtual bool write(ioHandler& handler,const bool complete=true) const;
00119 
00120       /**
00121        * read the parameters from the given ioHandler
00122        * @param handler the ioHandler to be used
00123        * @param complete if true (the default) the enclosing begin/end will
00124        *        be also written, otherwise only the data block will be written.
00125        * @return true if write was successful
00126        */
00127       virtual bool read(ioHandler& handler,const bool complete=true);
00128 
00129 #     ifdef _LTI_MSC_6
00130       /**
00131        * this function is required by MSVC only, as a workaround for a
00132        * very awful bug, which exists since MSVC V.4.0, and still by
00133        * V.6.0 with all bugfixes (so called "service packs") remains
00134        * there...  This method is also public due to another bug, so please
00135        * NEVER EVER call this method directly: use read() instead
00136        */
00137       bool readMS(ioHandler& handler,const bool complete=true);
00138 
00139       /**
00140        * this function is required by MSVC only, as a workaround for a
00141        * very awful bug, which exists since MSVC V.4.0, and still by
00142        * V.6.0 with all bugfixes (so called "service packs") remains
00143        * there...  This method is also public due to another bug, so please
00144        * NEVER EVER call this method directly: use write() instead
00145        */
00146       bool writeMS(ioHandler& handler,const bool complete=true) const;
00147 #     endif
00148 
00149       // ------------------------------------------------
00150       // the parameters
00151       // ------------------------------------------------
00152 
00153       /**
00154        * A probability that is returned for chrominances where no are very
00155        * few objects hit so far. At these chrominances the background,
00156        * referring to the first chrominance model with label 0, is
00157        * classified. To classify a foreground object its probability must
00158        * be above this threshold.
00159        *
00160        * Default: .0001.
00161        */
00162       float defaultProbability;
00163 
00164       /**
00165        * The object probabilities for each object. By default all objects
00166        * have the same probability.
00167        */
00168       genericVector<float> objectProbabilities;    
00169       
00170       /**
00171        * Automatically initialize the object probabilities with their
00172        * default values.
00173        *
00174        * Default: true.
00175        */
00176       bool autoObjectProbabilities;
00177       
00178       /**
00179        * Do not compute the probabilities for chrominances that contain
00180        * the background only. Turn on for speed up.
00181        *
00182        * Default: false.
00183        */
00184       bool excludeBackground;
00185     };
00186 
00187     /**
00188      * default constructor
00189      */
00190     chrominanceMapEstimator();
00191 
00192     /**
00193      * Construct a functor using the given parameters
00194      */
00195     chrominanceMapEstimator(const parameters& par);
00196 
00197     /**
00198      * copy constructor
00199      * @param other the object to be copied
00200      */
00201     chrominanceMapEstimator(const chrominanceMapEstimator& other);
00202 
00203     /**
00204      * destructor
00205      */
00206     virtual ~chrominanceMapEstimator();
00207 
00208     /**
00209      * returns the name of this type ("chrominanceModelEstimator")
00210      */
00211     virtual const char* getTypeName() const;
00212 
00213     /**
00214      * copy data of "other" functor.
00215      * @param other the functor to be copied
00216      * @return a reference to this functor object
00217      */
00218     chrominanceMapEstimator& copy(const chrominanceMapEstimator& other);
00219 
00220     /**
00221      * alias for copy member
00222      * @param other the functor to be copied
00223      * @return a reference to this functor object
00224      */
00225     chrominanceMapEstimator& operator=(const chrominanceMapEstimator& other);
00226 
00227     /**
00228      * returns a pointer to a clone of this functor.
00229      */
00230     virtual functor* clone() const;
00231 
00232     /**
00233      * returns used parameters
00234      */
00235     const parameters& getParameters() const;
00236 
00237     /** 
00238      * set the parameters and ensure validity 
00239      */
00240     bool setParameters(const parameters& theParams);
00241  
00242     /** 
00243      * For a given chrominance get the object label which is most likely.
00244      * For more sophisticated segmentation @see chrominanceMapMasking.
00245      * @param u refers to the columns of the chrominance map.
00246      * @param v refers to the rows of the chrominance map.
00247      * @param label the most likely object label.
00248      * @return true on success and false otherwise.
00249      */
00250     inline bool apply(ubyte u, ubyte v, int& label) const;
00251 
00252     /** 
00253      * For a given chrominance get the object label which is most likely
00254      * and its probability.
00255      * For more sophisticated segmentation @see chrominanceMapMasking.
00256      * @param u refers to the columns of the chrominance map.
00257      * @param v refers to the rows of the chrominance map.
00258      * @param label the most likely object label.
00259      * @param prob the probability of the most likely object.
00260      * @return true on success and false otherwise.
00261      */
00262     inline bool apply(ubyte u, ubyte v, int& label, float& prob) const;
00263 
00264     /** 
00265      * Recompute the chrominance map and probabilities from the chrominance
00266      * models. This method checks the parameters to determine if the entire
00267      * map is processed or only those parts that are covered by an object as
00268      * specified by the maximal chrominance sets.
00269      * @return true on success and false otherwise.
00270      */
00271     bool update();
00272     
00273     /** 
00274      * Recompute the chrominance map and probabilities from the chrominance
00275      * models. This method processes only those parts that are covered
00276      * by an object.
00277      * specified by the maximal chrominance sets.
00278      * @return true on success and false otherwise.
00279      */
00280     bool updateExclude();
00281  
00282     /** 
00283      * Recompute the chrominance map and probabilities from the chrominance
00284      * models. This method processes the entire map.
00285      * specified by the maximal chrominance sets.
00286      * @return true on success and false otherwise.
00287      */
00288     bool updateAll();
00289 
00290     /** 
00291      * Returns the chrominance map.
00292      */
00293     const channel8& getChrominanceMap() const;
00294     
00295     /** 
00296      * Returns the probabilities of the most likely object.
00297      */
00298     const channel& getProbabilityMap() const;
00299 
00300     /** 
00301      * Writes the chrominance map and probabilities to disk.
00302      * @param prefix a string containing the path or prefix to the files. If
00303      *   you specify a directory, please do not forget the /. It is not added
00304      *   automatically.
00305      * @param colors  color each object as specified by this vector.
00306      * @return true on success and false otherwise.
00307      */
00308     bool writeChrominanceMap(const std::string& prefix,
00309            const vector<rgbPixel>& colors) const;
00310 
00311   protected:
00312     /**
00313      * Initializes stuff needed by the update methods.
00314      * @param histIts iterator pointing at the chrominance model
00315      * @param objNumEntries the number of entries in each histogram.
00316      */
00317     void initUpdate(std::vector<histogram2D::const_iterator>& histIts,
00318         dvector& objNumEntries) const;
00319     /*
00320      * Get a pointer to the parameters.
00321      */
00322     parameters& getWritableParameters();
00323 
00324     /**
00325      * the color map
00326      */
00327     channel8 m_colorMap;
00328 
00329     /**
00330      * probabilities of the best object
00331      */
00332     channel m_probabilityMap;
00333     
00334     /**
00335      * Special care when error in setParameters during construction.
00336      * Historic and complicated code, but it works.
00337      */
00338     bool m_constructMap;
00339 
00340   };
00341 
00342   // -------------------------------------------------------------------
00343   // The apply-methods!
00344   // -------------------------------------------------------------------
00345   
00346   inline bool chrominanceMapEstimator::apply(ubyte u, ubyte v,
00347                int& label) const {
00348     
00349     label = m_colorMap.at(u,v);
00350     return true;
00351   };
00352   
00353   inline bool chrominanceMapEstimator::apply(ubyte u, ubyte v, 
00354               int& label, float& prob) const {
00355     
00356     label = m_colorMap.at(u,v);
00357     prob  = m_probabilityMap.at(u,v);
00358     return true;
00359   };
00360 }
00361 
00362 #endif
00363 
00364 #endif

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