vil_region_finder.hxx
Go to the documentation of this file.
1 #ifndef vil_region_finder_hxx_
2 #define vil_region_finder_hxx_
3 
4 #include "vil_region_finder.h"
5 #include <cassert>
6 #ifdef _MSC_VER
7 # include <vcl_msvc_warnings.h>
8 #endif
9 
10 // ---------------------------------------------------------------------------
11 // constructor
12 
13 template <class pix_type, class predicate_type>
15 vil_region_finder( image_view const& in_image,
17  : image_( in_image ),
18  processed_( image_.ni(), image_.nj(), image_.nplanes() )
19 {
20  init( conn );
21 }
22 
23 
24 // ---------------------------------------------------------------------------
25 // init
26 
27 template <class pix_type, class predicate_type>
28 void
31 {
32  static int const nbrs4_delta[4][2] = { { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1} };
33  static int const nbrs8_delta[8][2] = { { 1, 0}, { 1,-1}, { 0,-1}, {-1,-1},
34  {-1, 0}, {-1, 1}, { 0, 1}, { 1, 1} };
35  processed_.fill( false );
36  switch ( conn ) {
38  num_nbrs_ = 4;
39  nbr_delta_ = nbrs4_delta;
40  break;
42  num_nbrs_ = 8;
43  nbr_delta_ = nbrs8_delta;
44  break;
45  default:
46  assert(!"Invalid connectivity type");
47  }
48 }
49 
50 
51 // ---------------------------------------------------------------------------
52 // same int region
53 
54 template <class pix_type, class predicate_type>
55 void
57 same_int_region( unsigned i, unsigned j,
58  std::vector<unsigned>& ri,
59  std::vector<unsigned>& rj )
60 {
61  // get the pixel intensity
62  pix_type p = image_(i,j);
63 
64  // call the real function
65  same_int_region( i, j, p, ri, rj );
66 }
67 
68 
69 // ---------------------------------------------------------------------------
70 // same int region
71 
72 template <class pix_type, class predicate_type>
73 void
75 same_int_region( unsigned i, unsigned j, pix_type p,
76  std::vector<unsigned>& ri,
77  std::vector<unsigned>& rj )
78 {
79  // early stop if this pixel has already been processed
80  if ( processed_(i,j) )
81  return;
82 
83  // use ri, rj as storage space
84  ri.resize( 0 );
85  rj.resize( 0 );
86 
87  // mark the initial position
88  processed_(i,j) = true;
89  ri.push_back( i );
90  rj.push_back( j );
91 
92  for ( unsigned cur_pos = 0; cur_pos<ri.size(); ++cur_pos )
93  {
94  // get pixel coordinate
95  i = ri[cur_pos];
96  j = rj[cur_pos];
97 
98  // examine the neighbors
99  for ( unsigned c = 0; c < num_nbrs_; ++c )
100  {
101  unsigned nbr_i = (unsigned)( (signed)i + nbr_delta_[c][0] );
102  unsigned nbr_j = (unsigned)( (signed)j + nbr_delta_[c][1] );
103 
104  if (nbr_i < image_.ni() &&
105  nbr_j < image_.nj() &&
106  !processed_(nbr_i, nbr_j) &&
107  predi_( image_( nbr_i, nbr_j ), p ) )
108  {
109  // add this pixel to current region
110  processed_(nbr_i, nbr_j) = true;
111  ri.push_back( nbr_i );
112  rj.push_back( nbr_j );
113  }
114  }
115  }
116 }
117 
118 
119 // ---------------------------------------------------------------------------
120 // image
121 
122 template <class pix_type, class predicate_type>
125 image() const
126 {
127  return image_;
128 }
129 
130 template <class pix_type, class predicate_type>
134 {
135  return processed_;
136 }
137 
138 #endif // vil_region_finder_hxx_
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
image_view const & image() const
The image from which the regions are being extracted.
vil_image_view< bool > const & boolean_region_image() const
boolean mask on the region.
void same_int_region(unsigned i, unsigned j, std::vector< unsigned > &ri, std::vector< unsigned > &rj)
Extract the region containing (i,j).
vil_region_finder_connectivity
Type of connectivity to use in finding the regions.
void init(vil_region_finder_connectivity)
Marks all pixels as unprocessed, and sets the neighbour deltas based on the requested connectivity.
vil_region_finder(image_view const &image, vil_region_finder_connectivity conn=vil_region_finder_4_conn)
Prepare to extract regions from image.