Visual Servoing Platform  version 3.0.0
vpImageMorphology.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 
68 {
69 public:
73  typedef enum {
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 } ;
90 
108 template<class Type>
110  Type value,
111  Type value_out,
112  vpConnexityType connexity)
113 {
114  vpImage<Type> J(I.getHeight(), I.getWidth()) ;
115  J = I;
116 
117  if (connexity == CONNEXITY_4) {
118  for (unsigned int i=1 ; i < I.getHeight()-1 ; i++)
119  for (unsigned int j=1 ; j < I.getWidth()-1 ; j++)
120  {
121  if (I[i][j] == value)
122  {
123  // Consider 4 neighbors
124  if ((I[i-1][j] == value_out) ||
125  (I[i+1][j] == value_out) ||
126  (I[i][j-1] == value_out) ||
127  (I[i][j+1] == value_out))
128  J[i][j] = value_out;
129  }
130  }
131  }
132  else {
133  for (unsigned int i=1 ; i < I.getHeight()-1 ; i++)
134  for (unsigned int j=1 ; j < I.getWidth()-1 ; j++)
135  {
136  if (I[i][j] == value)
137  {
138  // Consider 8 neighbors
139  if ((I[i-1][j-1] == value_out) ||
140  (I[i-1][j] == value_out) ||
141  (I[i-1][j+1] == value_out) ||
142  (I[i][j-1] == value_out) ||
143  (I[i][j+1] == value_out) ||
144  (I[i+1][j+1] == value_out) ||
145  (I[i+1][j+1] == value_out) ||
146  (I[i+1][j+1] == value_out) )
147  J[i][j] = value_out ;
148  }
149  }
150 
151 
152  }
153  I = J ;
154 }
155 
173 template<class Type>
175  Type value,
176  Type value_out,
177  vpConnexityType connexity)
178 {
179  vpImage<Type> J(I.getHeight(), I.getWidth()) ;
180  J = I;
181  if (connexity == CONNEXITY_4) {
182  for (unsigned int i=1 ; i < I.getHeight()-1 ; i++)
183  for (unsigned int j=1 ; j < I.getWidth()-1 ; j++)
184  {
185  if (I[i][j] == value_out)
186  {
187  // Consider 4 neighbors
188  if ((I[i-1][j] == value) ||
189  (I[i+1][j] == value) ||
190  (I[i][j-1] == value) ||
191  (I[i][j+1] == value))
192  J[i][j] = value ;
193  }
194  }
195  }
196  else {
197  for (unsigned int i=1 ; i < I.getHeight()-1 ; i++)
198  for (unsigned int j=1 ; j < I.getWidth()-1 ; j++)
199  {
200  if (I[i][j] == value_out)
201  {
202  // Consider 8 neighbors
203  if ((I[i-1][j-1] == value) ||
204  (I[i-1][j] == value) ||
205  (I[i-1][j+1] == value) ||
206  (I[i][j-1] == value) ||
207  (I[i][j+1] == value) ||
208  (I[i+1][j+1] == value) ||
209  (I[i+1][j+1] == value) ||
210  (I[i+1][j+1] == value) )
211  J[i][j] = value ;
212  }
213  }
214  }
215 
216  I = J ;
217 }
218 #endif
219 
220 
221 /*
222  * Local variables:
223  * c-basic-offset: 2
224  * End:
225  */
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:161
static void erosion(vpImage< Type > &I, Type value, Type value_out, vpConnexityType connexity=CONNEXITY_4)
unsigned int getHeight() const
Definition: vpImage.h:152
Definition of the vpImage class member functions.
Definition: vpImage.h:111