RAWK
Gigapixel Raw Image Processing Toolkit
 All Classes Functions Variables Pages
image_morphology.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_MORPHOLOGY_HPP
14 #define IMAGE_MORPHOLOGY_HPP
15 
16 //------------------------------------------------------------------------------
17 
19 
20 class dilate : public image
21 {
22 public:
27 
28  dilate(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  double v = std::numeric_limits<double>::min();
37 
38  for (int y = -radius; y <= +radius; y++)
39  for (int x = -radius; x <= +radius; x++)
40 
41  if (x * x + y * y <= radius * radius)
42  v = std::max(v, L->get(wrap(i + y, h, mode & 1),
43  wrap(j + x, w, mode & 2), k));
44  return v;
45  }
46 
47  virtual void doc(std::ostream& out) const
48  {
49  out << "dilate " << radius << " " << mode;
50  }
51 
52 protected:
53  int radius;
54  int mode;
55 };
56 
57 //------------------------------------------------------------------------------
58 
60 
61 class erode : public image
62 {
63 public:
68 
69  erode(int radius, int mode, image *L)
70  : image(L), radius(radius), mode(mode) { }
71 
72  virtual double get(int i, int j, int k) const
73  {
74  const int h = L->get_height();
75  const int w = L->get_width ();
76 
77  double v = std::numeric_limits<double>::max();
78 
79  for (int y = -radius; y <= +radius; y++)
80  for (int x = -radius; x <= +radius; x++)
81 
82  if (x * x + y * y <= radius * radius)
83  v = std::min(v, L->get(wrap(i + y, h, mode & 1),
84  wrap(j + x, w, mode & 2), k));
85  return v;
86  }
87 
88  virtual void doc(std::ostream& out) const
89  {
90  out << "erode " << radius << " " << mode;
91  }
92 
93 protected:
94  int radius;
95  int mode;
96 };
97 
98 //------------------------------------------------------------------------------
99 
100 #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.
Dilation filter.
Definition: image_morphology.hpp:20
image * L
Left child.
Definition: image.hpp:117
dilate(int radius, int mode, image *L)
Apply a morphological dilate with a circular kernel of the given radius to image L, wrapped by the given mode. The dilate filter gives the maximum value found among all pixels within the kernel footprint.
Definition: image_morphology.hpp:28
Base class for all image sources, filters, and operators.
Definition: image.hpp:20
virtual int get_height() const
Return the height of this image.
Definition: image.hpp:46
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_morphology.hpp:47
virtual int get_width() const
Return the height of this image.
Definition: image.hpp:56
erode(int radius, int mode, image *L)
Apply a morphological erode with a circular kernel of the given radius to image L, wrapped by the given mode. The erode filter gives the minimum value found among all pixels within the kernel footprint.
Definition: image_morphology.hpp:69
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_morphology.hpp:88
Erosion filter.
Definition: image_morphology.hpp:61