vil_suppress_non_max.h
Go to the documentation of this file.
1 // This is core/vil/algo/vil_suppress_non_max.h
2 #ifndef vil_suppress_non_max_h_
3 #define vil_suppress_non_max_h_
4 //:
5 // \file
6 // \brief Suppress all non-maximal points in image
7 // \author Tim Cootes
8 
9 #include <vil/vil_image_view.h>
11 #include <vil/vil_fill.h>
12 #include <cassert>
13 #ifdef _MSC_VER
14 # include <vcl_msvc_warnings.h>
15 #endif
16 
17 //: Suppress all non-maximal (non peaks) pixels in the image
18 // If image(i,j) is strictly larger than all neighbouring pixels,
19 // and is above the threshold, then it is retained. All other
20 // pixels are set to non_max_value.
21 //
22 // non_max_value must be below the threshold (so the default value of
23 // zero is inappropriate if the image contains peaks of interest with
24 // negative values)
25 //
26 // Note that where there are neighbouring pixels with identical values
27 // on a raised plateau, then all the pixels on the plateau will be
28 // suppressed. This can cause some peaks to be missed. The effect
29 // can be reduced by using float images and pre-smoothing slightly.
30 // Alternatively, use vil_suppress_non_plateau_3x3() to retain plateau
31 // points as well as strict maxima.
32 //
33 // \sa vil_suppress_non_plateau_3x3()
34 // \relatesalso vil_image_view
35 template <class T>
36 inline void vil_suppress_non_max_3x3(const vil_image_view<T>& src_im,
37  vil_image_view<T>& dest_im,
38  T threshold=0, T non_max_value=0)
39 {
40  unsigned ni=src_im.ni(),nj=src_im.nj();
41  assert(src_im.nplanes()==1);
42 
43  dest_im.set_size(ni,nj,1);
44 
45  std::ptrdiff_t istep = src_im.istep(),jstep=src_im.jstep();
46  std::ptrdiff_t distep = dest_im.istep(),djstep=dest_im.jstep();
47  const T* row = src_im.top_left_ptr()+istep+jstep;
48  T* drow = dest_im.top_left_ptr()+distep+djstep;
49  for (unsigned j=1;j<nj-1;++j,row+=jstep,drow+=djstep)
50  {
51  const T* pixel = row;
52  T* dpixel = drow;
53  for (unsigned i=1;i<ni-1;++i,pixel+=istep,dpixel+=distep)
54  {
55  if (*pixel<threshold || !vil_is_peak_3x3(pixel,istep,jstep))
56  *dpixel = non_max_value;
57  else
58  *dpixel = *pixel;
59  }
60  }
61 
62  // Border pixels assumed not to be local maxima
63  vil_fill_row(dest_im,0,non_max_value);
64  vil_fill_row(dest_im,nj-1,non_max_value);
65  vil_fill_col(dest_im,0,non_max_value);
66  vil_fill_col(dest_im,ni-1,non_max_value);
67 }
68 
69 #endif // vil_suppress_non_max_h_
Various functions for manipulating image views.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
Find peaks in image.
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
void vil_fill_col(vil_image_view< T > &view, unsigned i, T value)
Fill column i in view with given value.
Definition: vil_fill.h:148
unsigned ni() const
Width.
void vil_suppress_non_max_3x3(const vil_image_view< T > &src_im, vil_image_view< T > &dest_im, T threshold=0, T non_max_value=0)
Suppress all non-maximal (non peaks) pixels in the image.
unsigned nj() const
Height.
A base class reference-counting view of some image data.
bool vil_is_peak_3x3(const T *im, std::ptrdiff_t i_step, std::ptrdiff_t j_step)
True if pixel at *im is strictly above 8 neighbours.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.
void vil_fill_row(vil_image_view< T > &view, unsigned j, T value)
Fill row j in view with given value.
Definition: vil_fill.h:133