RAWK
Gigapixel Raw Image Processing Toolkit
 All Classes Functions Variables Pages
image_sobel.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_SOBEL_HPP
14 #define IMAGE_SOBEL_HPP
15 
16 //------------------------------------------------------------------------------
17 
19 
20 class sobelx : public image
21 {
22 public:
25 
26  sobelx(int mode, image *L) : image(L), mode(mode) { }
27 
28  virtual double get(int i, int j, int k) const
29  {
30  const int in = wrap(i - 1, L->get_height(), mode & 1);
31  const int is = wrap(i + 1, L->get_height(), mode & 1);
32  const int jw = wrap(j - 1, L->get_width (), mode & 2);
33  const int je = wrap(j + 1, L->get_width (), mode & 2);
34 
35  double d1 = L->get(in, jw, k);
36  double d3 = L->get(in, je, k);
37  double d4 = L->get(i, jw, k);
38  double d6 = L->get(i, je, k);
39  double d7 = L->get(is, jw, k);
40  double d9 = L->get(is, je, k);
41 
42  return d3 - d1 + 2.0 * (d6 - d4) + d9 - d7;
43  }
44 
45  virtual void doc(std::ostream& out) const
46  {
47  out << "sobelx " << mode;
48  }
49 
50 private:
51  int mode;
52 };
53 
54 //------------------------------------------------------------------------------
55 
57 
58 class sobely : public image
59 {
60 public:
63 
64  sobely(int mode, image *L) : image(L), mode(mode) { }
65 
66  virtual double get(int i, int j, int k) const
67  {
68  const int in = wrap(i - 1, L->get_height(), mode & 1);
69  const int is = wrap(i + 1, L->get_height(), mode & 1);
70  const int jw = wrap(j - 1, L->get_width (), mode & 2);
71  const int je = wrap(j + 1, L->get_width (), mode & 2);
72 
73  double d1 = L->get(in, jw, k);
74  double d2 = L->get(in, j, k);
75  double d3 = L->get(in, je, k);
76  double d7 = L->get(is, jw, k);
77  double d8 = L->get(is, j, k);
78  double d9 = L->get(is, je, k);
79 
80  return d7 - d1 + 2.0 * (d8 - d2) + d9 - d3;
81  }
82 
83  virtual void doc(std::ostream& out) const
84  {
85  out << "sobely " << mode;
86  }
87 
88 private:
89  int mode;
90 };
91 
92 //------------------------------------------------------------------------------
93 
95 
96 class relief : public image
97 {
98 public:
99  relief(double dy, double dx, int mode, image *L)
100  : image(L), dy(dy), dx(dx), mode(mode) { }
101 
102  virtual double get(int i, int j, int k) const
103  {
104  const int in = wrap(i - 1, L->get_height(), mode & 1);
105  const int is = wrap(i + 1, L->get_height(), mode & 1);
106  const int jw = wrap(j - 1, L->get_width (), mode & 2);
107  const int je = wrap(j + 1, L->get_width (), mode & 2);
108 
109  double d1 = L->get(in, jw, k);
110  double d2 = L->get(in, j, k);
111  double d3 = L->get(in, je, k);
112  double d4 = L->get(i, jw, k);
113  double d6 = L->get(i, je, k);
114  double d7 = L->get(is, jw, k);
115  double d8 = L->get(is, j, k);
116  double d9 = L->get(is, je, k);
117 
118  double Ly = d7 - d1 + 2.0 * (d8 - d2) + d9 - d3;
119  double Lx = d3 - d1 + 2.0 * (d6 - d4) + d9 - d7;
120 
121  return 0.5 + Ly * dy + Lx * dx;
122  }
123 
124  virtual void tweak(int a, int v)
125  {
126  if (a == 0) dx += v;
127  if (a == 1) dy += v;
128  }
129 
130  virtual void doc(std::ostream& out) const
131  {
132  out << "relief " << dy << " " << dx << " " << mode;
133  }
134 
135 private:
136  double dy;
137  double dx;
138  int mode;
139 };
140 
141 //------------------------------------------------------------------------------
142 
144 
145 class gradient : public image
146 {
147 public:
151 
152  gradient(int mode, image *L) : image(L), mode(mode) { }
153 
154  virtual double get(int i, int j, int k) const
155  {
156  const int in = wrap(i - 1, L->get_height(), mode & 1);
157  const int is = wrap(i + 1, L->get_height(), mode & 1);
158  const int jw = wrap(j - 1, L->get_width (), mode & 2);
159  const int je = wrap(j + 1, L->get_width (), mode & 2);
160 
161  double d1 = L->get(in, jw, k);
162  double d2 = L->get(in, j, k);
163  double d3 = L->get(in, je, k);
164  double d4 = L->get(i, jw, k);
165  double d6 = L->get(i, je, k);
166  double d7 = L->get(is, jw, k);
167  double d8 = L->get(is, j, k);
168  double d9 = L->get(is, je, k);
169 
170  double Lx = d3 - d1 + 2.0 * (d6 - d4) + d9 - d7;
171  double Ly = d7 - d1 + 2.0 * (d8 - d2) + d9 - d3;
172 
173  return sqrt(Lx * Lx + Ly * Ly);
174  }
175 
176  virtual void doc(std::ostream& out) const
177  {
178  out << "gradient " << mode;
179  }
180 
181 private:
182  int mode;
183 };
184 
185 //------------------------------------------------------------------------------
186 
187 #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
image(image *L=0, image *R=0)
Create a new image object with left child L and right child R. The parents of L and R are set to this...
Definition: image.hpp:26
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_sobel.hpp:130
Horizontal Sobel filter.
Definition: image_sobel.hpp:20
virtual void tweak(int a, int v)
Tweak image parameter a, changing the value by a factor of v.
Definition: image_sobel.hpp:124
sobelx(int mode, image *L)
Apply the horizontal 3x3 Sobel filter to image L. This will detect vertical lines in the input...
Definition: image_sobel.hpp:26
Gradient filter.
Definition: image_sobel.hpp:145
Base class for all image sources, filters, and operators.
Definition: image.hpp:20
gradient(int mode, image *L)
Compute the magnitude of the gradient of image L. This is the value of the greatest rate of change in...
Definition: image_sobel.hpp:152
virtual int get_height() const
Return the height of this image.
Definition: image.hpp:46
Vertical Sobol filter.
Definition: image_sobel.hpp:58
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_sobel.hpp:45
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_sobel.hpp:176
sobely(int mode, image *L)
Apply the vertical 3x3 Sobel filter to image L. This will detect horizontal lines in the input...
Definition: image_sobel.hpp:64
Relief shading filter.
Definition: image_sobel.hpp:96
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_sobel.hpp:83