Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpImageMorphology.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Morphology tools.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
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 
67 class VISP_EXPORT vpImageMorphology
68 {
69 public:
73  typedef enum {
74  CONNEXITY_4,
76  CONNEXITY_8
78  } vpConnexityType;
79 
80 public:
81  template<class Type>
82  static void erosion(vpImage<Type> &I, Type value, Type value_out,
83  vpConnexityType connexity = CONNEXITY_4);
84 
85  template<class Type>
86  static void dilatation(vpImage<Type> &I, Type value, Type value_out,
87  vpConnexityType connexity = CONNEXITY_4);
88 
89  static void erosion(vpImage<unsigned char> &I, const vpConnexityType &connexity = CONNEXITY_4);
90  static void dilatation(vpImage<unsigned char> &I, const vpConnexityType &connexity = CONNEXITY_4);
91 } ;
92 
110 template<class Type>
112  Type value,
113  Type value_out,
114  vpConnexityType connexity)
115 {
116  if(I.getSize() == 0) {
117  std::cerr << "Input image is empty!" << std::endl;
118  return;
119  }
120 
121  vpImage<Type> J(I.getHeight()+2, I.getWidth()+2);
122  // Copy I to J and add border
123  for (unsigned int i = 0; i < J.getHeight(); i++) {
124  if (i == 0 || i == J.getHeight() - 1) {
125  for (unsigned int j = 0; j < J.getWidth(); j++) {
126  J[i][j] = value;
127  }
128  } else {
129  J[i][0] = value;
130  memcpy(J[i]+1, I[i-1], sizeof(unsigned char)*I.getWidth());
131  J[i][J.getWidth() - 1] = value;
132  }
133  }
134 
135  if (connexity == CONNEXITY_4) {
136  for (unsigned int i = 0; i < I.getHeight(); i++) {
137  for (unsigned int j = 0; j < I.getWidth(); j++) {
138  if (J[i+1][j+1] == value) {
139  // Consider 4 neighbors
140  if ((J[i][j+1] == value_out) || //Top
141  (J[i+2][j+1] == value_out) || //Bottom
142  (J[i+1][j] == value_out) || //Left
143  (J[i+1][j+2] == value_out)) { //Right
144  I[i][j] = value_out;
145  }
146  }
147  }
148  }
149  }
150  else {
151  for (unsigned int i = 0; i < I.getHeight(); i++) {
152  for (unsigned int j = 0; j < I.getWidth(); j++) {
153  if (J[i+1][j+1] == value) {
154  // Consider 8 neighbors
155  if ((J[i][j] == value_out) ||
156  (J[i][j+1] == value_out) ||
157  (J[i][j+2] == value_out) ||
158  (J[i+1][j] == value_out) ||
159  (J[i+1][j+2] == value_out) ||
160  (J[i+2][j] == value_out) ||
161  (J[i+2][j+1] == value_out) ||
162  (J[i+2][j+2] == value_out) )
163  I[i][j] = value_out ;
164  }
165  }
166  }
167  }
168 }
169 
187 template<class Type>
189  Type value,
190  Type value_out,
191  vpConnexityType connexity)
192 {
193  if(I.getSize() == 0) {
194  std::cerr << "Input image is empty!" << std::endl;
195  return;
196  }
197 
198  vpImage<Type> J(I.getHeight()+2, I.getWidth()+2);
199  // Copy I to J and add border
200  for (unsigned int i = 0; i < J.getHeight(); i++) {
201  if (i == 0 || i == J.getHeight() - 1) {
202  for (unsigned int j = 0; j < J.getWidth(); j++) {
203  J[i][j] = value_out;
204  }
205  } else {
206  J[i][0] = value_out;
207  memcpy(J[i]+1, I[i-1], sizeof(unsigned char)*I.getWidth());
208  J[i][J.getWidth() - 1] = value_out;
209  }
210  }
211 
212  if (connexity == CONNEXITY_4) {
213  for (unsigned int i = 0; i < I.getHeight(); i++) {
214  for (unsigned int j = 0; j < I.getWidth(); j++) {
215  if (J[i+1][j+1] == value_out) {
216  // Consider 4 neighbors
217  if ((J[i][j+1] == value) || //Top
218  (J[i+2][j+1] == value) || //Bottom
219  (J[i+1][j] == value) || //Left
220  (J[i+1][j+2] == value)) { //Right
221  I[i][j] = value;
222  }
223  }
224  }
225  }
226  }
227  else {
228  for (unsigned int i = 0; i < I.getHeight(); i++) {
229  for (unsigned int j = 0; j < I.getWidth(); j++) {
230  if (J[i+1][j+1] == value_out) {
231  // Consider 8 neighbors
232  if ((J[i][j] == value) ||
233  (J[i][j+1] == value) ||
234  (J[i][j+2] == value) ||
235  (J[i+1][j] == value) ||
236  (J[i+1][j+2] == value) ||
237  (J[i+2][j] == value) ||
238  (J[i+2][j+1] == value) ||
239  (J[i+2][j+2] == value) ) {
240  I[i][j] = value ;
241  }
242  }
243  }
244  }
245  }
246 }
247 #endif
248 
249 
250 /*
251  * Local variables:
252  * c-basic-offset: 2
253  * End:
254  */
Various mathematical morphology tools, erosion, dilatation...
static void dilatation(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
unsigned int getWidth() const
Definition: vpImage.h:226
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
unsigned int getSize() const
Definition: vpImage.h:212
unsigned int getHeight() const
Definition: vpImage.h:175
Definition of the vpImage class member functions.
Definition: vpImage.h:117