gml_Connexer.h

00001 // gml_Connexer.h --
00002 //
00003 //    Define the gml_TConnexer class.
00004 //
00005 //  Copyright (c) 1997-2004 CLIPS-IMAG
00006 //
00007 //  See the file "gml_LicenseTerms.txt" for information on usage and redistribution
00008 //  of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00009 //
00010 //  Created in July 1996 (FB).
00011 //  Revamped in February 2004 (JL).
00012 //
00013 
00014 /*   @todo
00015  *      replace the loop in Representant() by a lookup in a maintained 
00016  *      representant table
00017  */
00018 
00019 #ifndef __GML_CONNEXER__
00020 #define __GML_CONNEXER__
00021 
00022 // project includes
00023 #include "gml/base/gml_Array.h"
00024 #include "gml/base/gml_Types.h"
00025 #include "gml/image/gml_Geometry.h"
00026 #include "gml/math/gml_Gaussian2.h"
00027 #include "gml/base/gml_Errors.h"
00028 #include "gml/image/gml_Bitmap.h"
00029 #include "gml/image/gml_RGBColor.h"
00030 
00031 
00032 /*  
00033  *  gml_TConnexClass --
00034  * 
00035  *  Description of a connected component of a bitmap.
00036  */
00037 
00038 class gml_TConnexClass
00039 {
00040 public:
00041   UInt16                  fIndex;    ///< index used in the class map
00042   UInt32                  fArea;     ///< number of pixels in the class
00043   gml_TRect               fBounding; ///< bounding box of the class
00044   gml_TPointFloat         fCenter;   ///< coordinates of the barycenter
00045   gml_TGaussian2_Float32  fGaussian; ///< the spatial distribution of the class
00046   gml_TRGBColor           fColor;    ///< color used in the colormap for this class
00047 };
00048 
00049 
00050 
00051 /*  gml_TConnexClassInt --
00052  * 
00053  *  Internal description of a connected component, used during processing.
00054  */
00055 class gml_TConnexClassInt
00056 {
00057 public:
00058   UInt16 mIn;    ///< if mIn != index, the box is included in the box indexed  by mIn
00059   UInt16 __padding;
00060   UInt32 mArea;  ///< number of pixels in this class
00061 };
00062 
00063 
00064 ////////////////////////////////////////////////////////////////////////////////
00065 //                                                                            //
00066 //                         gml_TConnexer class declaration                    //
00067 //                                                                            //
00068 ////////////////////////////////////////////////////////////////////////////////
00069 
00070 /* 
00071  *  gml_TConnexer --
00072  *
00073  *    A class for finding the connected components in a bitmap,
00074  *    with an arbitrary connectivity radius, and either filter out some
00075  *    components, determine some of their properties, or both.
00076  * 
00077  *    Based on F. Kirouches algorithm and N. Oliver extensions.
00078  */
00079 class gml_TConnexer
00080 {
00081 public:
00082 
00083 
00084   /// Init --
00085 
00086   gml_TError Init ();
00087   
00088 
00089   /// Dispose --
00090 
00091   void Dispose ();
00092 
00093 
00094   /// Compute --
00095   ///
00096   /// Find connected components in the <sourceRect> region of interest of <source>.
00097 
00098   gml_TError Compute (
00099     gml_TBitmap*        source,       ///< [in/out] source bitmap
00100     gml_TRect*          sourceRect,   ///< [in]     region of intereset
00101     UInt16            & nbClasses,    ///< [in/out] maximum number of components to extract
00102     gml_TConnexClass*   classes,      ///< [in/out] output class representation (caller allocated),
00103                                       ///<          array of size <nbClasses>
00104     UInt8               radius,       ///< [in]     connectivity radius
00105     UInt32              minArea,      ///< [in]     minimum area of the components of interest, or 0
00106     UInt32              flags         ///< [in]     a combination of sFlagBounding, sFlagCenter,
00107                                       ///<          sFlagGaussian, and sFlagFilter.
00108   );
00109 
00110   static const UInt32 sFlagBounding = (1 << 0); ///< compute bouding and extremas
00111   static const UInt32 sFlagCenter   = (1 << 1); ///< compute the class centers
00112   static const UInt32 sFlagGaussian = (1 << 2); ///< compute gaussian distribution
00113   static const UInt32 sFlagFilter   = (1 << 3); ///< change source bitmap to match found classes
00114 
00115   /// maximum number of classes
00116   static const UInt16 sMaxNbClass = GML_CCOLORARRAYNBELEM;
00117 
00118 protected:
00119 
00120   UInt16 fMaxNbClasses;           ///< max number of connected components 
00121                                   ///< in the last computed bitmap
00122   UInt16 fNbClasses;              ///< number of found connected components 
00123                                   ///< in the last computed bitmap
00124   gml_TBitmap *fClassMap;         ///< support map as G_16 bitmap
00125   gml_TConnexClassInt *fClasses;  ///< array of classes, used in Compute ()
00126   gml_TArray<gml_TConnexClass> fResults;     ///< array of resulting classes
00127   
00128   // setup methods (allocate and zero the above tables)
00129   gml_TError AllocateClassMap (gml_TBitmap const * const source);
00130   gml_TError AllocateAndZeroClasses  (UInt16 const maxNbClasses);
00131 
00132   /// returns the representant of a class (for class grouping)
00133   UInt16 Representant (UInt16 iIndex);
00134 
00135 public:
00136   /// process one neighbor pixel
00137   inline void ProcessNeighborPixel (UInt16 neighbor, UInt16& current);
00138 
00139   /// add and setup a new class
00140   inline void CreateNewClass (UInt16& current);
00141 };
00142 
00143 
00144 
00145 ////////////////////////////////////////////////////////////////////////////////
00146 //                                                                            //
00147 //                         Inline method definitions                          //
00148 //                                                                            //
00149 ////////////////////////////////////////////////////////////////////////////////
00150 
00151 
00152 //
00153 // Representant --
00154 //
00155 
00156 inline UInt16 gml_TConnexer::Representant (UInt16 index)
00157 {
00158   UInt16 parent = fClasses[index].mIn;
00159   return (parent == 0) ? index : parent;
00160 //  return Select (parent, 1, index, parent);
00161 }
00162 
00163 
00164 //
00165 // CreateNewClass --
00166 //
00167 
00168 inline void gml_TConnexer::CreateNewClass (UInt16& current)
00169 {
00170   fNbClasses++;
00171   fClasses[fNbClasses].mIn = 0;
00172   fClasses[fNbClasses].mArea = 1;
00173   current = fNbClasses;
00174 }
00175 
00176 
00177 //
00178 //    ProcessNeighborPixel
00179 //
00180 
00181 inline void gml_TConnexer::ProcessNeighborPixel (UInt16 neighbor, UInt16& current)
00182 {
00183   // skip empty neighbors
00184   if (neighbor == 0) return;
00185   
00186   // there is a neighbor pixel, set its class to its representant
00187   neighbor = Representant (neighbor);
00188 
00189   if (current == 0) {
00190     // the current pixel has no class allocated (first neighbor pixel)
00191 
00192     // set the class for the current pixel to be 
00193     // the representant of the neighbor class
00194     current = neighbor;
00195 
00196     // increment area of representant
00197     (fClasses[neighbor].mArea)++;
00198   
00199   } else if (current != neighbor) {
00200     // set the representant of the neighbor class
00201     // to be the current pixel class
00202     fClasses[neighbor].mIn = current;
00203     
00204     // update all classes contained in <neighbor> with <current>
00205     for (int k = 0; k < fNbClasses; ++k) {
00206       gml_TConnexClassInt& c = fClasses[k];
00207       if (c.mIn == neighbor) c.mIn = current;
00208     }
00209 
00210     // update the area of global representant
00211     fClasses[current].mArea += fClasses[neighbor].mArea;
00212 
00213     // update the class of neighbor pixel to be the
00214     // representant
00215     // XXX this seems to be unnecessary ?
00216     /* neighbor = current; */
00217   }
00218 }
00219 
00220 #endif /* __GML_CONNEXER__ */
00221 
Generated on Tue Jun 12 14:03:27 2007 for gml by Doxygen 1.5.2.