vil_median.h
Go to the documentation of this file.
1 #ifndef vil_median_h_
2 #define vil_median_h_
3 //:
4 // \file
5 // \brief Perform median filtering on images
6 // \author Tim Cootes
7 
8 #include <algorithm>
10 #include <vil/vil_image_view.h>
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 
15 //: Return r-th sorted value of im[offset[k]]
16 // Values im[offset[k]] placed into values[k] then sorted. \a values
17 // should be a random access iterator into a container of T such
18 // that the range [values,values+n) is valid.
19 template <class T, class Iter>
20 inline T vil_sorted_value(const T* im, const std::ptrdiff_t* offset, Iter values,
21  unsigned n, unsigned r)
22 {
23  Iter v = values;
24  for (unsigned i=0;i<n;++i,++v) *v=im[offset[i]];
25  std::nth_element(values, values+r, values+n);
26  return values[r];
27 }
28 
29 //: Return (n*r)-th sorted value of pixels under element centred at (i0,j0)
30 // \param r in [0,1].
31 // \param values used to store values sampled from image before sorting
32 // Checks boundary overlap
33 // \relatesalso vil_image_view
34 // \relatesalso vil_structuring_element
35 template <class T>
36 inline T vil_sorted_value(const vil_image_view<T>& image, unsigned plane,
37  const vil_structuring_element& element, int i0, int j0,
38  std::vector<T>& values, double r)
39 {
40  values.clear();
41  std::size_t n = element.p_i().size();
42  for (std::size_t k=0;k<n;++k)
43  {
44  unsigned int i = i0+element.p_i()[k];
45  unsigned int j = j0+element.p_j()[k];
46  if (i<image.ni() && j<image.nj())
47  values.push_back(image(i,j,plane));
48  }
49  std::nth_element(values.begin(),values.begin()+std::size_t(r*(values.size()-1)),
50  values.end());
51  return values[std::size_t(r*(values.size()-1))];
52 }
53 
54 //: Computes median value of pixels under structuring element.
55 // dest_image(i0,j0) is the median value of the pixels under the
56 // structuring element when it is centred on src_image(i0,j0)
57 // \relatesalso vil_image_view
58 // \relatesalso vil_structuring_element
59 template <class T>
60 void vil_median(const vil_image_view<T>& src_image,
61  vil_image_view<T>& dest_image,
62  const vil_structuring_element& element);
63 
64 #endif // vil_median_h_
Structuring element for morphology represented as a list of non-zero pixels.
const std::vector< int > & p_j() const
j position of elements (i,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
void vil_median(const vil_image_view< T > &src_image, vil_image_view< T > &dest_image, const vil_structuring_element &element)
Computes median value of pixels under structuring element.
unsigned ni() const
Width.
unsigned nj() const
Height.
#define v
T vil_sorted_value(const vil_image_view< T > &image, unsigned plane, const vil_structuring_element &element, int i0, int j0, std::vector< T > &values, double r)
Return (n*r)-th sorted value of pixels under element centred at (i0,j0).
Definition: vil_median.h:36
A base class reference-counting view of some image data.
const std::vector< int > & p_i() const
i position of elements (i,j).