Visual Servoing Platform version 3.5.0
vpImageMorphology.h
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See http://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Morphology tools.
33 *
34 * Authors:
35 * Fabien Spindler
36 *
37 *****************************************************************************/
38
39#ifndef vpImageMorphology_H
40#define vpImageMorphology_H
41
47#include <visp3/core/vpImage.h>
48#include <visp3/core/vpImageException.h>
49#include <visp3/core/vpMatrix.h>
50
51#include <fstream>
52#include <iostream>
53#include <math.h>
54#include <string.h>
55
67class VISP_EXPORT vpImageMorphology
68{
69public:
73 typedef enum {
74 CONNEXITY_4,
76 CONNEXITY_8
79 } vpConnexityType;
80
81public:
82 template <class Type>
83 static void erosion(vpImage<Type> &I, Type value, Type value_out, vpConnexityType connexity = CONNEXITY_4);
84
85 template <class Type>
86 static void dilatation(vpImage<Type> &I, Type value, Type value_out, vpConnexityType connexity = CONNEXITY_4);
87
88 static void erosion(vpImage<unsigned char> &I, const vpConnexityType &connexity = CONNEXITY_4);
89 static void dilatation(vpImage<unsigned char> &I, const vpConnexityType &connexity = CONNEXITY_4);
90};
91
109template <class Type>
110void vpImageMorphology::erosion(vpImage<Type> &I, Type value, Type value_out, vpConnexityType connexity)
111{
112 if (I.getSize() == 0) {
113 std::cerr << "Input image is empty!" << std::endl;
114 return;
115 }
116
117 vpImage<Type> J(I.getHeight() + 2, I.getWidth() + 2);
118 // Copy I to J and add border
119 for (unsigned int i = 0; i < J.getHeight(); i++) {
120 if (i == 0 || i == J.getHeight() - 1) {
121 for (unsigned int j = 0; j < J.getWidth(); j++) {
122 J[i][j] = value;
123 }
124 } else {
125 J[i][0] = value;
126 memcpy(J[i] + 1, I[i - 1], sizeof(unsigned char) * I.getWidth());
127 J[i][J.getWidth() - 1] = value;
128 }
129 }
130
131 if (connexity == CONNEXITY_4) {
132 for (unsigned int i = 0; i < I.getHeight(); i++) {
133 for (unsigned int j = 0; j < I.getWidth(); j++) {
134 if (J[i + 1][j + 1] == value) {
135 // Consider 4 neighbors
136 if ((J[i][j + 1] == value_out) || // Top
137 (J[i + 2][j + 1] == value_out) || // Bottom
138 (J[i + 1][j] == value_out) || // Left
139 (J[i + 1][j + 2] == value_out)) { // Right
140 I[i][j] = value_out;
141 }
142 }
143 }
144 }
145 } else {
146 for (unsigned int i = 0; i < I.getHeight(); i++) {
147 for (unsigned int j = 0; j < I.getWidth(); j++) {
148 if (J[i + 1][j + 1] == value) {
149 // Consider 8 neighbors
150 if ((J[i][j] == value_out) || (J[i][j + 1] == value_out) || (J[i][j + 2] == value_out) ||
151 (J[i + 1][j] == value_out) || (J[i + 1][j + 2] == value_out) || (J[i + 2][j] == value_out) ||
152 (J[i + 2][j + 1] == value_out) || (J[i + 2][j + 2] == value_out))
153 I[i][j] = value_out;
154 }
155 }
156 }
157 }
158}
159
177template <class Type>
178void vpImageMorphology::dilatation(vpImage<Type> &I, Type value, Type value_out, vpConnexityType connexity)
179{
180 if (I.getSize() == 0) {
181 std::cerr << "Input image is empty!" << std::endl;
182 return;
183 }
184
185 vpImage<Type> J(I.getHeight() + 2, I.getWidth() + 2);
186 // Copy I to J and add border
187 for (unsigned int i = 0; i < J.getHeight(); i++) {
188 if (i == 0 || i == J.getHeight() - 1) {
189 for (unsigned int j = 0; j < J.getWidth(); j++) {
190 J[i][j] = value_out;
191 }
192 } else {
193 J[i][0] = value_out;
194 memcpy(J[i] + 1, I[i - 1], sizeof(unsigned char) * I.getWidth());
195 J[i][J.getWidth() - 1] = value_out;
196 }
197 }
198
199 if (connexity == CONNEXITY_4) {
200 for (unsigned int i = 0; i < I.getHeight(); i++) {
201 for (unsigned int j = 0; j < I.getWidth(); j++) {
202 if (J[i + 1][j + 1] == value_out) {
203 // Consider 4 neighbors
204 if ((J[i][j + 1] == value) || // Top
205 (J[i + 2][j + 1] == value) || // Bottom
206 (J[i + 1][j] == value) || // Left
207 (J[i + 1][j + 2] == value)) { // Right
208 I[i][j] = value;
209 }
210 }
211 }
212 }
213 } else {
214 for (unsigned int i = 0; i < I.getHeight(); i++) {
215 for (unsigned int j = 0; j < I.getWidth(); j++) {
216 if (J[i + 1][j + 1] == value_out) {
217 // Consider 8 neighbors
218 if ((J[i][j] == value) || (J[i][j + 1] == value) || (J[i][j + 2] == value) || (J[i + 1][j] == value) ||
219 (J[i + 1][j + 2] == value) || (J[i + 2][j] == value) || (J[i + 2][j + 1] == value) ||
220 (J[i + 2][j + 2] == value)) {
221 I[i][j] = value;
222 }
223 }
224 }
225 }
226 }
227}
228#endif
229
230/*
231 * Local variables:
232 * c-basic-offset: 2
233 * End:
234 */
Various mathematical morphology tools, erosion, dilatation...
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
Definition of the vpImage class member functions.
Definition: vpImage.h:139
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getSize() const
Definition: vpImage.h:227
unsigned int getHeight() const
Definition: vpImage.h:188