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

ltiRegionGraphMeans.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 .......: ltiRegionGraphFunctor.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 25.10.2003
00030  * revisions ..: $Id: ltiRegionGraphMeans.h,v 1.3 2006/02/08 11:44:43 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_REGION_GRAPH_MEANS_H_
00034 #define _LTI_REGION_GRAPH_MEANS_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 "ltiImage.h"
00047 #include "ltiRegionGraphFunctor.h"
00048 #include "ltiL2Distance.h"
00049 #include <string>
00050 
00051 namespace lti {
00052 
00053   /**
00054    * Type for adjacency graph nodes containing the mean values of each
00055    * region.
00056    *
00057    * The template value specifies the data type representing a "mean", or 
00058    * better said, \a n times the mean, with \a n the number of pixels in the
00059    * region.
00060    *
00061    * The type \c T can be for instance trgbPixel<float> or just a
00062    * scalar \c double or \c float, but in general it is a type with a
00063    * default constructor that initialize to zero or a neutral element,
00064    * and also accepts the operator+=, and operator/(int).
00065    */
00066   template <class T>
00067   class regionGraphMeansNode : public ioObject {
00068   public:
00069     /**
00070      * Type used to represent the mean value
00071      */
00072     typedef T value_type;
00073 
00074     /**
00075      * Default constructor initialize class with "zero" state
00076      */
00077     regionGraphMeansNode();
00078 
00079     /**
00080      * Default constructor initialize class with "zero" state
00081      */
00082     ~regionGraphMeansNode();
00083 
00084     /**
00085      * Copy constructor
00086      */
00087     regionGraphMeansNode(const regionGraphMeansNode<T>& other);
00088 
00089     /**
00090      * Operator+= required by the interface to combine two nodes
00091      */
00092     regionGraphMeansNode<T>& 
00093     operator+=(const regionGraphMeansNode<T>& other);
00094 
00095     /**
00096      * Assignment operator
00097      */
00098     regionGraphMeansNode<T>&
00099     operator=(const regionGraphMeansNode<T>& other);
00100 
00101     /**
00102      * Consider one element to the means computation.
00103      */
00104     regionGraphMeansNode<T>& consider(const T& elem);
00105 
00106     /**
00107      * Compute the mean value
00108      */
00109     T computeMean() const;
00110 
00111     /**
00112      * Return the number of points considered
00113      */
00114     int size() const;
00115 
00116     /**
00117      * Read node from ioHandler
00118      */
00119     bool read(ioHandler& handler,const bool complete=true);
00120 
00121     /**
00122      * Write node to ioHandler
00123      */
00124     bool write(ioHandler& handler,const bool complete=true) const;
00125 
00126   protected:
00127 
00128     /**
00129      * Contains the sum of the elements considered until now.
00130      */
00131     T sumOfElements;
00132 
00133     /**
00134      * Number of elements considered until now.
00135      */
00136     int n;    
00137   };
00138   
00139   /**
00140    * Compute the weight between two scalar nodes as the absolute value
00141    * of the difference
00142    */
00143   class regionGraphScalarMeanDistance {
00144   public:    
00145     typedef regionGraphMeansNode<float> node_type;
00146     float operator()(const node_type& a,
00147                      const node_type& b,
00148                      const int& data) const;
00149   };
00150     
00151   /**
00152    * Compute the weight between two scalar nodes as the absolute value
00153    * of the difference
00154    */
00155   class regionGraphScalarHarisDistance {
00156   public:    
00157     typedef regionGraphMeansNode<float> node_type;
00158     float operator()(const node_type& a,
00159                      const node_type& b,
00160                      const int& data) const;
00161   };
00162 
00163   /**
00164    * Compute the weight between two color point nodes as the euclidean
00165    * distance between both points.
00166    */
00167   class regionGraphColorMeanDistance {
00168   public:    
00169     typedef regionGraphMeansNode< trgbPixel<float> > node_type;
00170     float operator()(const node_type& a,
00171                      const node_type& b,
00172                      const int& data) const;
00173   };
00174 
00175   /**
00176    * Compute the weight between two color point nodes as the euclidean
00177    * distance between both points.
00178    */
00179   class regionGraphColorHarisDistance {
00180   public:    
00181     typedef regionGraphMeansNode< trgbPixel<float> > node_type;
00182     float operator()(const node_type& a,
00183                      const node_type& b,
00184                      const int& data) const;
00185   };
00186 
00187 
00188   /**
00189    * Functor to manipulate graphs of adjacent image regions, where the
00190    * nodes of the graphs contain the mean value of the region represented by
00191    * the node.
00192    *
00193    * The template F is the distance computation functor required by the graph.
00194    */
00195   template<class F>
00196   class regionGraphColor
00197     : public regionGraphFunctor< 
00198                       adjacencyGraph< regionGraphMeansNode< trgbPixel<float> >,
00199                                       float,
00200                                       int,
00201                                       F,
00202                                       symmetricEdgeTraits<float> > > {
00203   public:
00204     /**
00205      * Parent class shortcut
00206      */
00207     typedef regionGraphFunctor< 
00208                       adjacencyGraph< regionGraphMeansNode< trgbPixel<float> >,
00209                                       float,
00210                                       int,
00211                                       F,
00212                                       symmetricEdgeTraits<float> > > parent;
00213 
00214     typedef typename parent::parameters parameters;
00215     typedef typename parent::graph_type graph_type;
00216     typedef typename parent::weight_type weight_type;
00217     typedef typename parent::edge_data_type edge_data_type;
00218     typedef typename parent::node_type node_type;
00219 
00220     /**
00221      * default constructor
00222      */
00223     regionGraphColor();
00224 
00225     /**
00226      * Construct a functor using the given parameters
00227      */
00228     regionGraphColor(const parameters& par);
00229 
00230     /**
00231      * copy constructor
00232      * @param other the object to be copied
00233      */
00234     regionGraphColor(const regionGraphColor& other);
00235 
00236     /**
00237      * destructor
00238      */
00239     virtual ~regionGraphColor();
00240 
00241     /**
00242      * returns the name of this type ("regionGraphColorMean")
00243      */
00244     virtual const char* getTypeName() const;
00245 
00246     /**
00247      * returns a pointer to a clone of this functor.
00248      */
00249     virtual functor* clone() const;
00250     
00251     /**
00252      * Generate a graph representation for the given \a image using the
00253      * partition in \a regions and considering only the labels above or
00254      * equal \a minLabel.  The nodes of the graph contain at the end enough
00255      * information to compute the color mean values of each region.
00256      */
00257     bool apply(const matrix<int>& regions,
00258                const image& image,
00259                const int minLabel,
00260                graph_type& graph);
00261  
00262     /**
00263      * Generate a graph representation for the given \a image using the
00264      * partition in \a regions and considering only the labels above or
00265      * equal \a minLabel. The nodes of the graph contain at the end enough
00266      * information to compute the color mean values of each region.
00267      */
00268     bool apply(const matrix<int>& regions,
00269                const channel& c1,
00270                const channel& c2,
00271                const channel& c3,
00272                const int minLabel,
00273                graph_type& graph);
00274   
00275     /**
00276      * Generate a graph representation for the given \a image using the
00277      * partition in \a regions and considering only the labels above or
00278      * equal \a minLabel.  The nodes of the graph contain at the end enough
00279      * information to compute the color mean values of each region.
00280      *
00281      * This is an alias.  You can also directly call the corresponding
00282      * apply method.
00283      */
00284     bool generate(const matrix<int>& regions,
00285                   const image& image,
00286                   const int minLabel,
00287                   graph_type& graph);
00288  
00289     /**
00290      * Generate a graph representation for the given \a image using the
00291      * partition in \a regions and considering only the labels above or
00292      * equal \a minLabel. The nodes of the graph contain at the end enough
00293      * information to compute the color mean values of each region.
00294      *
00295      * This is an alias.  You can also directly call the corresponding
00296      * apply method.
00297      */
00298     bool generate(const matrix<int>& regions,
00299                   const channel& c1,
00300                   const channel& c2,
00301                   const channel& c3,
00302                   const int minLabel,
00303                   graph_type& graph);
00304 
00305     /**
00306      * Alias for merge()
00307      */
00308     bool apply(const weight_type& threshold,
00309                graph_type& graph,
00310                ivector& equivalences);
00311 
00312     /**
00313      * Alias for merge() taking the threshold from the parameters
00314      */
00315     bool apply(graph_type& graph,
00316                ivector& equivalences);
00317 
00318     /**
00319      * Alias for merge()
00320      */
00321     bool apply(const weight_type& threshold,
00322                const int minLabel,
00323                graph_type& graph,
00324                ivector& equivalences);
00325 
00326     /**
00327      * Alias for merge() taking the threshold from the parameters
00328      */
00329     bool apply(const int minLabel,
00330                graph_type& graph,
00331                ivector& equivalences);
00332 
00333   protected:
00334     /**
00335      * Alias for generate()
00336      */
00337     bool apply(const matrix<int>& regions,
00338                const int minLabel,
00339                graph_type& graph);
00340 
00341     /**
00342      * @name Reimplemented virtual methods
00343      */
00344     //@{
00345 
00346     /**
00347      * Check if the internal data is compatible with the region mask.
00348      *
00349      * The weights between region nodes can be computed from very different
00350      * information sources.  Usually, this information is coded in images and
00351      * channels also extracted from the same image from which the regions map
00352      * was computed.  Therefore, it is important to provide a way to check if
00353      * the internal data is compatible with the regions map, since both must
00354      * be provided by the user at different times.
00355      *
00356      * @param regionsSize size of the original image and region mask.
00357      *                    This allows to check for channels and other data 
00358      *                    in the original image size format.
00359      * @param maxRegionIndex maximum region index employed.  This is used
00360      *                       to check if the vectors with information per
00361      *                       region have the appropriate size.
00362      * @return true if the internal data is compatible, false if there 
00363      *         are problems.
00364      */
00365     virtual bool checkInternalData(const point& regionsSize,
00366                                    const int maxRegionIndex) const;
00367 
00368     /**
00369      * For each two neighbor pixels that belong to different regions, this
00370      * method is called to compute the edge data value in a sequential manner.
00371      * It will be assumed, that the default constructor of the
00372      * graph_type::edge_type::data_type "resets" correctly the data of the
00373      * edge.
00374      *
00375      * @param p1 coordinates of pixel belonging to one region
00376      * @param p2 coordinates of pixel belonging to another region
00377      * @param edgeData reference to data structure of the edge, where
00378      *        some values can be accumulated or set.
00379      * @return true if successful, false otherwise.
00380      */
00381     virtual bool considerForEdgeData(const point& p1,
00382                                      const point& p2,
00383                                      edge_data_type& edgeData);
00384 
00385     /**
00386      * For each pixel in each region, this method is called once to 
00387      * compute statistics or other data for each node.
00388      *
00389      * @param p1 coordinates of the current pixel
00390      * @param label identification label for the region to which the pixel
00391      *              belongs.
00392      * @param nodeData reference to the data structure of the node, where
00393      *                 some values can be accumulated or set.
00394      * @return true if successful, false otherwise.
00395      */
00396     virtual bool considerForNodeData(const point& p1,
00397                                      const int label,
00398                                      node_type& nodeData);
00399     
00400     //@}
00401 
00402     /**
00403      * @name Internal data
00404      */
00405     //@{
00406     channel c1;
00407     channel c2;
00408     channel c3;
00409     //@}
00410     
00411 
00412   };
00413 
00414   /**
00415    * Functor to manipulate graphs of adjacent image regions, where the
00416    * nodes of the graphs contain the mean value of the region represented by
00417    * the node.
00418    *
00419    * The template F is the distance computation functor required by the graph.
00420    */
00421   template<class F = regionGraphScalarMeanDistance>
00422   class regionGraphGray
00423     : public regionGraphFunctor< 
00424                       adjacencyGraph< regionGraphMeansNode< float >,
00425                                       float,
00426                                       int,
00427                                       F,
00428                                       symmetricEdgeTraits<float> > > {
00429   public:
00430  
00431     /**
00432      * Type definitions of the parent class
00433      */
00434     //@{
00435     typedef regionGraphFunctor< 
00436                       adjacencyGraph< regionGraphMeansNode< float >,
00437                                       float,
00438                                       int,
00439                                       F,
00440                                       symmetricEdgeTraits<float> > > parent;
00441 
00442     typedef typename parent::parameters parameters;
00443     typedef typename parent::graph_type graph_type;
00444     typedef typename parent::weight_type weight_type;
00445     typedef typename parent::edge_data_type edge_data_type;
00446     typedef typename parent::node_type node_type;
00447     //@}
00448     
00449     /**
00450      * default constructor
00451      */
00452     regionGraphGray();
00453 
00454     /**
00455      * Construct a functor using the given parameters
00456      */
00457     regionGraphGray(const parameters& par);
00458 
00459     /**
00460      * copy constructor
00461      * @param other the object to be copied
00462      */
00463     regionGraphGray(const regionGraphGray& other);
00464 
00465     /**
00466      * destructor
00467      */
00468     virtual ~regionGraphGray();
00469 
00470     /**
00471      * returns the name of this type ("regionGraphGrayMean")
00472      */
00473     virtual const char* getTypeName() const;
00474 
00475     /**
00476      * returns a pointer to a clone of this functor.
00477      */
00478     virtual functor* clone() const;
00479     
00480     /**
00481      * Alias for generate()
00482      */
00483     bool apply(const matrix<int>& regions,
00484                const channel& chnl,
00485                const int minLabel,
00486                graph_type& graph);
00487  
00488     /**
00489      * Alias for merge()
00490      */
00491     bool apply(const weight_type& threshold,
00492                graph_type& graph,
00493                ivector& equivalences);
00494 
00495     /**
00496      * Alias for merge() taking the threshold from the parameters
00497      */
00498     bool apply(graph_type& graph,
00499                ivector& equivalences);
00500 
00501     /**
00502      * Alias for merge()
00503      */
00504     bool apply(const weight_type& threshold,
00505                const int minLabel,
00506                graph_type& graph,
00507                ivector& equivalences);
00508 
00509     /**
00510      * Alias for merge() taking the threshold from the parameters
00511      */
00512     bool apply(const int minLabel,
00513                graph_type& graph,
00514                ivector& equivalences);
00515 
00516   protected:
00517     /**
00518      * Alias for generate()
00519      */
00520     bool apply(const matrix<int>& regions,
00521                const int minLabel,
00522                graph_type& graph);
00523 
00524     /**
00525      * @name Methods to be reimplemented
00526      */
00527     //@{
00528 
00529     /**
00530      * Check if the internal data is compatible with the region mask.
00531      *
00532      * The weights between region nodes can be computed from very different
00533      * information sources.  Usually, this information is coded in images and
00534      * channels also extracted from the same image from which the regions map
00535      * was computed.  Therefore, it is important to provide a way to check if
00536      * the internal data is compatible with the regions map, since both must
00537      * be provided by the user at different times.
00538      *
00539      * @param regionsSize size of the original image and region mask.
00540      *                    This allows to check for channels and other data 
00541      *                    in the original image size format.
00542      * @param maxRegionIndex maximum region index employed.  This is used
00543      *                       to check if the vectors with information per
00544      *                       region have the appropriate size.
00545      * @return true if the internal data is compatible, false if there 
00546      *         are problems.
00547      */
00548     virtual bool checkInternalData(const point& regionsSize,
00549                                    const int maxRegionIndex) const;
00550 
00551     /**
00552      * For each two neighbor pixels that belong to different regions, this
00553      * method is called to compute the edge data value in a sequential manner.
00554      * It will be assumed, that the default constructor of the
00555      * graph_type::edge_type::data_type "resets" correctly the data of the
00556      * edge.
00557      *
00558      * @param p1 coordinates of pixel belonging to one region
00559      * @param p2 coordinates of pixel belonging to another region
00560      * @param edgeData reference to data structure of the edge, where
00561      *        some values can be accumulated or set.
00562      * @return true if successful, false otherwise.
00563      */
00564     virtual bool considerForEdgeData(const point& p1,
00565                                      const point& p2,
00566                                      edge_data_type& edgeData);
00567 
00568     /**
00569      * For each pixel in each region, this method is called once to 
00570      * compute statistics or other data for each node.
00571      *
00572      * @param p1 coordinates of the current pixel
00573      * @param label identification label for the region to which the pixel
00574      *              belongs.
00575      * @param nodeData reference to the data structure of the node, where
00576      *                 some values can be accumulated or set.
00577      * @return true if successful, false otherwise.
00578      */
00579     virtual bool considerForNodeData(const point& p1,
00580                                      const int label,
00581                                      node_type& nodeData);
00582     
00583     //@}
00584 
00585     /**
00586      * @name Internal data
00587      */
00588     //@{
00589     channel c1;
00590     //@}
00591     
00592 
00593   };
00594 
00595 
00596   /**
00597    * Alias for graph representation with plain mean value distances.
00598    */
00599   typedef regionGraphColor<regionGraphColorMeanDistance> regionGraphColorMeans;
00600 
00601   /**
00602    * Alias for graph representation with weighted mean value distances.
00603    */
00604   typedef regionGraphColor<regionGraphColorHarisDistance> 
00605           regionGraphColorHaris;
00606 
00607   /**
00608    * Alias for graph representation with plain mean value distances.
00609    */
00610   typedef regionGraphGray<regionGraphScalarMeanDistance> regionGraphGrayMeans;
00611 
00612   /**
00613    * Alias for graph representation with weighted mean value distances.
00614    */
00615   typedef regionGraphGray<regionGraphScalarHarisDistance> 
00616           regionGraphGrayHaris;
00617 }
00618 
00619 #endif
00620 #endif

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