vil_greyscale_erode.hxx
Go to the documentation of this file.
1 #ifndef vil_greyscale_erode_hxx_
2 #define vil_greyscale_erode_hxx_
3 //:
4 // \file
5 // \brief Perform greyscale erosion on images
6 // \author Tim Cootes
7 
8 #include "vil_greyscale_erode.h"
9 #include <cassert>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 
14 //: Erodes src_image to produce dest_image (assumed single plane).
15 // dest_image(i0,j0) is the maximum value of the pixels under the
16 // structuring element when it is centred on src_image(i0,j0)
17 template <class T>
18 void vil_greyscale_erode(const vil_image_view<T>& src_image,
19  vil_image_view<T>& dest_image,
20  const vil_structuring_element& element)
21 {
22  assert(src_image.nplanes()==1);
23  unsigned ni = src_image.ni();
24  unsigned nj = src_image.nj();
25  dest_image.set_size(ni,nj,1);
26 
27  std::ptrdiff_t s_istep = src_image.istep(), s_jstep = src_image.jstep(),
28  d_istep = dest_image.istep(), d_jstep = dest_image.jstep();
29 
30  const T* src_row0 = src_image.top_left_ptr();
31  T* dest_row0 = dest_image.top_left_ptr();
32 
33  std::vector<std::ptrdiff_t> offset;
34  vil_compute_offsets(offset,element,s_istep,s_jstep);
35 
36  // Define box in which all element will be valid
37  int ilo = -element.min_i();
38  int ihi = ni-1-element.max_i();
39  int jlo = -element.min_j();
40  int jhi = nj-1-element.max_j();
41 
42  // Deal with left edge
43  for (int i=0;i<ilo;++i)
44  for (unsigned int j=0;j<nj;++j)
45  dest_image(i,j,0)=vil_greyscale_erode(src_image,0,element,i,j);
46  // Deal with right edge
47  for (unsigned int i=ihi+1;i<ni;++i)
48  for (unsigned int j=0;j<nj;++j)
49  dest_image(i,j,0)=vil_greyscale_erode(src_image,0,element,i,j);
50  // Deal with bottom edge
51  for (int i=ilo;i<=ihi;++i)
52  for (int j=0;j<jlo;++j)
53  dest_image(i,j,0)=vil_greyscale_erode(src_image,0,element,i,j);
54  // Deal with top edge
55  for (int i=ilo;i<=ihi;++i)
56  for (unsigned int j=jhi+1;j<nj;++j)
57  dest_image(i,j,0)=vil_greyscale_erode(src_image,0,element,i,j);
58 
59  for (int j=jlo;j<=jhi;++j)
60  {
61  const T* src_p = src_row0 + j*s_jstep + ilo*s_istep;
62  T* dest_p = dest_row0 + j*d_jstep + ilo * d_istep;
63 
64  for (int i=ilo;i<=ihi;++i,src_p+=s_istep,dest_p+=d_istep)
65  *dest_p=vil_greyscale_erode(src_p,&offset[0],offset.size());
66  }
67 }
68 
69 #undef VIL_GREYSCALE_ERODE_INSTANTIATE
70 #define VIL_GREYSCALE_ERODE_INSTANTIATE(T) \
71 template void vil_greyscale_erode(const vil_image_view< T >& src_image, \
72  vil_image_view< T >& dest_image, \
73  const vil_structuring_element& element)
74 
75 #endif // vil_greyscale_erode_hxx_
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.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
int max_j() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
unsigned nj() const
Height.
T vil_greyscale_erode(const vil_image_view< T > &image, unsigned plane, const vil_structuring_element &element, int i0, int j0)
Return max of pixels under structuring element centred at (i0,j0).
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()].
Perform greyscale erosion on images.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
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.