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

ltiSimilarityMatrix.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 .......: ltiSimilarityMatrix.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 18.3.2002
00030  * revisions ..: $Id: ltiSimilarityMatrix.h,v 1.8 2006/02/08 11:50:34 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_SIMILARITY_MATRIX_H_
00034 #define _LTI_SIMILARITY_MATRIX_H_
00035 
00036 #include "ltiContour.h"
00037 #include "ltiImage.h"
00038 #include "ltiFunctor.h"
00039 
00040 namespace lti {
00041   /**
00042    * Given a set of connected image regions (coded in a labeled mask
00043    * or std::list of area point lists) and the original color image,
00044    * this functor will compute a similarity measure between the
00045    * objects, and will code them in a similarity matrix.
00046    *
00047    * The first approach (BorderBased) will compute the sum of the
00048    * square root of the difference of the colors at the borders between two
00049    * both objects.  The second approach (AreaBased) will compute the
00050    * difference of the mean colors for each object.
00051    *
00052    * See also \ref segmPage
00053    */
00054   class similarityMatrix : public functor {
00055   public:
00056     /**
00057      * the parameters for the class similarityMatrix
00058      */
00059     class parameters : public functor::parameters {
00060     public:
00061       /**
00062        * default constructor
00063        */
00064       parameters();
00065 
00066       /**
00067        * copy constructor
00068        * @param other the parameters object to be copied
00069        */
00070       parameters(const parameters& other);
00071 
00072       /**
00073        * destructor
00074        */
00075       ~parameters();
00076 
00077       /**
00078        * returns name of this type
00079        */
00080       const char* getTypeName() const;
00081 
00082       /**
00083        * copy the contents of a parameters object
00084        * @param other the parameters object to be copied
00085        * @return a reference to this parameters object
00086        */
00087       parameters& copy(const parameters& other);
00088 
00089       /**
00090        * copy the contents of a parameters object
00091        * @param other the parameters object to be copied
00092        * @return a reference to this parameters object
00093        */
00094       parameters& operator=(const parameters& other);
00095 
00096 
00097       /**
00098        * returns a pointer to a clone of the parameters
00099        */
00100       virtual functor::parameters* clone() const;
00101 
00102       /**
00103        * write the parameters in the given ioHandler
00104        * @param handler the ioHandler to be used
00105        * @param complete if true (the default) the enclosing begin/end will
00106        *        be also written, otherwise only the data block will be written.
00107        * @return true if write was successful
00108        */
00109       virtual bool write(ioHandler& handler,const bool complete=true) const;
00110 
00111       /**
00112        * read the parameters from 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 read(ioHandler& handler,const bool complete=true);
00119 
00120 #     ifdef _LTI_MSC_6
00121       /**
00122        * this function is required by MSVC only, as a workaround for a
00123        * very awful bug, which exists since MSVC V.4.0, and still by
00124        * V.6.0 with all bugfixes (so called "service packs") remains
00125        * there...  This method is also public due to another bug, so please
00126        * NEVER EVER call this method directly: use read() instead
00127        */
00128       bool readMS(ioHandler& handler,const bool complete=true);
00129 
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 write() instead
00136        */
00137       bool writeMS(ioHandler& handler,const bool complete=true) const;
00138 #     endif
00139 
00140       // ------------------------------------------------
00141       // the parameters
00142       // ------------------------------------------------
00143 
00144       /**
00145        * type to specify which kind of color similarity should be
00146        * computed.
00147        */
00148       enum eModeType {
00149         BorderBased, /**< compute the similarity measure using the mean
00150                       *   value of square of the color differences at the
00151                       *   border between two objects
00152                       */
00153         AreaBased    /**< compute the similarity measure using the magnitude
00154                       *   of the difference of the mean colors of each object
00155                       */
00156       };
00157 
00158       /**
00159        * Color similarity modus (see eModusType)
00160        *
00161        * Default value: BorderBased
00162        */
00163       eModeType mode;
00164 
00165       /**
00166        * function pointer used when evaluating the distance between two
00167        * colors.
00168        *
00169        * Default value: lti::similarityMatrix::chromaticity2
00170        */
00171       double (*distFunction)(const rgbPixel&,const rgbPixel&);
00172 
00173     };
00174 
00175     /**
00176      * default constructor
00177      */
00178     similarityMatrix();
00179 
00180     /**
00181      * copy constructor
00182      * @param other the object to be copied
00183      */
00184     similarityMatrix(const similarityMatrix& other);
00185 
00186     /**
00187      * destructor
00188      */
00189     virtual ~similarityMatrix();
00190 
00191     /**
00192      * returns the name of this type ("similarityMatrix")
00193      */
00194     virtual const char* getTypeName() const;
00195 
00196     /**
00197      * operates on the given %parameter.
00198      * @param img original image.  All points in the list of areaPoints must
00199      *        lie inside the image, or an exception will be thrown.
00200      * @param nObj number of objects in the mask (without background).
00201      *             Is expected here in order to
00202      *             reduce its computation from the mask.  This value should be
00203      *             equal to the maximum value in the mask, i.e. the number of
00204      *             elements of the area points list used to generate the mask.
00205      * @param lmask label mask computed from the area points
00206      * @param colSimMat color similarity matrix
00207      * @param geoSimMat geometry similarity measure
00208      * @return true if apply successful or false otherwise.
00209      */
00210     bool apply(const image& img,
00211                const int& nObj,
00212          const imatrix& lmask,
00213                dmatrix& colSimMat,
00214                dmatrix& geoSimMat) const;
00215 
00216 
00217     /**
00218      * operates on the given %parameter.
00219      * @param img original image.  All points in the list of areaPoints must
00220      *        lie inside the image, or an exception will be thrown.
00221      * @param lapts list of area points.
00222      * @param colSimMat color similarity matrix
00223      * @param geoSimMat geometry similarity measure
00224      * @param lmask label mask computed from the area points
00225      * @return true if apply successful or false otherwise.
00226      */
00227     bool apply(const image& img,
00228                const std::list<areaPoints>& lapts,
00229                dmatrix& colSimMat,
00230                dmatrix& geoSimMat,
00231                imatrix& lmask) const;
00232 
00233 
00234     /**
00235      * computes for the given mask which labels are used in neighbor objects
00236      * for the given label.
00237      * @param nObj number of objects in the mask (without background).
00238      *             Is expected here in order to
00239      *             reduce its computation from the mask.  This value should be
00240      *             equal to the maximum value in the mask, i.e. the number of
00241      *             elements of the area points list used to generate the mask.
00242      * @param lmask label mask computed from the area points
00243      * @param label label for which the neighbors need to be found
00244      * @param neighborLabels resulting vector
00245      */
00246     bool neighbors(const int& nObj,
00247                    const imatrix& lmask,
00248                    const int& label,
00249                    ivector& neighborLabels) const;
00250 
00251     /**
00252      * copy data of "other" functor.
00253      * @param other the functor to be copied
00254      * @return a reference to this functor object
00255      */
00256     similarityMatrix& copy(const similarityMatrix& other);
00257 
00258     /**
00259      * alias for copy member
00260      * @param other the functor to be copied
00261      * @return a reference to this functor object
00262      */
00263     similarityMatrix& operator=(const similarityMatrix& other);
00264 
00265     /**
00266      * returns a pointer to a clone of this functor.
00267      */
00268     virtual functor* clone() const;
00269 
00270     /**
00271      * returns used parameters
00272      */
00273     const parameters& getParameters() const;
00274 
00275     /**
00276      * @name Color Distances
00277      */
00278     //@{
00279 
00280     /**
00281      * This measure compares the differences between the color channels
00282      * (red-green)^2, (red-blue)^2 and (green-blue)^2 for each pixel,
00283      * and then the square of the differences between these values for
00284      * a and b.
00285      */
00286     static double euclideanLLL2(const rgbPixel& a,
00287         const rgbPixel& b);
00288     /**
00289      * square of the euclidean distance in RGB color space, i.e.
00290      * this returns a.distanceSqr(b)
00291      */
00292     static double euclideanRGB2(const rgbPixel& a,
00293                                 const rgbPixel& b);
00294 
00295     /**
00296      * euclidean distance in the rg color space, ignoring the
00297      * intensity channel.
00298      */
00299     static double chromaticity2(const rgbPixel& a,
00300                                 const rgbPixel& b);
00301 
00302     /**
00303      * distance of the pixels in the HSV color space.  The value
00304      * channel is ignored.  The angular distance of hue is always normed
00305      * from 0 to 255.  The Saturation value is also normed.
00306      *
00307      * Also usefull to ignore shadow based differences.
00308      */
00309     static double hueSaturation(const rgbPixel& a,
00310                                 const rgbPixel& b);
00311 
00312     /**
00313      * distance of the pixels in the HSV color space, where only the hue
00314      * channel is considered.  The angular distance of hue is always normed
00315      * from 0 to 255.
00316      *
00317      * Also usefull to ignore shadow based differences.
00318      */
00319     static double hue(const rgbPixel& a,
00320                       const rgbPixel& b);
00321 
00322     /**
00323      * returns always 1.0
00324      * Usefull to create a matrix to know which areas are neighbors
00325      */
00326     static double neighbor(const rgbPixel& a,
00327          const rgbPixel& b);
00328 
00329     //@}
00330 
00331   protected:
00332 
00333     /**
00334      * create a mask from the area points.
00335      * The mask must have the correct dimensions before calling this method.
00336      */
00337     void createMask(const std::list<areaPoints>& apts,
00338                     imatrix& mask) const;
00339 
00340     /**
00341      * create the border similarity matrix.
00342      * The size of the similarity matrix must be set before calling this
00343      * method.
00344      *
00345      * @param img original color image
00346      * @param mask labeled segmentation mask, created with createMask()
00347      * @param colBorderSimMat border color similarity mask, the size must be
00348      *                        set previously
00349      * @param colAreaSimMat area color similarity
00350      * @param geoSimMat geomatrical similarity measure
00351      */
00352     void similarity(const image& img,
00353                     const imatrix& mask,
00354                     dmatrix& colBorderSimMat,
00355                     dmatrix& colAreaSimMat,
00356                     dmatrix& geoSimMat) const;
00357 
00358   };
00359 }
00360 
00361 #endif

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