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