| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 46 | /** | ||
| 47 | * \file geogram/image/morpho_math.h | ||
| 48 | * \brief Classes for morphological operations on images. | ||
| 49 | */ | ||
| 50 | |||
| 51 | namespace GEO { | ||
| 52 | |||
| 53 | /** | ||
| 54 | * \brief A structuring element, that is the definition of | ||
| 55 | * neighborhood used by a morphological operation. | ||
| 56 | */ | ||
| 57 | ✗ | class GEOGRAM_API StructuringElement { | |
| 58 | public: | ||
| 59 | |||
| 60 | /** | ||
| 61 | * \brief StructuringElement constructor. | ||
| 62 | * \param[in] source a pointer to the source image. | ||
| 63 | */ | ||
| 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 | |||
| 71 | /** | ||
| 72 | * \brief Gets the radius. | ||
| 73 | * \return the maximum difference of coordinate between the | ||
| 74 | * center and one of the neighbors. | ||
| 75 | */ | ||
| 76 | index_t radius() const { | ||
| 77 | ✗ | return radius_; | |
| 78 | } | ||
| 79 | |||
| 80 | /** | ||
| 81 | * \brief Adds a neighbor to this structuting element. | ||
| 82 | * \param[in] xrel , yrel the coordinates of the neighbor, | ||
| 83 | * relative to the center of this structuring element. | ||
| 84 | */ | ||
| 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 | |||
| 93 | /** | ||
| 94 | * \brief Computes the convolution at a given memory location. | ||
| 95 | * \param[in] from a pointer to the source pixel at the center | ||
| 96 | * of the structuring element. | ||
| 97 | * \param[in] to a pointer to the target pixel at the center of | ||
| 98 | * the structuring element. | ||
| 99 | */ | ||
| 100 | void convolve(Memory::byte* from, Memory::byte* to) const; | ||
| 101 | |||
| 102 | /** | ||
| 103 | * \brief Computes the convolution at a given pixel. | ||
| 104 | * \details The source image is the one that was specified to the | ||
| 105 | * constructor of this StructuringElement. | ||
| 106 | * \param[in] x , y the coordinates of the pixel. | ||
| 107 | * \param[in] target_img a pointer to the target image. | ||
| 108 | */ | ||
| 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 | |||
| 126 | /** | ||
| 127 | * \brief Implements morphological operators for images. | ||
| 128 | */ | ||
| 129 | class GEOGRAM_API MorphoMath { | ||
| 130 | public: | ||
| 131 | /** | ||
| 132 | * \brief MorphoMath constructor. | ||
| 133 | * \details Only implemented for 2D images with byte components. | ||
| 134 | * \param[in] target a pointer to the target image. | ||
| 135 | * \pre target->component_encoding() == Image::BYTE | ||
| 136 | */ | ||
| 137 | MorphoMath(Image* target); | ||
| 138 | |||
| 139 | /** | ||
| 140 | * \brief MorphoMath destructor; | ||
| 141 | */ | ||
| 142 | ~MorphoMath(); | ||
| 143 | |||
| 144 | /** | ||
| 145 | * \brief Computes a dilation. | ||
| 146 | * \param[in] elt a const reference to the structuring element. | ||
| 147 | * \param[in] nb_iterations number of dilations to be applied. | ||
| 148 | */ | ||
| 149 | void dilate(const StructuringElement& elt, index_t nb_iterations = 1); | ||
| 150 | |||
| 151 | /** | ||
| 152 | * \brief Computes a dilation with a default structuring element. | ||
| 153 | * \param[in] nb_iterations number of dilations to be applied. | ||
| 154 | */ | ||
| 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 | ||
| 168 |