Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
morpho_math.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef H_IMAGE_ALGOS_MORPHO_MATH_H
41 #define H_IMAGE_ALGOS_MORPHO_MATH_H
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/image/image.h>
45 
51 namespace GEO {
52 
57  class GEOGRAM_API StructuringElement {
58  public:
59 
64  StructuringElement(Image* source) : source_(source) {
65  base_mem_ = source_->base_mem();
66  width_ = source_->width();
67  radius_ = 0;
68  bytes_per_pixel_ = source->bytes_per_pixel();
69  }
70 
76  index_t radius() const {
77  return radius_;
78  }
79 
85  void add_neighbor(int xrel, int yrel) {
86  offset_.push_back(
87  (xrel + int(width_) * yrel) * int(bytes_per_pixel_)
88  );
89  radius_ = std::max(radius_, index_t(std::abs(xrel)));
90  radius_ = std::max(radius_, index_t(std::abs(yrel)));
91  }
92 
100  void convolve(Memory::byte* from, Memory::byte* to) const;
101 
109  inline void convolve(int x, int y, Image* target_img) const {
110  int pixel_base = ((x + int(width_) * y) * int(bytes_per_pixel_));
111  convolve(
112  base_mem_ + pixel_base, target_img->base_mem() + pixel_base
113  );
114  }
115 
116  private:
117  Memory::byte* base_mem_;
118  index_t width_;
119  Image* source_;
120  index_t radius_;
121  vector<int> offset_;
122  size_t bytes_per_pixel_;
123  };
124 
125 
129  class GEOGRAM_API MorphoMath {
130  public:
137  MorphoMath(Image* target);
138 
143 
149  void dilate(const StructuringElement& elt, index_t nb_iterations = 1);
150 
155  void dilate(index_t nb_iterations = 1);
156 
157  private:
158  Image* target_;
159  Numeric::uint8* graph_mem_;
160  index_t width_;
161  index_t height_;
162  size_t bytes_per_pixel_;
163  size_t bytes_per_line_;
164  };
165 }
166 
167 #endif
An image.
Definition: image.h:59
Memory::pointer base_mem() const
Gets the base memory.
Definition: image.h:240
size_t bytes_per_pixel() const
Gets the number of bytes per pixel.
Definition: image.h:165
Implements morphological operators for images.
Definition: morpho_math.h:129
void dilate(index_t nb_iterations=1)
Computes a dilation with a default structuring element.
~MorphoMath()
MorphoMath destructor;.
MorphoMath(Image *target)
MorphoMath constructor.
void dilate(const StructuringElement &elt, index_t nb_iterations=1)
Computes a dilation.
A structuring element, that is the definition of neighborhood used by a morphological operation.
Definition: morpho_math.h:57
void convolve(Memory::byte *from, Memory::byte *to) const
Computes the convolution at a given memory location.
void convolve(int x, int y, Image *target_img) const
Computes the convolution at a given pixel.
Definition: morpho_math.h:109
void add_neighbor(int xrel, int yrel)
Adds a neighbor to this structuting element.
Definition: morpho_math.h:85
index_t radius() const
Gets the radius.
Definition: morpho_math.h:76
StructuringElement(Image *source)
StructuringElement constructor.
Definition: morpho_math.h:64
Common include file, providing basic definitions. Should be included before anything else by all head...
unsigned char byte
Unsigned byte type.
Definition: memory.h:92
uint8_t uint8
Definition: numeric.h:135
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329