core/vil/algo/vil_region_finder.txx
Go to the documentation of this file.
00001 #ifndef vil_region_finder_txx_
00002 #define vil_region_finder_txx_
00003 
00004 #include "vil_region_finder.h"
00005 #include <vcl_cassert.h>
00006 
00007 // ---------------------------------------------------------------------------
00008 //                                                                 constructor
00009 
00010 template <class pix_type, class predicate_type>
00011 vil_region_finder<pix_type, predicate_type>::
00012 vil_region_finder( image_view const& in_image,
00013                    vil_region_finder_connectivity conn )
00014   : image_( in_image ),
00015     processed_( image_.ni(), image_.nj(), image_.nplanes() )
00016 {
00017   init( conn );
00018 }
00019 
00020 
00021 // ---------------------------------------------------------------------------
00022 //                                                                        init
00023 
00024 template <class pix_type, class predicate_type>
00025 void
00026 vil_region_finder<pix_type, predicate_type>::
00027 init( vil_region_finder_connectivity conn )
00028 {
00029   static int const nbrs4_delta[4][2] = { { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1} };
00030   static int const nbrs8_delta[8][2] = { { 1, 0}, { 1,-1}, { 0,-1}, {-1,-1},
00031                                          {-1, 0}, {-1, 1}, { 0, 1}, { 1, 1} };
00032   processed_.fill( false );
00033   switch ( conn ) {
00034    case vil_region_finder_4_conn:
00035     num_nbrs_ = 4;
00036     nbr_delta_ = nbrs4_delta;
00037     break;
00038    case vil_region_finder_8_conn:
00039     num_nbrs_ = 8;
00040     nbr_delta_ = nbrs8_delta;
00041     break;
00042    default:
00043     assert(!"Invalid connectivity type");
00044   }
00045 }
00046 
00047 
00048 // ---------------------------------------------------------------------------
00049 //                                                             same int region
00050 
00051 template <class pix_type, class predicate_type>
00052 void
00053 vil_region_finder<pix_type, predicate_type>::
00054 same_int_region( unsigned i, unsigned j,
00055                  vcl_vector<unsigned>& ri,
00056                  vcl_vector<unsigned>& rj )
00057 {
00058   // get the pixel intensity
00059   pix_type p = image_(i,j);
00060 
00061   // call the real function
00062   same_int_region( i, j, p, ri, rj );
00063 }
00064 
00065 
00066 // ---------------------------------------------------------------------------
00067 //                                                             same int region
00068 
00069 template <class pix_type, class predicate_type>
00070 void
00071 vil_region_finder<pix_type, predicate_type>::
00072 same_int_region( unsigned i, unsigned j, pix_type p,
00073                  vcl_vector<unsigned>& ri,
00074                  vcl_vector<unsigned>& rj )
00075 {
00076   // early stop if this pixel has already been processed
00077   if ( processed_(i,j) )
00078     return;
00079 
00080   // use ri, rj as storage space
00081   ri.resize( 0 );
00082   rj.resize( 0 );
00083 
00084   // mark the initial position
00085   processed_(i,j) = true;
00086   ri.push_back( i );
00087   rj.push_back( j );
00088 
00089   for ( unsigned cur_pos = 0; cur_pos<ri.size(); ++cur_pos )
00090   {
00091     // get pixel coordinate
00092     i = ri[cur_pos];
00093     j = rj[cur_pos];
00094 
00095     // examine the neighbors
00096     for ( unsigned c = 0; c < num_nbrs_; ++c )
00097     {
00098       unsigned nbr_i = (unsigned)( (signed)i + nbr_delta_[c][0] );
00099       unsigned nbr_j = (unsigned)( (signed)j + nbr_delta_[c][1] );
00100 
00101       if (nbr_i < image_.ni() &&
00102           nbr_j < image_.nj() &&
00103           !processed_(nbr_i, nbr_j) &&
00104           predi_( image_( nbr_i, nbr_j ),  p ) )
00105       {
00106         // add this pixel to current region
00107         processed_(nbr_i, nbr_j) = true;
00108         ri.push_back( nbr_i );
00109         rj.push_back( nbr_j );
00110       }
00111     }
00112   }
00113 }
00114 
00115 
00116 // ---------------------------------------------------------------------------
00117 //                                                                       image
00118 
00119 template <class pix_type, class predicate_type>
00120 typename vil_region_finder<pix_type, predicate_type>::image_view const&
00121 vil_region_finder<pix_type, predicate_type>::
00122 image() const
00123 {
00124   return image_;
00125 }
00126 
00127 template <class pix_type, class predicate_type>
00128 vil_image_view<bool> const&
00129 vil_region_finder<pix_type, predicate_type>::
00130 boolean_region_image() const
00131 {
00132   return processed_;
00133 }
00134 
00135 #endif // vil_region_finder_txx_