core/vil/algo/vil_region_finder.h
Go to the documentation of this file.
00001 #ifndef vil_region_finder_h_
00002 #define vil_region_finder_h_
00003 //:
00004 // \file
00005 // \verbatim
00006 //  Modifications
00007 //   March.2005 - Gehua Yang - template on predicate to make it generic
00008 // \endverbatim
00009 
00010 #include <vcl_vector.h>
00011 #include <vcl_functional.h>
00012 #include <vil/vil_image_view.h>
00013 
00014 //: Type of connectivity to use in finding the regions
00015 //
00016 // \relatesalso vil_region_finder
00017 //
00018 enum vil_region_finder_connectivity
00019 {
00020   vil_region_finder_4_conn,
00021   vil_region_finder_8_conn
00022 };
00023 
00024 //: Extract regions from an image using a flood-fill.
00025 //
00026 // This class encapsulates a simple flood fill algorithm to extract a
00027 // four or eight connected regions from an image. It uses an auxiliary
00028 // bool image to mark pixels as processed. This mark is not reset
00029 // between calls to the region extraction routine, so each region can
00030 // be extracted only once.
00031 //
00032 template <class pix_type, class predicate_type = vcl_equal_to<pix_type> >
00033 class vil_region_finder
00034 {
00035  public:
00036   //:
00037   typedef vil_image_view<pix_type> image_view;
00038 
00039   //: Prepare to extract regions from \a image
00040   vil_region_finder( image_view const& image,
00041                      vil_region_finder_connectivity conn = vil_region_finder_4_conn );
00042 
00043   //: Extract the region containing (i,j)
00044   //
00045   // This will return the coordinates of all the pixels in the region
00046   // around (i,j) where the predicate claims true compared with the intensity of
00047   // pixel (i,j).
00048   //
00049   // This is a simple flood fill algorithm.
00050   //
00051   void
00052   same_int_region( unsigned i, unsigned j,
00053                    vcl_vector<unsigned>& ri,
00054                    vcl_vector<unsigned>& rj );
00055 
00056 
00057   //: Extract the region containing (i,j)
00058   //
00059   // This will return the coordinates of all the pixels in the region
00060   // around (i,j) where the predicate claims true compared with the intensity p
00061   //
00062   // This is a simple flood fill algorithm.
00063   //
00064   void
00065   same_int_region( unsigned i, unsigned j, pix_type p,
00066                    vcl_vector<unsigned>& ri,
00067                    vcl_vector<unsigned>& rj );
00068 
00069   //: The image from which the regions are being extracted
00070   image_view const&
00071   image() const;
00072 
00073   //: boolean mask on the region
00074   vil_image_view<bool> const&  boolean_region_image() const;
00075 
00076  private:
00077   //:
00078   // Marks all pixels as unprocessed, and sets the neighbour deltas
00079   // based on the requested connectivity.
00080   //
00081   void
00082   init( vil_region_finder_connectivity );
00083 
00084   //: The image
00085   image_view const image_;
00086 
00087   //: The marks
00088   vil_image_view<bool> processed_;
00089 
00090   //: The size of the nbr_delta_ array
00091   unsigned num_nbrs_;
00092 
00093   //: The deltas to the neighbours.
00094   int const (*nbr_delta_)[2];
00095 
00096   //: predicate
00097   predicate_type predi_;
00098 };
00099 
00100 // do the implicit template thing.
00101 #include "vil_region_finder.txx"
00102 
00103 #endif // vil_region_finder_h_