RAWK
Gigapixel Raw Image Processing Toolkit
 All Classes Functions Variables Pages
image_convolve.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_CONVOLVE_HPP
14 #define IMAGE_CONVOLVE_HPP
15 
16 //------------------------------------------------------------------------------
17 
19 
20 class convolve : public image
21 {
22 public:
25 
26  convolve(int yradius, int xradius, int mode, image *L)
27  : image(L), yradius(yradius), xradius(xradius), mode(mode) { }
28 
29  virtual double get(int i, int j, int k) const
30  {
31  const int h = L->get_height();
32  const int w = L->get_width ();
33 
34  double s = 0;
35  double t = 0;
36  double T = 0;
37 
38  for (int y = -yradius; y <= yradius; y++)
39  for (int x = -xradius; x <= xradius; x++)
40  if ((s = kernel(y, x)))
41  {
42  T += s;
43  t += s * L->get(wrap(i + y, h, mode & 1),
44  wrap(j + x, w, mode & 2), k);
45  }
46 
47  return t / T;
48  }
49 
50 protected:
51  virtual double kernel(int, int) const = 0;
52 
53  int yradius;
54  int xradius;
55  int mode;
56 };
57 
58 //------------------------------------------------------------------------------
59 
61 
62 class gaussian : public convolve
63 {
64 public:
68 
69  gaussian(double sigma, int mode, image *L)
70  : convolve(int(ceil(sigma * 3)),
71  int(ceil(sigma * 3)), mode, L), sigma(sigma) { }
72 
73  virtual void tweak(int a, int v)
74  {
75  if (a == 0)
76  {
77  sigma += v;
78  xradius = int(ceil(sigma * 3));
79  yradius = int(ceil(sigma * 3));
80  }
81  }
82 
83  virtual double kernel(int y, int x) const
84  {
85  return sigma ? exp(-0.5 * (x * x + y * y) / (sigma * sigma)) : 1.0;
86  }
87 
88  virtual void doc(std::ostream& out) const
89  {
90  out << "gaussian " << sigma << " " << mode;
91  }
92 
93 private:
94  double sigma;
95 };
96 
97 //------------------------------------------------------------------------------
98 
100 
101 class gaussianv : public convolve
102 {
103 public:
107 
108  gaussianv(double sigma, int mode, image *L)
109  : convolve(int(ceil(sigma * 3)), 0, mode, L), sigma(sigma) { }
110 
111  virtual void tweak(int a, int v)
112  {
113  if (a == 0)
114  {
115  sigma += v;
116  yradius = int(ceil(sigma * 3));
117  }
118  }
119 
120  virtual double kernel(int y, int x) const
121  {
122  return sigma ? exp(-0.5 * (y * y) / (sigma * sigma)) : 1.0;
123  }
124 
125  virtual void doc(std::ostream& out) const
126  {
127  out << "gaussianv " << sigma << " " << mode;
128  }
129 
130 private:
131  double sigma;
132 };
133 
134 //------------------------------------------------------------------------------
135 
137 
138 class gaussianh : public convolve
139 {
140 public:
144 
145  gaussianh(double sigma, int mode, image *L)
146  : convolve(0, int(ceil(sigma * 3)), mode, L), sigma(sigma) { }
147 
148  virtual void tweak(int a, int v)
149  {
150  if (a == 0)
151  {
152  sigma += v;
153  xradius = int(ceil(sigma * 3));
154  }
155  }
156 
157  virtual double kernel(int y, int x) const
158  {
159  return sigma ? exp(-0.5 * (x * x) / (sigma * sigma)) : 1.0;
160  }
161 
162  virtual void doc(std::ostream& out) const
163  {
164  out << "gaussianh " << sigma << " " << mode;
165  }
166 
167 private:
168  double sigma;
169 };
170 
171 //------------------------------------------------------------------------------
172 
173 #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
gaussianv(double sigma, int mode, image *L)
Convolve image L using a vertical 1D Gaussian kernel with standard deviation sigma. Mode gives the wrapping mode. The true radius of the kernel is 3 sigma, rounded up.
Definition: image_convolve.hpp:108
virtual void tweak(int a, int v)
Tweak image parameter a, changing the value by a factor of v.
Definition: image_convolve.hpp:111
Base class for all image sources, filters, and operators.
Definition: image.hpp:20
Gaussian convolution filter.
Definition: image_convolve.hpp:62
gaussian(double sigma, int mode, image *L)
Convolve image L using a Gaussian kernel with standard deviation sigma. Mode gives the wrapping mode...
Definition: image_convolve.hpp:69
virtual int get_height() const
Return the height of this image.
Definition: image.hpp:46
convolve(int yradius, int xradius, int mode, image *L)
Convolve image L using a kernel of the given yradius and xradius. The kernel is expected to be suppli...
Definition: image_convolve.hpp:26
Horizontal Gaussian convolution filter.
Definition: image_convolve.hpp:138
gaussianh(double sigma, int mode, image *L)
Convolve image L using a horizontal 1D Gaussian kernel with standard deviation sigma. Mode gives the wrapping mode. The true radius of the kernel is 3 sigma, rounded up.
Definition: image_convolve.hpp:145
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_convolve.hpp:88
Vertical Gaussian convolution filter.
Definition: image_convolve.hpp:101
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_convolve.hpp:162
virtual void tweak(int a, int v)
Tweak image parameter a, changing the value by a factor of v.
Definition: image_convolve.hpp:73
Convolution filter.
Definition: image_convolve.hpp:20
virtual void tweak(int a, int v)
Tweak image parameter a, changing the value by a factor of v.
Definition: image_convolve.hpp:148
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_convolve.hpp:125