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   UInt32 mArea;  ///< number of pixels in this class
00060 };
00061 
00062 
00063 ////////////////////////////////////////////////////////////////////////////////
00064 //                                                                            //
00065 //                         gml_TConnexer class declaration                    //
00066 //                                                                            //
00067 ////////////////////////////////////////////////////////////////////////////////
00068 
00069 /* 
00070  *  gml_TConnexer --
00071  *
00072  *    A class for finding the connected components in a bitmap,
00073  *    with an arbitrary connectivity radius, and either filter out some
00074  *    components, determine some of their properties, or both.
00075  * 
00076  *    Based on F. Kirouches algorithm and N. Oliver extensions.
00077  */
00078 class gml_TConnexer
00079 {
00080 public:
00081 
00082 
00083   /// Init --
00084 
00085   gml_TError Init ();
00086   
00087 
00088   /// Dispose --
00089 
00090   void Dispose ();
00091 
00092 
00093   /// Compute --
00094   ///
00095   /// Find connected components in the <sourceRect> region of interest of <source>.
00096 
00097   gml_TError Compute (
00098     gml_TBitmap*        source,       ///< [in/out] source bitmap
00099     gml_TRect*          sourceRect,   ///< [in]     region of intereset
00100     UInt16            & nbClasses,    ///< [in/out] maximum number of components to extract
00101     gml_TConnexClass*   classes,      ///< [in/out] output class representation (caller allocated),
00102                                       ///<          array of size <nbClasses>
00103     UInt8               radius,       ///< [in]     connectivity radius
00104     UInt32              minArea,      ///< [in]     minimum area of the components of interest, or 0
00105     UInt32              flags         ///< [in]     a combination of sFlagBounding, sFlagCenter,
00106                                       ///<          sFlagGaussian, and sFlagFilter.
00107   );
00108 
00109   static const UInt32 sFlagBounding = (1 << 0); ///< compute bouding and extremas
00110   static const UInt32 sFlagCenter   = (1 << 1); ///< compute the class centers
00111   static const UInt32 sFlagGaussian = (1 << 2); ///< compute gaussian distribution
00112   static const UInt32 sFlagFilter   = (1 << 3); ///< change source bitmap to match found classes
00113 
00114   /// maximum number of classes
00115   static const UInt16 sMaxNbClass = GML_CCOLORARRAYNBELEM;
00116 
00117 protected:
00118 
00119   UInt16 fMaxNbClasses;           ///< max number of connected components 
00120                                   ///< in the last computed bitmap
00121   UInt16 fNbClasses;              ///< number of found connected components 
00122                                   ///< in the last computed bitmap
00123   gml_TBitmap *fClassMap;         ///< support map as G_16 bitmap
00124   gml_TConnexClassInt *fClasses;  ///< array of classes, used in Compute ()
00125   gml_TArray<gml_TConnexClass> fResults;     ///< array of resulting classes
00126   
00127   // setup methods (allocate and zero the above tables)
00128   gml_TError AllocateClassMap (gml_TBitmap const * const source);
00129   gml_TError AllocateAndZeroClasses  (UInt16 const maxNbClasses);
00130 
00131   /// returns the representant of a class (for class grouping)
00132   UInt16 Representant (UInt16 iIndex);
00133 
00134 public:
00135   /// process one neighbor pixel
00136   void ProcessNeighborPixel (UInt16 *neighbor, UInt16 *current);
00137 
00138   /// add and setup a new class
00139   void CreateNewClass (UInt16 *current);
00140 };
00141 
00142 
00143 
00144 ////////////////////////////////////////////////////////////////////////////////
00145 //                                                                            //
00146 //                         Inline method definitions                          //
00147 //                                                                            //
00148 ////////////////////////////////////////////////////////////////////////////////
00149 
00150 
00151 //
00152 // Representant --
00153 //
00154 
00155 inline UInt16 gml_TConnexer::Representant (UInt16 index)
00156 {
00157   UInt16 newIndex;
00158   assert (index <= fNbClasses);
00159 
00160   while (true) {
00161     newIndex = fClasses[index].mIn;
00162     assert (newIndex <= fNbClasses);
00163     if (newIndex == 0) break;
00164     index = newIndex;
00165   }
00166 
00167   return index;
00168 }
00169 
00170 
00171 //
00172 // CreateNewClass --
00173 //
00174 
00175 inline void gml_TConnexer::CreateNewClass (UInt16 *current)
00176 {
00177   fNbClasses++;
00178   fClasses[fNbClasses].mIn = 0;
00179   fClasses[fNbClasses].mArea = 1;
00180   *current = fNbClasses;
00181 }
00182 
00183 
00184 //
00185 //    ProcessNeighborPixel
00186 //
00187 
00188 inline void gml_TConnexer::ProcessNeighborPixel (UInt16 *neighbor, UInt16 *current)
00189 {
00190   assert (*neighbor <= fNbClasses);
00191   assert (*current <= fNbClasses);
00192   
00193   // skip empty neighbors
00194   if (*neighbor == 0) return;
00195   
00196   // there is a neighbor pixel, set its class to its representant
00197   *neighbor = Representant (*neighbor);
00198 
00199   if (*current == 0) {
00200     // the current pixel has no class allocated (first neighbor pixel)
00201 
00202     // set the class for the current pixel to be 
00203     // the representant of the neighbor class
00204     *current = *neighbor;
00205 
00206     // increment area of representant
00207     fClasses[*neighbor].mArea += 1;
00208   
00209   } else if (*current != *neighbor) {
00210     // set the representant of the neighbor class
00211     // to be the current pixel class
00212     fClasses[*neighbor].mIn = *current;
00213 
00214     // update the area of global representant
00215     fClasses[*current].mArea += fClasses[*neighbor].mArea;
00216 
00217     // update the class of neighbor pixel to be the
00218     // representant
00219     *neighbor = *current;
00220   }
00221 
00222   assert (*neighbor <= fNbClasses);
00223   assert (*current <= fNbClasses);
00224 }
00225 
00226 #endif /* __GML_CONNEXER__ */
00227 
Generated on Tue Jun 12 14:03:27 2007 for gml by Doxygen 1.5.2.