RAWK
Gigapixel Raw Image Processing Toolkit
 All Classes Functions Variables Pages
image_median.hpp
1 // RAWK Copyright (C) 2014 Robert Kooima
2 //
3 // This program is free software: you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation, either version 3 of the License, or (at your option)
6 // any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITH-
9 // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 // more details.
12 
13 #ifndef IMAGE_MEDIAN_HPP
14 #define IMAGE_MEDIAN_HPP
15 
16 //------------------------------------------------------------------------------
17 
19 
20 class median : public image
21 {
22 public:
27 
28  median(int radius, int mode, image *L)
29  : image(L), radius(radius), mode(mode) { }
30 
31  virtual double get(int i, int j, int k) const
32  {
33  const int h = L->get_height();
34  const int w = L->get_width ();
35 
36  std::vector<double> v((2 * radius + 1) * (2 * radius + 1));
37 
38  int z = 0;
39 
40  for (int y = -radius; y <= radius; y++)
41  for (int x = -radius; x <= radius; x++)
42  if (x * x + y * y <= radius * radius)
43  v[z++] = L->get(wrap(i + y, h, mode & 1),
44  wrap(j + x, w, mode & 2), k);
45 
46  std::nth_element(v.begin(), v.begin() + z / 2, v.begin() + z);
47  return v[z / 2];
48  }
49 
50  virtual void tweak(int a, int v)
51  {
52  if (a == 0)
53  {
54  radius += v;
55  }
56  }
57 
58  virtual void doc(std::ostream& out) const
59  {
60  out << "median " << radius << " " << mode;
61  }
62 
63 protected:
64  int radius;
65  int mode;
66 };
67 
68 //------------------------------------------------------------------------------
69 
71 
72 class medianv : public median
73 {
74 public:
77 
78  medianv(int radius, int mode, image *L) : median(radius, mode, L) { }
79 
80  virtual double get(int i, int j, int k) const
81  {
82  const int h = L->get_height();
83 
84  std::vector<double> v(2 * radius + 1);
85 
86  int z = 0;
87 
88  for (int y = -radius; y <= radius; y++, z++)
89  v[z] = L->get(wrap(i + y, h, mode & 1), j, k);
90 
91  std::nth_element(v.begin(), v.begin() + z / 2, v.begin() + z);
92  return v[z / 2];
93  }
94 
95  virtual void doc(std::ostream& out) const
96  {
97  out << "medianv " << radius;
98  }
99 };
100 
101 //------------------------------------------------------------------------------
102 
104 
105 
106 class medianh : public median
107 {
108 public:
111 
112  medianh(int radius, int mode, image *L) : median(radius, mode, L) { }
113 
114  virtual double get(int i, int j, int k) const
115  {
116  const int w = L->get_width();
117 
118  std::vector<double> v(2 * radius + 1);
119 
120  int z = 0;
121 
122  for (int x = -radius; x <= radius; x++, z++)
123  v[z] = L->get(i, wrap(j + x, w, mode & 2), k);
124 
125  std::nth_element(v.begin(), v.begin() + z / 2, v.begin() + z);
126  return v[z / 2];
127  }
128 
129  virtual void doc(std::ostream& out) const
130  {
131  out << "medianh " << radius;
132  }
133 };
134 
135 //------------------------------------------------------------------------------
136 
137 #endif
virtual double get(int i, int j, int k) const =0
Return the value of the sample at row i, column j, channel k.
image * L
Left child.
Definition: image.hpp:117
medianv(int radius, int mode, image *L)
Find the median over a vertical line of neighboring pixels within radius, wrapped with the given wrap...
Definition: image_median.hpp:78
Median filter with a horizontal kernel.
Definition: image_median.hpp:106
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_median.hpp:58
Base class for all image sources, filters, and operators.
Definition: image.hpp:20
Median filter.
Definition: image_median.hpp:20
virtual void tweak(int a, int v)
Tweak image parameter a, changing the value by a factor of v.
Definition: image_median.hpp:50
virtual int get_height() const
Return the height of this image.
Definition: image.hpp:46
Median filter with a vertical kernel.
Definition: image_median.hpp:72
medianh(int radius, int mode, image *L)
Find the median over a horizontal line of neighboring pixels within radius, wrapped with the given wr...
Definition: image_median.hpp:112
virtual int get_width() const
Return the height of this image.
Definition: image.hpp:56
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_median.hpp:129
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_median.hpp:95
median(int radius, int mode, image *L)
Find the median over a square of neighboring pixels within radius, wrapped with the given wrapping mo...
Definition: image_median.hpp:28