vil_binary_erode.cxx
Go to the documentation of this file.
1 //:
2 // \file
3 // \brief Perform binary erosion on images
4 // \author Tim Cootes
5 
6 #include "vil_binary_erode.h"
7 #include <cassert>
8 #ifdef _MSC_VER
9 # include <vcl_msvc_warnings.h>
10 #endif
11 
12 //: Erodes src_image to produce dest_image (assumed single plane)
13 void vil_binary_erode(const vil_image_view<bool>& src_image,
14  vil_image_view<bool>& dest_image,
15  const vil_structuring_element& element)
16 {
17  vil_binary_erode(src_image, dest_image, element,
18  vil_border_create_constant(src_image, true));
19 }
20 
21 //: Erodes src_image to produce dest_image (assumed single plane)
22 void vil_binary_erode(const vil_image_view<bool>& src_image,
23  vil_image_view<bool>& dest_image,
24  const vil_structuring_element& element,
25  const vil_border<vil_image_view<bool> >& border)
26 {
27  assert(src_image.nplanes()==1);
28  assert(src_image.is_contiguous());
29  unsigned ni = src_image.ni();
30  unsigned nj = src_image.nj();
31  dest_image.set_size(ni,nj,1);
32 
33  std::ptrdiff_t s_istep = src_image.istep(), s_jstep = src_image.jstep();
34  std::ptrdiff_t d_istep = dest_image.istep(), d_jstep = dest_image.jstep();
35 
36  const bool* src_row0 = src_image.top_left_ptr();
37  bool* dest_row0 = dest_image.top_left_ptr();
38 
39  std::vector<std::ptrdiff_t> offset;
40  vil_compute_offsets(offset,element,s_istep,s_jstep);
41 
42  // Define box in which all elements will be valid
43  int ilo = -element.min_i();
44  int ihi = ni-1-element.max_i();
45  int jlo = -element.min_j();
46  int jhi = nj-1-element.max_j();
47 
49  border_accessor = vil_border_create_accessor(src_image, border);
50 
51  // Handle border.
52  {
53  // Deal with left edge
54  for (int i=0;i<ilo;++i)
55  for (unsigned int j=0;j<nj;++j)
56  dest_image(i,j,0)=vil_binary_erode(border_accessor,0,element,i,j);
57  // Deal with right edge
58  for (unsigned int i=ihi+1;i<ni;++i)
59  for (unsigned int j=0;j<nj;++j)
60  dest_image(i,j,0)=vil_binary_erode(border_accessor,0,element,i,j);
61  // Deal with bottom edge
62  for (int i=ilo;i<=ihi;++i)
63  for (int j=0;j<jlo;++j)
64  dest_image(i,j,0)=vil_binary_erode(border_accessor,0,element,i,j);
65  // Deal with top edge
66  for (int i=ilo;i<=ihi;++i)
67  for (unsigned int j=jhi+1;j<nj;++j)
68  dest_image(i,j,0)=vil_binary_erode(border_accessor,0,element,i,j);
69  }
70 
71  for (int j=jlo;j<=jhi;++j)
72  {
73  const bool* src_p = src_row0 + j*s_jstep + ilo*s_istep;
74  bool* dest_p = dest_row0 + j*d_jstep + ilo * d_istep;
75 
76  for (int i=ilo;i<=ihi;++i,src_p+=s_istep,dest_p+=d_istep)
77  {
78  *dest_p=vil_binary_erode(src_p,&offset[0],offset.size());
79  }
80  }
81 }
int max_i() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
Structuring element for morphology represented as a list of non-zero pixels.
int max_j() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
bool is_contiguous() const
True if data all in one unbroken block and top_left_ptr() is lowest data address.
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
Border class. Makes pixel access outside image range transparent and configurable.
Definition: vil_border.h:37
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
Provides a pixel accessor which is syntax-compatible with vil_image_view.
Definition: vil_border.h:119
bool vil_binary_erode(const imAccessorT &image, unsigned plane, const vil_structuring_element &element, int i0, int j0)
Return false if any image pixel under element centred at (i0,j0) is zero.
void vil_compute_offsets(std::vector< std::ptrdiff_t > &offset, const vil_structuring_element &element, std::ptrdiff_t istep, std::ptrdiff_t jstep)
Generate a list of offsets for use on image with istep,jstep.
int min_j() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
Perform binary erosion on images.
vil_border_accessor< imT > vil_border_create_accessor(const imT &im, const vil_border< imT > &border)
Instantiates a border accessor, provided for convenience.
Definition: vil_border.h:144
unsigned nplanes() const
Number of planes.
vil_border< imT > vil_border_create_constant(const imT &, typename imT::pixel_type constant_val=0)
Instantiate a constant border whose type is derived from imT.
Definition: vil_border.h:152
int min_i() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.