Visual Servoing Platform  version 3.0.0
vpImageFilter.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  * Various image tools, convolution, ...
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
38 
39 
40 #ifndef vpImageFilter_H
41 #define vpImageFilter_H
42 
49 #include <visp3/core/vpImage.h>
50 #include <visp3/core/vpImageException.h>
51 #include <visp3/core/vpMatrix.h>
52 #include <visp3/core/vpMath.h>
53 
54 #include <fstream>
55 #include <iostream>
56 #include <math.h>
57 #include <string.h>
58 
67 class VISP_EXPORT vpImageFilter
68 {
69 
70 public:
71 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
72  static void canny(const vpImage<unsigned char>& I,
74  const unsigned int gaussianFilterSize,
75  const double thresholdCanny,
76  const unsigned int apertureSobel);
77 #endif
78 
86  template<class T>
87  static double derivativeFilterX(const vpImage<T> &I,
88  const unsigned int r, const unsigned int c)
89  {
90  return (2047.0 *(I[r][c+1] - I[r][c-1])
91  +913.0 *(I[r][c+2] - I[r][c-2])
92  +112.0 *(I[r][c+3] - I[r][c-3]))/8418.0;
93  }
94 
102  template<class T>
103  static double derivativeFilterY(const vpImage<T> &I,
104  const unsigned int r, const unsigned int c)
105  {
106  return (2047.0 *(I[r+1][c] - I[r-1][c])
107  +913.0 *(I[r+2][c] - I[r-2][c])
108  +112.0 *(I[r+3][c] - I[r-3][c]))/8418.0;
109  }
110 
123  template<class T>
124  static double derivativeFilterX(const vpImage<T> &I,
125  const unsigned int r, const unsigned int c,
126  const double *filter, const unsigned int size)
127  {
128  unsigned int i;
129  double result;
130 
131  result = 0;
132 
133  for(i=1; i<=(size-1)/2; i++)
134  {
135  result += filter[i]*(I[r][c+i] - I[r][c-i]) ;
136  }
137  return result;
138  }
139 
140 
141 
153  template<class T>
154  static double derivativeFilterY(const vpImage<T> &I,
155  const unsigned int r, const unsigned int c,
156  const double *filter, const unsigned int size)
157  {
158  unsigned int i;
159  double result;
160 
161  result = 0;
162 
163  for(i=1; i<=(size-1)/2; i++)
164  {
165  result += filter[i]*(I[r+i][c] - I[r-i][c]) ;
166  }
167  return result;
168  }
169 
170  static void filter(const vpImage<double> &I,
171  vpImage<double>& Iu,
172  vpImage<double>& Iv,
173  const vpMatrix& M) ;
174 
175 
176  static void filter(const vpImage<unsigned char> &I,
177  vpImage<double>& If,
178  const vpMatrix& M) ;
179 
180 
181  static void filter(const vpImage<unsigned char> &I, vpImage<double>& GI, const double *filter,unsigned int size);
182  static void filter(const vpImage<double> &I, vpImage<double>& GI, const double *filter,unsigned int size);
183 
184  static inline unsigned char filterGaussXPyramidal(const vpImage<unsigned char> &I, unsigned int i, unsigned int j)
185  {
186  return (unsigned char)((1.*I[i][j-2]+4.*I[i][j-1]+6.*I[i][j]+4.*I[i][j+1]+1.*I[i][j+2])/16.);
187  }
188  static inline unsigned char filterGaussYPyramidal(const vpImage<unsigned char> &I, unsigned int i, unsigned int j)
189  {
190  return (unsigned char)((1.*I[i-2][j]+4.*I[i-1][j]+6.*I[i][j]+4.*I[i+1][j]+1.*I[i+2][j])/16.);
191  }
192 
193  static void filterX(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
194  static void filterX(const vpImage<double> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
195 
196  static inline double filterX(const vpImage<unsigned char> &I,
197  unsigned int r, unsigned int c,
198  const double *filter,unsigned int size)
199  {
200  double result;
201 
202  result = 0;
203 
204  for(unsigned int i=1; i<=(size-1)/2; i++)
205  {
206  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
207  }
208  return result+filter[0]*I[r][c];
209  }
210 
211 
212  static inline double filterXLeftBorder(const vpImage<unsigned char> &I,
213  unsigned int r, unsigned int c,
214  const double *filter,unsigned int size)
215  {
216  double result;
217 
218  result = 0;
219 
220  for(unsigned int i=1; i<=(size-1)/2; i++)
221  {
222  if(c>i)
223  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
224  else
225  result += filter[i]*(I[r][c+i] + I[r][i-c]) ;
226  }
227  return result+filter[0]*I[r][c];
228  }
229 
230  static inline double filterXRightBorder(const vpImage<unsigned char> &I,
231  unsigned int r, unsigned int c,
232  const double *filter,unsigned int size)
233  {
234  double result;
235 
236  result = 0;
237 
238  for(unsigned int i=1; i<=(size-1)/2; i++)
239  {
240  if(c+i<I.getWidth())
241  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
242  else
243  result += filter[i]*(I[r][2*I.getWidth()-c-i-1] + I[r][c-i]) ;
244  }
245  return result+filter[0]*I[r][c];
246  }
247 
248  static inline double filterX(const vpImage<double> &I,
249  unsigned int r, unsigned int c,
250  const double *filter,unsigned int size)
251  {
252  double result;
253 
254  result = 0;
255 
256  for(unsigned int i=1; i<=(size-1)/2; i++)
257  {
258  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
259  }
260  return result+filter[0]*I[r][c];
261  }
262 
263  static inline double filterXLeftBorder(const vpImage<double> &I,
264  unsigned int r, unsigned int c,
265  const double *filter,unsigned int size)
266  {
267  double result;
268 
269  result = 0;
270 
271  for(unsigned int i=1; i<=(size-1)/2; i++)
272  {
273  if(c>i)
274  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
275  else
276  result += filter[i]*(I[r][c+i] + I[r][i-c]) ;
277  }
278  return result+filter[0]*I[r][c];
279  }
280 
281  static inline double filterXRightBorder(const vpImage<double> &I,
282  unsigned int r, unsigned int c,
283  const double *filter,unsigned int size)
284  {
285  double result;
286 
287  result = 0;
288 
289  for(unsigned int i=1; i<=(size-1)/2; i++)
290  {
291  if(c+i<I.getWidth())
292  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
293  else
294  result += filter[i]*(I[r][2*I.getWidth()-c-i-1] + I[r][c-i]) ;
295  }
296  return result+filter[0]*I[r][c];
297  }
298 
299  static void filterY(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
300  static void filterY(const vpImage<double> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
301  static inline double filterY(const vpImage<unsigned char> &I,
302  unsigned int r, unsigned int c,
303  const double *filter,unsigned int size)
304  {
305  double result;
306 
307  result = 0;
308 
309  for(unsigned int i=1; i<=(size-1)/2; i++)
310  {
311  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
312  }
313  return result+filter[0]*I[r][c];
314  }
315 
316  double static inline filterYTopBorder(const vpImage<unsigned char> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
317  {
318  double result;
319 
320  result = 0;
321 
322  for(unsigned int i=1; i<=(size-1)/2; i++)
323  {
324  if(r>i)
325  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
326  else
327  result += filter[i]*(I[r+i][c] + I[i-r][c]) ;
328  }
329  return result+filter[0]*I[r][c];
330  }
331 
332  double static inline filterYBottomBorder(const vpImage<unsigned char> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
333  {
334  double result;
335 
336  result = 0;
337 
338  for(unsigned int i=1; i<=(size-1)/2; i++)
339  {
340  if(r+i<I.getHeight())
341  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
342  else
343  result += filter[i]*(I[2*I.getHeight()-r-i-1][c] + I[r-i][c]) ;
344  }
345  return result+filter[0]*I[r][c];
346  }
347 
348  static inline double filterYTopBorder(const vpImage<double> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
349  {
350  double result;
351 
352  result = 0;
353 
354  for(unsigned int i=1; i<=(size-1)/2; i++)
355  {
356  if(r>i)
357  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
358  else
359  result += filter[i]*(I[r+i][c] + I[i-r][c]) ;
360  }
361  return result+filter[0]*I[r][c];
362  }
363 
364  static inline double filterYBottomBorder(const vpImage<double> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
365  {
366  double result;
367 
368  result = 0;
369 
370  for(unsigned int i=1; i<=(size-1)/2; i++)
371  {
372  if(r+i<I.getHeight())
373  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
374  else
375  result += filter[i]*(I[2*I.getHeight()-r-i-1][c] + I[r-i][c]) ;
376  }
377  return result+filter[0]*I[r][c];
378  }
379 
380  static inline double filterY(const vpImage<double> &I,
381  unsigned int r, unsigned int c,
382  const double *filter,unsigned int size)
383  {
384  double result;
385 
386  result = 0;
387 
388  for(unsigned int i=1; i<=(size-1)/2; i++)
389  {
390  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
391  }
392  return result+filter[0]*I[r][c];
393  }
394 
395  static void gaussianBlur(const vpImage<unsigned char> &I, vpImage<double>& GI, unsigned int size=7, double sigma=0., bool normalize=true);
396  static void gaussianBlur(const vpImage<double> &I, vpImage<double>& GI, unsigned int size=7, double sigma=0., bool normalize=true);
404  template<class T>
405  static double gaussianFilter(const vpImage<T> & fr,
406  const unsigned int r, const unsigned int c)
407  {
408  //filter Gaussien
409  return (
410  15.0 * fr[r][c]
411  + 12.0 * ( fr[r-1][c] + fr[r][c-1] + fr[r+1][c] + fr[r][c+1] )
412  + 9.0 * ( fr[r-1][c-1] + fr[r+1][c-1] + fr[r-1][c+1] + fr[r+1][c+1])
413  + 5.0 * ( fr[r-2][c] + fr[r][c-2] + fr[r+2][c] + fr[r][c+2] )
414  + 4.0 * ( fr[r-2][c+1] + fr[r-2][c-1] + fr[r-1][c-2] + fr[r+1][c-2] +
415  fr[r+2][c-1] + fr[r+2][c+1] + fr[r-1][c+2] + fr[r+1][c+2] )
416  + 2.0 * ( fr[r-2][c-2] + fr[r+2][c-2] + fr[r-2][c+2] + fr[r+2][c+2] )
417  )
418  /159.0;
419  }
420  //operation pour pyramide gaussienne
421  static void getGaussPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
422  static void getGaussXPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
423  static void getGaussYPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
424 
425  static void getGaussianKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true);
426  static void getGaussianDerivativeKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true);
427 
428  //fonction renvoyant le gradient en X de l'image I pour traitement pyramidal => dimension /2
429  static void getGradX(const vpImage<unsigned char> &I, vpImage<double>& dIx);
430  static void getGradX(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter, unsigned int size);
431  static void getGradX(const vpImage<double> &I, vpImage<double>& dIx, const double *filter, unsigned int size);
432  static void getGradXGauss2D(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *gaussianKernel,
433  const double *gaussianDerivativeKernel, unsigned int size);
434 
435  //fonction renvoyant le gradient en Y de l'image I
436  static void getGradY(const vpImage<unsigned char> &I, vpImage<double>& dIy);
437  static void getGradY(const vpImage<unsigned char> &I, vpImage<double>& dIy, const double *filter, unsigned int size);
438  static void getGradY(const vpImage<double> &I, vpImage<double>& dIy, const double *filter, unsigned int size);
439  static void getGradYGauss2D(const vpImage<unsigned char> &I, vpImage<double>& dIy, const double *gaussianKernel,
440  const double *gaussianDerivativeKernel,unsigned int size);
441 
442 } ;
443 
444 
445 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
static double filterXLeftBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
unsigned int getWidth() const
Definition: vpImage.h:161
static double filterXLeftBorder(const vpImage< double > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterX(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double derivativeFilterY(const vpImage< T > &I, const unsigned int r, const unsigned int c)
static unsigned char filterGaussYPyramidal(const vpImage< unsigned char > &I, unsigned int i, unsigned int j)
static double gaussianFilter(const vpImage< T > &fr, const unsigned int r, const unsigned int c)
static double filterXRightBorder(const vpImage< double > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterY(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterYBottomBorder(const vpImage< double > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterX(const vpImage< double > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterYTopBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterXRightBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterY(const vpImage< double > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double filterYTopBorder(const vpImage< double > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static double derivativeFilterY(const vpImage< T > &I, const unsigned int r, const unsigned int c, const double *filter, const unsigned int size)
static double filterYBottomBorder(const vpImage< unsigned char > &I, unsigned int r, unsigned int c, const double *filter, unsigned int size)
static unsigned char filterGaussXPyramidal(const vpImage< unsigned char > &I, unsigned int i, unsigned int j)
unsigned int getHeight() const
Definition: vpImage.h:152
static double derivativeFilterX(const vpImage< T > &I, const unsigned int r, const unsigned int c)
Definition: vpImageFilter.h:87
static double derivativeFilterX(const vpImage< T > &I, const unsigned int r, const unsigned int c, const double *filter, const unsigned int size)
Various image filter, convolution, etc...
Definition: vpImageFilter.h:67