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

ltiOverlappingSets2D.h

00001 /*
00002  * Copyright (C) 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  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00025  * file .......: ltiOverlappingSets2D.h
00026  * authors ....: Claudia Goenner
00027  * organization: LTI, RWTH Aachen
00028  * creation ...: 8.2.2004
00029  * revisions ..: $Id: ltiOverlappingSets2D.h,v 1.4 2006/02/08 11:36:57 ltilib Exp $
00030  */
00031 
00032 #ifndef _LTI_OVERLAPPING_SETS_2D_H_
00033 #define _LTI_OVERLAPPING_SETS_2D_H_
00034 
00035 #include "ltiMacroSymbols.h"
00036 
00037 // only for compilers different than VC++ 6.0 available
00038 #ifdef _LTI_MSC_6
00039 
00040 #pragma message("Insufficient C++ Template Support for lti::regionGraphMeans.")
00041 #pragma message("You need a newer compiler")
00042 
00043 #else
00044 
00045 #include "ltiImage.h"
00046 #include "cstdlib"
00047 #include "list"
00048 
00049 #include "ltiFunctor.h"
00050 
00051 #undef _LTI_DEBUG
00052 //#define _LTI_DEBUG 2
00053 #include "ltiDebug.h"
00054 #if defined(_LTI_DEBUG) && (_LTI_DEBUG>1)
00055 #include "ltiViewer.h"
00056 #include <iostream>
00057 using std::cout;
00058 using std::endl;
00059 using std::flush;
00060 #endif
00061 
00062 namespace lti {
00063 
00064   /**
00065    * Considering 2d sets, which may overlap, one might ask in which sets a
00066    * particular 2d point is contained. This class creates a single
00067    * 2d look up table (LUT), which stores the ids of all sets that cover
00068    * a particular position. This is useful to exclude any other set
00069    * from further processing. For example an image contains several objects
00070    * and each object has certain 2d features, which are known a priori.
00071    * If a specific feature is extracted, an application would only process
00072    * the objects which contain the feature.
00073    * The classes for estimating chrominance models use this framework for
00074    * robustness and run time efficiency. If it is known that an object is
00075    * red then only red chrominances are processed.
00076    *
00077    * The LUT entry is coded in an additive manner. E.g. for a total of
00078    * three sets the LUT will contain values from 0 to 7. If a position is
00079    * covered by no set, the LUT contains a 0. If a position is contained
00080    * in all sets the LUT's entry is 7. If a position is contained in the
00081    * first and third set the LUT returns 3. If a position is contained in
00082    * the third set only the LUT contains 4.
00083    *
00084    * The maximum number of sets is limited by the capacity of the data
00085    * type int. Caution, this class is only efficient if a few sets overlap.
00086    *
00087    * @see chrominanceMapEstimator, chrominanceModelEstimator
00088    */
00089   class overlappingSets2D : public functor {
00090   public:
00091     /**
00092      * The parameters for the class overlappingSets2D
00093      */
00094     class parameters : public functor::parameters {
00095     public:
00096       /**
00097        * Default constructor
00098        */
00099       parameters();
00100 
00101       /**
00102        * Copy constructor
00103        * @param other the parameters object to be copied
00104        */
00105       parameters(const parameters& other);
00106 
00107       /**
00108        * Destructor
00109        */
00110       ~parameters();
00111 
00112       /**
00113        * Returns name of this type
00114        */
00115       const char* getTypeName() const;
00116 
00117       /**
00118        * Copy the contents of a parameters object
00119        * @param other the parameters object to be copied
00120        * @return a reference to this parameters object
00121        */
00122       parameters& copy(const parameters& other);
00123 
00124       /**
00125        * Copy the contents of a parameters object
00126        * @param other the parameters object to be copied
00127        * @return a reference to this parameters object
00128        */
00129       parameters& operator=(const parameters& other);
00130 
00131 
00132       /**
00133        * Returns a pointer to a clone of the parameters
00134        */
00135       virtual functor::parameters* clone() const;
00136 
00137       /**
00138        * Write the parameters in the given ioHandler
00139        * @param handler the ioHandler to be used
00140        * @param complete if true (the default) the enclosing begin/end will
00141        *        be also written, otherwise only the data block will be written.
00142        * @return true if write was successful
00143        */
00144       virtual bool write(ioHandler& handler,const bool complete=true) const;
00145 
00146       /**
00147        * Read the parameters from the given ioHandler
00148        * @param handler the ioHandler to be used
00149        * @param complete if true (the default) the enclosing begin/end will
00150        *        be also written, otherwise only the data block will be written.
00151        * @return true if write was successful
00152        */
00153       virtual bool read(ioHandler& handler,const bool complete=true);
00154 
00155 #     ifdef _LTI_MSC_6
00156       /**
00157        * This function is required by MSVC only, as a workaround for a
00158        * very awful bug, which exists since MSVC V.4.0, and still by
00159        * V.6.0 with all bugfixes (so called "service packs") remains
00160        * there...  This method is also public due to another bug, so please
00161        * NEVER EVER call this method directly: use read() instead
00162        */
00163       bool readMS(ioHandler& handler,const bool complete=true);
00164 
00165       /**
00166        * This function is required by MSVC only, as a workaround for a
00167        * very awful bug, which exists since MSVC V.4.0, and still by
00168        * V.6.0 with all bugfixes (so called "service packs") remains
00169        * there...  This method is also public due to another bug, so please
00170        * NEVER EVER call this method directly: use write() instead
00171        */
00172       bool writeMS(ioHandler& handler,const bool complete=true) const;
00173 #     endif
00174 
00175       // ------------------------------------------------
00176       // the parameters
00177       // ------------------------------------------------ 
00178 
00179       /**
00180        * All ids below this threshold will be marked as discarded.
00181        * This is useful to blend out objects ore sets from processing
00182        * which relate to "default" actions only. Internally ids start
00183        * with 1.
00184        *
00185        * Default: 0 (disabled).
00186        */
00187       ubyte discardThreshold;
00188 
00189       /**
00190        * The size of the internal table storing the contained sets.
00191        * If this value is modified the internal table will be scaled
00192        * to match the new size using nearest neighbor interpolation.
00193        *
00194        * Default: 256.
00195        */
00196       ipoint size;
00197   
00198 #if defined(_LTI_DEBUG) && (_LTI_DEBUG>0)
00199       //good for debugging
00200       bool getAllContainedSets(int value, std::list<int>& dest);
00201 #endif
00202 
00203     };
00204 
00205     // -------------------------------------------------------------------
00206 
00207     /**
00208      * Default constructor
00209      */
00210     overlappingSets2D();
00211 
00212     /**
00213      * Construct a functor using the given parameters
00214      */
00215     overlappingSets2D(const parameters& par);
00216 
00217     /**
00218      * Copy constructor
00219      * @param other the object to be copied
00220      */
00221     overlappingSets2D(const overlappingSets2D& other);
00222 
00223     /**
00224      * Destructor
00225      */
00226     virtual ~overlappingSets2D();
00227 
00228     /**
00229      * Returns the name of this type ("overlappingSets2D")
00230      */
00231     virtual const char* getTypeName() const;
00232  
00233     /**
00234      * Copy data of "other" functor.
00235      * @param other the functor to be copied
00236      * @return a reference to this functor object
00237      */
00238     overlappingSets2D& copy(const overlappingSets2D& other);
00239 
00240     /**
00241      * Alias for copy member
00242      * @param other the functor to be copied
00243      * @return a reference to this functor object
00244      */
00245     overlappingSets2D& operator=(const overlappingSets2D& other);
00246 
00247     /**
00248      * Returns a pointer to a clone of this functor.
00249      */
00250     virtual functor* clone() const;
00251 
00252     /**
00253      * Returns used parameters
00254      */
00255     const parameters& getParameters() const;
00256 
00257     /**
00258      * Set used parameters
00259      */
00260     bool setParameters(const parameters& theParams);
00261 
00262     /**
00263      * Initialize the LUT with the overlapping sets from disk.
00264      * Each set is interpreted as an image and loaded accordingly. Regions
00265      * that are contained in a set must have intensities above 0.
00266      * @param prefix a string containing the path or prefix to the files. If
00267      *   you specify a directory, please do not forget the /. It is not added
00268      *   automatically.
00269      * @param names  a vector with the filename of each image or set. The
00270      *   internal set ids correspond with the order of this vector,
00271      *   but start from 1.
00272      * @returns true on success and false otherwise.
00273      */
00274     bool readObjectTable(const std::string& prefix,
00275        const std::vector<std::string>& names);
00276     
00277     /**
00278      * Initialize the LUT with overlapping sets coded as channel8. Regions
00279      * that are contained in a set must have values above 0.
00280      * @param src  a vector containing one channel8 for each set. The
00281      *   internal set ids correspond with the order of this vector,
00282      *   but start from 1.
00283      * @returns true on success and false otherwise.
00284      */
00285     bool setObjectTable(const std::vector<channel8>& src);
00286     
00287     /**
00288      * Initialize the LUT with overlapping sets coded as channel8. Regions
00289      * that are contained in a set must have intensities above 0.
00290      * @param src  a vector containing one image for each set. The
00291      *   internal set ids correspond with the order of this vector,
00292      *   but start from 1.
00293      * @returns true on success and false otherwise.
00294      */
00295     bool setObjectTable(const std::vector<image>& src);
00296 
00297     /**
00298      * Returns the LUT with the overlapping sets.
00299      */
00300     const imatrix& getObjectTable() const;
00301     
00302     /**
00303      * Returns true if position (u,v) is contained in the set associated
00304      * with the provided label. Fast access which does not check if the 
00305      * LUT is initialized.
00306      * @param label the label of the set, which starts from 0. Please note
00307      * that the label and the internal id are not identical.
00308      * @param u the column of the LUT.
00309      * @param v the row of the LUT.
00310      * @returns true if position (u,v) is contained.
00311      */
00312     inline bool isContained(int label, int u, int v) const;
00313       
00314     /**
00315      * Returns true if position (u,v) is not contained
00316      * in any set, whose internal id is above the discard threshold.
00317      * Fast access which does not check if the LUT is initialized.
00318      * @param u the column of the LUT.
00319      * @param v the row of the LUT.
00320      * @returns true if position (u,v) is not contained in any set.
00321      */
00322     inline bool doDiscard(int u, int v) const;
00323         
00324     /**
00325      * Checks if the LUT for the overlapping sets is initialized.
00326      * @returns true on success and false otherwise.
00327      */
00328     bool isInitialized() const;
00329 
00330     /**
00331      * Checks if position (u,v) is contained in the set associated
00332      * with the provided label.
00333      * @param label the label of the set, which starts from 0. Please note
00334      * that the label and the internal id are not identical.
00335      * @param u the column of the LUT.
00336      * @param v the row of the LUT.
00337      * @param isContained true if (u,v) is contained in the set and false
00338      *   otherwise.
00339      * @returns true on success and false otherwise.
00340      */
00341     bool apply(int label, int u, int v, bool& isContained) const;
00342 
00343     /**
00344      * Checks if position (u,v) is not contained
00345      * in any set, whose internal id is above the discard threshold.
00346      * @param u the column of the LUT.
00347      * @param v the row of the LUT.
00348      * @param doDiscard true if (u,v) is not contained in any set and false
00349      *   otherwise.
00350      * @returns true on success and false otherwise.
00351      */
00352     bool apply(int u, int v, bool& doDiscard) const;
00353 
00354   protected:
00355 
00356     /**
00357      * Initialize the LUT with overlapping sets coded as channel8. Regions
00358      * that are contained in a set must have values above 0.
00359      * @param src  a vector containing one channel8 for each set. The
00360      *   internal set ids correspond with the order of this vector,
00361      *   but start from 1.
00362      * @returns true on success and false otherwise.
00363      */
00364     bool initObjectTable(const std::vector<channel8>& src);
00365     
00366     /**
00367      * Scale a channel8 to match the LUT size specified in the parameters.
00368      * @param src original channel8.
00369      * @param dest scaled channel8.
00370      * @returns true on success and false otherwise.
00371      */
00372     bool scale(const channel8& src, channel8& dest) const;
00373     
00374     /**
00375      * Scale a channel8 to match the LUT size specified in the parameters.
00376      * @param src channel8 to be scaled. The result is left here too.
00377      * @returns true on success and false otherwise.
00378      */
00379     bool scale(channel8& src) const;
00380     
00381     /**
00382      * Adjust the size of the internal LUT to fit the parameters. The LUT
00383      * will be scaled using nearest neighbor interpolation.
00384      */
00385     bool resize();
00386  
00387     /**
00388      * The LUT with the overlapping sets.
00389      */
00390     imatrix m_objectTable;
00391     
00392     /**
00393      * A table which stores the id which is associated with each label,
00394      * defined by id = 2 power label.
00395      */
00396     std::vector<int> m_id;
00397 
00398     /**
00399      * A copy of the discardThreshold in the parameters. This is here
00400      * for efficiency.
00401      */
00402     int m_discardThreshold;
00403   };
00404   
00405   // -------------------------------------------------------------------
00406 
00407   inline bool overlappingSets2D::doDiscard(int u, int v ) const {
00408 
00409     return ( m_objectTable.at(v,u) <= m_discardThreshold );
00410   }
00411 
00412   inline bool overlappingSets2D::isContained(int label, int u, int v) const {
00413 
00414     int value ( m_objectTable.at(v,u) );
00415 
00416     //this is here for speedup
00417     const int test ( m_id[label] );
00418 
00419     //the 3 most frequent cases
00420     if ( test > value ) {
00421       return false;
00422     }
00423     if ( test == value-1 ) { //only this object and background in set
00424       return true;
00425     }
00426     if ( test == value ) { //only this object in set
00427       return true;
00428     }
00429 
00430     //check all other possible overlaps
00431 
00432     //get the largest multiple of 2 that is smaller than value
00433     int id ( 1 );
00434     while ( id < value ) {
00435       id *= 2;
00436     }
00437     if ( id > value ) {
00438       id = id>>1;
00439     }
00440 
00441     while ( value > 0 ) {
00442       value -= id;
00443       if ( test == id ) {
00444   return true;
00445       }
00446       //get the largest multiple of 2 that is smaller than value
00447       id = 1;
00448       while ( id < value ) {
00449   id *= 2;
00450       }
00451       if ( id > value )  {
00452   id = id>>1;
00453       }
00454     }
00455 
00456     return false;
00457   }
00458 }
00459 
00460 #include "ltiUndebug.h"
00461 
00462 #endif
00463 #endif

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