CVR-Lib last update 20 Sep 2009

cvrUsePalette.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 
00042 /**
00043  * \file   cvrUsePalette.h
00044  *         Contains class to create a color image from a labeled mask and
00045  *         a palette of colors (one for each label).
00046  * \author Pablo Alvarado
00047  * \date   8.5.2001
00048  *
00049  * $Id: cvrUsePalette.h,v 1.4 2006/01/13 04:59:04 alvarado Exp $
00050  */
00051 
00052 #ifndef _CVR_USE_PALETTE_H_
00053 #define _CVR_USE_PALETTE_H_
00054 
00055 #include "cvrImage.h"
00056 #include "cvrVector.h"
00057 #include "cvrFunctor.h"
00058 #include "cvrKdTree.h"
00059 
00060 namespace cvr {
00061   /**
00062    *
00063    * Use color or gray-valued palette.
00064    *
00065    * This class has two uses:
00066 
00067    * - For a given labeled mask (which is usually an cvr::imatrix or a
00068    *   cvr::channel8) and a palette, replace in a destination image each region
00069    *   label by the corresponding palette entry.  This is a simple but frequent
00070    *   used operation when dealing with segmentation results, color
00071    *   quantization results, and other image partitioning operators.
00072    *
00073 
00074    * - For a given image and a palette, find which palette entry in the image
00075    *   is the most similar to each corresponding pixel, and generate an index
00076    *   image (cvr::imatrix or cvr::channel8) containing those indices.
00077    *
00078    * For the second operation mode you can choose in the parameters to use
00079    * a cvr::kdTree in order to avoid a "brute-force" search.
00080    *
00081    * @see cvr::computePalette
00082    *
00083    * @ingroup gRegionAnalysis
00084    */
00085   class usePalette : public functor {
00086   public:
00087     /**
00088      * the parameters for the class usePalette
00089      */
00090     class parameters : public functor::parameters {
00091     public:
00092       /**
00093        * default constructor
00094        */
00095       parameters();
00096 
00097       /**
00098        * copy constructor
00099        * @param other the parameters object to be copied
00100        */
00101       parameters(const parameters& other);
00102 
00103       /**
00104        * destructor
00105        */
00106       ~parameters();
00107 
00108       /**
00109        * returns name of this type
00110        */
00111       const std::string& name() const;
00112 
00113       /**
00114        * copy the contents of a parameters object
00115        * @param other the parameters object to be copied
00116        * @return a reference to this parameters object
00117        */
00118       parameters& copy(const parameters& other);
00119 
00120       /**
00121        * copy the contents of a parameters object
00122        * @param other the parameters object to be copied
00123        * @return a reference to this parameters object
00124        */
00125       parameters& operator=(const parameters& other);
00126 
00127       /**
00128        * returns a pointer to a clone of the parameters
00129        */
00130       virtual parameters* clone() const;
00131 
00132       /**
00133        * returns a pointer to a new instance of the parameters
00134        */
00135       virtual parameters* newInstance() 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        * write the parameters in 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       // ------------------------------------------------
00156       // the parameters
00157       // ------------------------------------------------
00158 
00159       /**
00160        * The palette to be used.  This is the vector of rgbaPixel
00161        * values to be used.
00162        *
00163        * Default Palete: gray-value palette (256 entries, where
00164        *                 ( colors.at(i) == rgbPixel(i,i,i) )
00165        */
00166       palette colors;
00167 
00168       /**
00169        * To efficiently get the best entry in a palette, other techniques
00170        * besides the "brute force" linear search exist.  This functor can
00171        * also use k-d trees.
00172        *
00173        * This makes only sense if the same palette is going to be used
00174        * with different images, or if the images are big enough to compensate
00175        * the overhead of generating the k-d tree, task which only depends on
00176        * the size of the color palette.
00177        *
00178        * If this attribute is set to true, the linear search will be used,
00179        * avoiding the computation of the k-d Tree.
00180        *
00181        * If set to false, a k-d Tree will be computed from the \c colors
00182        * attribute at a time determined by the attribute \c kdTreeOnDemand.
00183        *
00184        * Default value: false (i.e. use k-d trees)
00185        */
00186       bool linearSearch;
00187 
00188       /**
00189        * For those apply methods that get directly a palette or k-d Tree,
00190        * this parameter will be ignored.
00191        *
00192        * For all other methods that use the color entries in the
00193        * \c colors attribute this parameter determines when to compute
00194        * the k-d Tree (only if linearSearch is set to false):
00195        * - If true, the k-d Tree is computed the first time an apply() method
00196        *   is called.  This can take little bit time, and the total time
00197        *   execution for this first apply can be longer than a simple
00198        *   linear search (when the images are too small, for example).
00199        * - If false, the k-d Tree is computed when the parameters are set.
00200        *
00201        * If you want to apply the same palette to many different images, set
00202        * this parameter to false in order to force the computation of the
00203        * k-d Tree off-line.
00204        *
00205        * Default value: false
00206        */
00207       bool kdTreeOnDemand;
00208 
00209       /**
00210        * Each leaf node of the k-d Tree can contain a number of
00211        * palette entries greater than one.  A linear search method
00212        * takes place on sets of maximal this size.
00213        *
00214        * Default value: 16
00215        */
00216       int bucketSize;
00217     };
00218 
00219     /**
00220      * default constructor
00221      */
00222     usePalette();
00223 
00224     /**
00225      * copy constructor
00226      * @param other the object to be copied
00227      */
00228     usePalette(const usePalette& other);
00229 
00230     /**
00231      * destructor
00232      */
00233     virtual ~usePalette();
00234 
00235     /**
00236      * returns the name of this type
00237      */
00238     virtual const std::string& name() const;
00239 
00240     /**
00241      * Takes the matrix<ubyte> and uses its elements as index for the palette
00242      * in the parameters.  The result will be left in a new image.
00243      *
00244      * @param chnl the matrix<ubyte> with the indices for the palette
00245      * @param other the resulting image
00246      * @return true if successful, false otherwise
00247      */
00248     bool apply(const matrix<ubyte>& chnl,image& other) const;
00249 
00250     /**
00251      * Takes the matrix<int> and uses its elements as index for the palette in
00252      * the parameters.  The result will be left in a new image.
00253      *
00254      * @param chnl the matrix<int> with the indices for the palette
00255      * @param other the resulting image
00256      * @return true if successful, false otherwise
00257      */
00258     bool apply(const matrix<int>& chnl,image& other) const;
00259 
00260     /**
00261      * Takes the matrix<ubyte> and uses its elements as index for the given
00262      * palette.  The result will be left in a new image.
00263      *
00264      * @param chnl the matrix<ubyte> with the indices for the palette
00265      * @param thePalette palette to be used
00266      * @param other the resulting image
00267      * @return true if successful, false otherwise
00268      */
00269     bool apply(const matrix<ubyte>& chnl,
00270                const palette& thePalette,
00271                image& other) const;
00272 
00273     /**
00274      * Takes the matrix<int> and uses its elements as index for the given
00275      * palette.  The result will be left in a new image.
00276      *
00277      * @param chnl the matrix<int> with the indices for the palette
00278      * @param thePalette palette to be used
00279      * @param other the resulting image
00280      * @return true if successful, false otherwise
00281      */
00282     bool apply(const matrix<int>& chnl,
00283                const palette& thePalette,
00284                image& other) const;
00285 
00286     /**
00287      * Use gray valued palette.
00288      *
00289      * This method is provided for completeness.  It is not for color images,
00290      * but for gray valued ones, but since the principle used is the same than
00291      * with colored palettes, the method seems to be in a proper place here.
00292      * It complements the corresponding apply method of cvr::computePalette to
00293      * compute gray-valued palettes from channels.
00294      *
00295      * @param regions regions map, containing for each region a palette index
00296      *        entry.
00297      * @param values gray-valued palette entries.  For each region with index
00298      *        \e i, the output channel will contain values.at(i).
00299      * @param dest output channel, the result of using the gray-valued palette
00300      *        for the given regions map will be left here.
00301      * @return true if successful, false otherwise.
00302      */
00303     bool apply(const matrix<ubyte>& regions,
00304                const vector<float>& values,
00305                fmatrix& dest) const;
00306 
00307     /**
00308      * Use gray valued palette.
00309      *
00310      * This method is provided for completeness.  It is not for color images,
00311      * but for gray valued ones, but since the principle used is the same than
00312      * with colored palettes, the method seems to be in a proper place here.
00313      * It complements the corresponding apply method of cvr::computePalette to
00314      * compute gray-valued palettes from channels.
00315      *
00316      * @param regions regions map, containing for each region a palette index
00317      *        entry.
00318      * @param values gray-valued palette entries.  For each region with index
00319      *        \e i, the output channel will contain values.at(i).
00320      * @param dest output channel, the result of using the gray-valued palette
00321      *        for the given regions map will be left here.
00322      * @return true if successful, false otherwise.
00323      */
00324     bool apply(const matrix<int>& regions,
00325                const vector<float>& values,
00326                fmatrix& dest) const;
00327 
00328     /**
00329      * For each pixel in the given image, find the "best" entry in the palette
00330      * found in the parameters and leave the index of the entry in the
00331      * correspondig pixel of the matrix<ubyte>.
00332      *
00333      * @param img the color image (true color)
00334      * @param chnl the indices for each pixel in img of the correponding
00335      *             palette entry in the parameters object
00336      */
00337     bool apply(const image& img, matrix<ubyte>& chnl);
00338 
00339     /**
00340      * Find for each pixel in the given image the best entry in the
00341      * palette found in the parameters and leave the index of the
00342      * entry in the correspondig pixel of the matrix<int>.
00343      *
00344      * @param img the color image (true color)
00345      * @param chnl the indices for each pixel in img of the correponding
00346      *             palette entry in the parameters object
00347      */
00348     bool apply(const image& img, matrix<int>& chnl);
00349 
00350     /**
00351      * Find for each pixel in the given image the best entry of the
00352      * given palette and leave the index of that entry in the
00353      * correspondig pixel of the matrix<ubyte>.
00354      *
00355      * Note that with this method the parameters::colors attribute will be
00356      * ignored.  The internal k-d Tree will be computed using thePalette
00357      * given here.
00358      *
00359      * @param img the color image (true color)
00360      * @param thePalette a color palette.  The index of the most similar color
00361      *                   entry within this palette to each pixel of the
00362      *                   <code>img</code> will be left in the <code>chnl</code>
00363      * @param chnl the indices for each pixel in img of the correponding
00364      *             palette entry in <code>thePalette</code>
00365      */
00366     bool apply(const image& img,const palette& thePalette,matrix<ubyte>& chnl);
00367 
00368     /**
00369      * Find for each pixel in the given image the best entry of the
00370      * given palette and leave the index of that entry in the
00371      * correspondig pixel of the matrix<int>.
00372      *
00373      * Note that with this method the parameters::colors attribute will be
00374      * ignored.
00375      *
00376      * @param img the color image (true color)
00377      * @param thePalette a color palette.  The index of the most similar color
00378      *                   entry within this palette to each pixel of the
00379      *                   <code>img</code> will be left in the <code>chnl</code>
00380      * @param chnl the indices for each pixel in img of the correponding
00381      *             palette entry in <code>thePalette</code>
00382      */
00383     bool apply(const image& img, const palette& thePalette, matrix<int>& chnl);
00384 
00385     /**
00386      * Find for each pixel in the given image the nearest entry in the
00387      * given k-d Tree and leave the data component of that entry in the
00388      * correspondig pixel of the matrix<ubyte>.
00389      *
00390      * Note that with this method the parameters::colors attribute or
00391      * previously computed k-d trees will be ignored.
00392      *
00393      * @param img the color image (true color)
00394      * @param tree k-d Tree coding a color palette.  The index in
00395      *             the integer data component of the tree elements will
00396      *             be assumed to be the index of a color palette.
00397      * @param chnl the data of the nearest component for each pixel
00398      *             in img will be stored in the correponding pixel of this
00399      *             channel.
00400      */
00401     bool apply(const image& img,
00402                const kdTree<rgbaPixel,int>& tree,
00403                matrix<ubyte>& chnl) const;
00404 
00405     /**
00406      * Find for each pixel in the given image the nearest entry in the
00407      * given k-d Tree and leave the data component of that entry in the
00408      * correspondig pixel of the matrix<ubyte>.
00409      *
00410      * Note that with this method the parameters::colors attribute or
00411      * previously computed k-d trees will be ignored.
00412      *
00413      * @param img the color image (true color)
00414      * @param tree k-d Tree coding a color palette.  The index in
00415      *             the integer data component of the tree elements will
00416      *             be assumed to be the index of a color palette.
00417      * @param chnl the data of the nearest component for each pixel
00418      *             in img will be stored in the correponding pixel of this
00419      *             channel.
00420      */
00421     bool apply(const image& img,
00422                const kdTree<rgbaPixel,int>& tree,
00423                matrix<int>& chnl) const;
00424 
00425     /**
00426      * Get a constant reference to the internal k-d tree.
00427      */
00428     const kdTree<rgbaPixel,int>& getKdTree() const;
00429 
00430     /**
00431      * Copy data of "other" functor.
00432      * @param other the functor to be copied
00433      * @return a reference to this functor object
00434      */
00435     usePalette& copy(const usePalette& other);
00436 
00437     /**
00438      * Returns a pointer to a clone of this functor.
00439      */
00440     virtual usePalette* clone() const;
00441 
00442     /**
00443      * Returns a pointer to a new instance of this functor.
00444      */
00445     virtual usePalette* newInstance() const;
00446 
00447     /**
00448      * Returns used parameters
00449      */
00450     const parameters& getParameters() const;
00451 
00452     /**
00453      * Set functor's parameters.
00454      * This member makes a copy of <em>theParam</em>: the functor
00455      * will keep its own copy of the parameters!
00456      * @return true if successful, false otherwise
00457      */
00458     virtual bool updateParameters();
00459 
00460   protected:
00461     /**
00462      * The parameters palette is converted (if desired) into this
00463      * k-d Tree.
00464      *
00465      * The "data" type int states for the entry index.
00466      */
00467     kdTree<rgbaPixel,int> tree_;
00468 
00469     /**
00470      * Build the k-d Tree from the given color palette
00471      */
00472     bool buildKdTree(const palette& pal);
00473 
00474     /**
00475      * Flag to indicate if the k-d Tree has already been build or not.
00476      */
00477     bool treeBuilded_;
00478   };
00479 }
00480 
00481 #endif
00482 

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8