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

ltiUsePalette.h

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 .......: ltiUsePalette.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 8.5.2001
00030  * revisions ..: $Id: ltiUsePalette.h,v 1.12 2006/02/08 11:59:12 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_USE_PALETTE_H_
00034 #define _LTI_USE_PALETTE_H_
00035 
00036 #include "ltiImage.h"
00037 #include "ltiVector.h"
00038 #include "ltiFunctor.h"
00039 #include "ltiKdTree.h"
00040 
00041 namespace lti {
00042   /**
00043    * Use color or gray-valued palette.
00044    *
00045    * This class has two uses:
00046    * - For a given region index map, which is usually an lti::imatrix or 
00047    *   a lti::channel8, and a palette, replace in a destination image
00048    *   each region label by the corresponding palette entry.  This is a simple
00049    *   but frequent operation when dealing with segmentation results, color
00050    *   quantization results, and other image partitioning operators.
00051    * - For a given image and a palette, find which palette entry in the image
00052    *   is the most similar to each corresponding pixel, and generate an index
00053    *   image (lti::imatrix or lti::channel8) containing those indices.
00054    *
00055    * For the second operation mode you can choose in the parameters to use
00056    * a lti::kdTree in order to avoid a "brute-force" search.
00057    *
00058    * @see lti::computePalette
00059    *
00060    * @ingroup gRegionAnalysis
00061    */
00062   class usePalette : public functor {
00063   public:
00064     /**
00065      * the parameters for the class usePalette
00066      */
00067     class parameters : public functor::parameters {
00068     public:
00069       /**
00070        * default constructor
00071        */
00072       parameters();
00073 
00074       /**
00075        * copy constructor
00076        * @param other the parameters object to be copied
00077        */
00078       parameters(const parameters& other);
00079 
00080       /**
00081        * destructor
00082        */
00083       ~parameters();
00084 
00085       /**
00086        * returns name of this type
00087        */
00088       const char* getTypeName() const;
00089 
00090       /**
00091        * copy the contents of a parameters object
00092        * @param other the parameters object to be copied
00093        * @return a reference to this parameters object
00094        */
00095       parameters& copy(const parameters& other);
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& operator=(const parameters& other);
00103 
00104       /**
00105        * returns a pointer to a clone of the parameters
00106        */
00107       virtual functor::parameters* clone() const;
00108 
00109       /**
00110        * write the parameters in the given ioHandler
00111        * @param handler the ioHandler to be used
00112        * @param complete if true (the default) the enclosing begin/end will
00113        *        be also written, otherwise only the data block will be written.
00114        * @return true if write was successful
00115        */
00116       virtual bool write(ioHandler& handler,const bool complete=true) const;
00117 
00118       /**
00119        * write the parameters in the given ioHandler
00120        * @param handler the ioHandler to be used
00121        * @param complete if true (the default) the enclosing begin/end will
00122        *        be also written, otherwise only the data block will be written.
00123        * @return true if write was successful
00124        */
00125       virtual bool read(ioHandler& handler,const bool complete=true);
00126 
00127 #     ifdef _LTI_MSC_6
00128       /**
00129        * this function is required by MSVC only, as a workaround for a
00130        * very awful bug, which exists since MSVC V.4.0, and still by
00131        * V.6.0 with all bugfixes (so called "service packs") remains
00132        * there...  This method is also public due to another bug, so please
00133        * NEVER EVER call this method directly: use read() instead
00134        */
00135       bool readMS(ioHandler& handler,const bool complete=true);
00136 
00137       /**
00138        * this function is required by MSVC only, as a workaround for a
00139        * very awful bug, which exists since MSVC V.4.0, and still by
00140        * V.6.0 with all bugfixes (so called "service packs") remains
00141        * there...  This method is also public due to another bug, so please
00142        * NEVER EVER call this method directly: use write() instead
00143        */
00144       bool writeMS(ioHandler& handler,const bool complete=true) const;
00145 #     endif
00146 
00147       // ------------------------------------------------
00148       // the parameters
00149       // ------------------------------------------------
00150 
00151       /**
00152        * The palette to be used.  This is the vector of rgbPixel
00153        * values to be used.
00154        *
00155        * Default Palete: gray-value palette (256 entries, where
00156        *                 ( colors.at(i) == rgbPixel(i,i,i) )
00157        */
00158       palette colors;
00159 
00160       /**
00161        * To efficiently get the best entry in a palette, other techniques
00162        * besides the "brute force" linear search exist.  This functor can
00163        * also use k-d trees.  
00164        *
00165        * This makes only sense if the same palette is going to be used
00166        * with different images, or if the images are big enough to compensate
00167        * the overhead of generating the k-d tree, task which only depends on
00168        * the size of the color palette.
00169        *
00170        * If this attribute is set to true, the linear search will be used,
00171        * avoiding the computation of the k-d Tree.
00172        *
00173        * If set to false, a k-d Tree will be computed from the \c colors
00174        * attribute at a time determined by the attribute \c kdTreeOnDemand.
00175        *
00176        * Default value: false (i.e. use k-d trees)
00177        */
00178       bool linearSearch;
00179 
00180       /**
00181        * For those apply methods that get directly a palette or k-d Tree,
00182        * this parameter will be ignored.
00183        *
00184        * For all other methods that use the color entries in the 
00185        * \c colors attribute this parameter determines when to compute
00186        * the k-d Tree (only if linearSearch is set to false):
00187        * - If true, the k-d Tree is computed the first time an apply() method
00188        *   is called.  This can take little bit time, and the total time
00189        *   execution for this first apply can be longer than a simple
00190        *   linear search (when the images are too small, for example).
00191        * - If false, the k-d Tree is computed when the parameters are set.
00192        * 
00193        * If you want to apply the same palette to many different images, set
00194        * this parameter to false in order to force the computation of the
00195        * k-d Tree off-line.
00196        *
00197        * Default value: false
00198        */
00199       bool kdTreeOnDemand;
00200 
00201       /**
00202        * Each leaf node of the k-d Tree can contain a number of
00203        * palette entries greater than one.  A linear search method
00204        * takes place on sets of maximal this size.
00205        *
00206        * Default value: 16
00207        */
00208       int bucketSize;
00209     };
00210 
00211     /**
00212      * default constructor
00213      */
00214     usePalette();
00215 
00216     /**
00217      * copy constructor
00218      * @param other the object to be copied
00219      */
00220     usePalette(const usePalette& other);
00221 
00222     /**
00223      * destructor
00224      */
00225     virtual ~usePalette();
00226 
00227     /**
00228      * returns the name of this type ("usePalette")
00229      */
00230     virtual const char* getTypeName() const;
00231 
00232     /**
00233      * Takes the matrix<ubyte> and uses its elements as index for the palette
00234      * in the parameters.  The result will be left in a new image.
00235      *
00236      * @param chnl the matrix<ubyte> with the indices for the palette
00237      * @param other the resulting image
00238      * @return true if successful, false otherwise
00239      */
00240     bool apply(const matrix<ubyte>& chnl,image& other) const;
00241 
00242     /**
00243      * Takes the matrix<int> and uses its elements as index for the palette in
00244      * the parameters.  The result will be left in a new image.
00245      *
00246      * @param chnl the matrix<int> with the indices for the palette
00247      * @param other the resulting image
00248      * @return true if successful, false otherwise
00249      */
00250     bool apply(const matrix<int>& chnl,image& other) const;
00251 
00252     /**
00253      * Takes the matrix<ubyte> and uses its elements as index for the given
00254      * palette.  The result will be left in a new image.
00255      *
00256      * @param chnl the matrix<ubyte> with the indices for the palette
00257      * @param thePalette palette to be used
00258      * @param other the resulting image
00259      * @return true if successful, false otherwise
00260      */
00261     bool apply(const matrix<ubyte>& chnl,
00262                const palette& thePalette,
00263                      image& other) const;
00264 
00265     /**
00266      * Takes the matrix<int> and uses its elements as index for the given
00267      * palette.  The result will be left in a new image.
00268      *
00269      * @param chnl the matrix<int> with the indices for the palette
00270      * @param thePalette palette to be used
00271      * @param other the resulting image
00272      * @return true if successful, false otherwise
00273      */
00274     bool apply(const matrix<int>& chnl,
00275                const palette& thePalette,
00276                      image& other) const;
00277 
00278     /**
00279      * Use gray valued palette.
00280      *
00281      * This method is provided for completeness.  It is not for color images,
00282      * but for gray valued ones, but since the principle used is the same than
00283      * with colored palettes, the method seems to be in a proper place here.
00284      * It complements the corresponding apply method of lti::computePalette to
00285      * compute gray-valued palettes from channels.
00286      *
00287      * @param regions regions map, containing for each region a palette index
00288      *        entry.
00289      * @param values gray-valued palette entries.  For each region with index
00290      *        \e i, the output channel will contain values.at(i).
00291      * @param dest output channel, the result of using the gray-valued palette
00292      *        for the given regions map will be left here.
00293      * @return true if successful, false otherwise.
00294      */
00295     bool apply(const matrix<ubyte>& regions,
00296                const vector<float>& values,
00297                fmatrix& dest) const;
00298 
00299     /**
00300      * Use gray valued palette.
00301      *
00302      * This method is provided for completeness.  It is not for color images,
00303      * but for gray valued ones, but since the principle used is the same than
00304      * with colored palettes, the method seems to be in a proper place here.
00305      * It complements the corresponding apply method of lti::computePalette to
00306      * compute gray-valued palettes from channels.
00307      *
00308      * @param regions regions map, containing for each region a palette index
00309      *        entry.
00310      * @param values gray-valued palette entries.  For each region with index
00311      *        \e i, the output channel will contain values.at(i).
00312      * @param dest output channel, the result of using the gray-valued palette
00313      *        for the given regions map will be left here.
00314      * @return true if successful, false otherwise.
00315      */
00316     bool apply(const matrix<int>& regions,
00317                const vector<float>& values,
00318                fmatrix& dest) const;
00319 
00320     /**
00321      * Find for each pixel in the given image the best entry in the palette
00322      * found in the parameters and leave the index of the entry in the
00323      * correspondig pixel of the matrix<ubyte>.
00324      *
00325      * @param img the color image (true color)
00326      * @param chnl the indices for each pixel in img of the correponding
00327      *             palette entry in the parameters object
00328      */
00329     bool apply(const image& img, matrix<ubyte>& chnl);
00330 
00331     /**
00332      * Find for each pixel in the given image the best entry in the
00333      * palette found in the parameters and leave the index of the
00334      * entry in the correspondig pixel of the matrix<int>.
00335      *
00336      * @param img the color image (true color)
00337      * @param chnl the indices for each pixel in img of the correponding
00338      *             palette entry in the parameters object
00339      */
00340     bool apply(const image& img, matrix<int>& chnl);
00341 
00342     /**
00343      * Find for each pixel in the given image the best entry of the
00344      * given palette and leave the index of that entry in the
00345      * correspondig pixel of the matrix<ubyte>.
00346      *
00347      * Note that with this method the parameters::colors attribute will be
00348      * ignored.  The internal k-d Tree will be computed using thePalette 
00349      * given here.
00350      *
00351      * @param img the color image (true color)
00352      * @param thePalette a color palette.  The index of the most similar color
00353      *                   entry within this palette to each pixel of the
00354      *                   <code>img</code> will be left in the <code>chnl</code>
00355      * @param chnl the indices for each pixel in img of the correponding
00356      *             palette entry in <code>thePalette</code>
00357      */
00358     bool apply(const image& img,const palette& thePalette,matrix<ubyte>& chnl);
00359 
00360     /**
00361      * Find for each pixel in the given image the best entry of the
00362      * given palette and leave the index of that entry in the
00363      * correspondig pixel of the matrix<int>.
00364      *
00365      * Note that with this method the parameters::colors attribute will be
00366      * ignored.
00367      *
00368      * @param img the color image (true color)
00369      * @param thePalette a color palette.  The index of the most similar color
00370      *                   entry within this palette to each pixel of the
00371      *                   <code>img</code> will be left in the <code>chnl</code>
00372      * @param chnl the indices for each pixel in img of the correponding
00373      *             palette entry in <code>thePalette</code>
00374      */
00375     bool apply(const image& img, const palette& thePalette, matrix<int>& chnl);
00376 
00377     /**
00378      * Find for each pixel in the given image the nearest entry in the
00379      * given k-d Tree and leave the data component of that entry in the
00380      * correspondig pixel of the matrix<ubyte>.
00381      *
00382      * Note that with this method the parameters::colors attribute or
00383      * previously computed k-d trees will be ignored.
00384      *
00385      * @param img the color image (true color)
00386      * @param tree k-d Tree coding a color palette.  The index in
00387      *             the integer data component of the tree elements will
00388      *             be assumed to be the index of a color palette.
00389      * @param chnl the data of the nearest component for each pixel 
00390      *             in img will be stored in the correponding pixel of this
00391      *             channel.
00392      */
00393     bool apply(const image& img,
00394                const kdTree<rgbPixel,int>& tree,
00395                matrix<ubyte>& chnl) const;
00396 
00397     /**
00398      * Find for each pixel in the given image the nearest entry in the
00399      * given k-d Tree and leave the data component of that entry in the
00400      * correspondig pixel of the matrix<ubyte>.
00401      *
00402      * Note that with this method the parameters::colors attribute or
00403      * previously computed k-d trees will be ignored.
00404      *
00405      * @param img the color image (true color)
00406      * @param tree k-d Tree coding a color palette.  The index in
00407      *             the integer data component of the tree elements will
00408      *             be assumed to be the index of a color palette.
00409      * @param chnl the data of the nearest component for each pixel 
00410      *             in img will be stored in the correponding pixel of this
00411      *             channel.
00412      */
00413     bool apply(const image& img,
00414                const kdTree<rgbPixel,int>& tree,
00415                matrix<int>& chnl) const;
00416 
00417     /**
00418      * Get a constant reference to the internal k-d tree.
00419      */
00420     const kdTree<rgbPixel,int>& getKdTree() const;
00421 
00422     /**
00423      * Copy data of "other" functor.
00424      * @param other the functor to be copied
00425      * @return a reference to this functor object
00426      */
00427     usePalette& copy(const usePalette& other);
00428 
00429     /**
00430      * Returns a pointer to a clone of this functor.
00431      */
00432     virtual functor* clone() const;
00433 
00434     /**
00435      * Returns used parameters
00436      */
00437     const parameters& getParameters() const;
00438 
00439     /**
00440      * Set functor's parameters.
00441      * This member makes a copy of <em>theParam</em>: the functor
00442      * will keep its own copy of the parameters!
00443      * @return true if successful, false otherwise
00444      */
00445     virtual bool updateParameters();
00446 
00447   protected:
00448     /**
00449      * The parameters palette is converted (if desired) into this
00450      * k-d Tree.
00451      *
00452      * The "data" type int states for the entry index.
00453      */ 
00454     kdTree<rgbPixel,int> tree;
00455 
00456     /**
00457      * Build the k-d Tree from the given color palette
00458      */
00459     bool buildKdTree(const palette& pal);
00460 
00461     /**
00462      * Flag to indicate if the k-d Tree has already been build or not.
00463      */
00464     bool treeBuilded;
00465   };
00466 }
00467 
00468 #endif

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