ViSP  2.9.0
vpImageFilter.h
1 /****************************************************************************
2  *
3  * $Id: vpImageFilter.h 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Various image tools, convolution, ...
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
43 
44 #ifndef vpImageFilter_H
45 #define vpImageFilter_H
46 
53 #include <visp/vpImage.h>
54 #include <visp/vpImageException.h>
55 #include <visp/vpMatrix.h>
56 #include <visp/vpMath.h>
57 
58 #include <fstream>
59 #include <iostream>
60 #include <math.h>
61 #include <string.h>
62 
71 class VISP_EXPORT vpImageFilter
72 {
73 
74 public:
75 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
76  static void canny(const vpImage<unsigned char>& I,
78  const unsigned int gaussianFilterSize,
79  const double thresholdCanny,
80  const unsigned int apertureSobel);
81 #endif
82 
90  template<class T>
91  static double derivativeFilterX(const vpImage<T> &I,
92  const unsigned int r, const unsigned int c)
93  {
94  return (2047.0 *(I[r][c+1] - I[r][c-1])
95  +913.0 *(I[r][c+2] - I[r][c-2])
96  +112.0 *(I[r][c+3] - I[r][c-3]))/8418.0;
97  }
98 
106  template<class T>
107  static double derivativeFilterY(const vpImage<T> &I,
108  const unsigned int r, const unsigned int c)
109  {
110  return (2047.0 *(I[r+1][c] - I[r-1][c])
111  +913.0 *(I[r+2][c] - I[r-2][c])
112  +112.0 *(I[r+3][c] - I[r-3][c]))/8418.0;
113  }
114 
127  template<class T>
128  static double derivativeFilterX(const vpImage<T> &I,
129  const unsigned int r, const unsigned int c,
130  const double *filter, const unsigned int size)
131  {
132  unsigned int i;
133  double result;
134 
135  result = 0;
136 
137  for(i=1; i<=(size-1)/2; i++)
138  {
139  result += filter[i]*(I[r][c+i] - I[r][c-i]) ;
140  }
141  return result;
142  }
143 
144 
145 
157  template<class T>
158  static double derivativeFilterY(const vpImage<T> &I,
159  const unsigned int r, const unsigned int c,
160  const double *filter, const unsigned int size)
161  {
162  unsigned int i;
163  double result;
164 
165  result = 0;
166 
167  for(i=1; i<=(size-1)/2; i++)
168  {
169  result += filter[i]*(I[r+i][c] - I[r-i][c]) ;
170  }
171  return result;
172  }
173 
174  static void filter(const vpImage<double> &I,
175  vpImage<double>& Iu,
176  vpImage<double>& Iv,
177  const vpMatrix& M) ;
178 
179 
180  static void filter(const vpImage<unsigned char> &I,
181  vpImage<double>& If,
182  const vpMatrix& M) ;
183 
184 
185  static void filter(const vpImage<unsigned char> &I, vpImage<double>& GI, const double *filter,unsigned int size);
186  static void filter(const vpImage<double> &I, vpImage<double>& GI, const double *filter,unsigned int size);
187 
188  static inline unsigned char filterGaussXPyramidal(const vpImage<unsigned char> &I, unsigned int i, unsigned int j)
189  {
190  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.);
191  }
192  static inline unsigned char filterGaussYPyramidal(const vpImage<unsigned char> &I, unsigned int i, unsigned int j)
193  {
194  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.);
195  }
196 
197  static void filterX(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
198  static void filterX(const vpImage<double> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
199 
200  static inline double filterX(const vpImage<unsigned char> &I,
201  unsigned int r, unsigned int c,
202  const double *filter,unsigned int size)
203  {
204  double result;
205 
206  result = 0;
207 
208  for(unsigned int i=1; i<=(size-1)/2; i++)
209  {
210  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
211  }
212  return result+filter[0]*I[r][c];
213  }
214 
215 
216  static inline double filterXLeftBorder(const vpImage<unsigned char> &I,
217  unsigned int r, unsigned int c,
218  const double *filter,unsigned int size)
219  {
220  double result;
221 
222  result = 0;
223 
224  for(unsigned int i=1; i<=(size-1)/2; i++)
225  {
226  if(c>i)
227  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
228  else
229  result += filter[i]*(I[r][c+i] + I[r][i-c]) ;
230  }
231  return result+filter[0]*I[r][c];
232  }
233 
234  static inline double filterXRightBorder(const vpImage<unsigned char> &I,
235  unsigned int r, unsigned int c,
236  const double *filter,unsigned int size)
237  {
238  double result;
239 
240  result = 0;
241 
242  for(unsigned int i=1; i<=(size-1)/2; i++)
243  {
244  if(c+i<I.getWidth())
245  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
246  else
247  result += filter[i]*(I[r][2*I.getWidth()-c-i-1] + I[r][c-i]) ;
248  }
249  return result+filter[0]*I[r][c];
250  }
251 
252  static inline double filterX(const vpImage<double> &I,
253  unsigned int r, unsigned int c,
254  const double *filter,unsigned int size)
255  {
256  double result;
257 
258  result = 0;
259 
260  for(unsigned int i=1; i<=(size-1)/2; i++)
261  {
262  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
263  }
264  return result+filter[0]*I[r][c];
265  }
266 
267  static inline double filterXLeftBorder(const vpImage<double> &I,
268  unsigned int r, unsigned int c,
269  const double *filter,unsigned int size)
270  {
271  double result;
272 
273  result = 0;
274 
275  for(unsigned int i=1; i<=(size-1)/2; i++)
276  {
277  if(c>i)
278  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
279  else
280  result += filter[i]*(I[r][c+i] + I[r][i-c]) ;
281  }
282  return result+filter[0]*I[r][c];
283  }
284 
285  static inline double filterXRightBorder(const vpImage<double> &I,
286  unsigned int r, unsigned int c,
287  const double *filter,unsigned int size)
288  {
289  double result;
290 
291  result = 0;
292 
293  for(unsigned int i=1; i<=(size-1)/2; i++)
294  {
295  if(c+i<I.getWidth())
296  result += filter[i]*(I[r][c+i] + I[r][c-i]) ;
297  else
298  result += filter[i]*(I[r][2*I.getWidth()-c-i-1] + I[r][c-i]) ;
299  }
300  return result+filter[0]*I[r][c];
301  }
302 
303  static void filterY(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
304  static void filterY(const vpImage<double> &I, vpImage<double>& dIx, const double *filter,unsigned int size);
305  static inline double filterY(const vpImage<unsigned char> &I,
306  unsigned int r, unsigned int c,
307  const double *filter,unsigned int size)
308  {
309  double result;
310 
311  result = 0;
312 
313  for(unsigned int i=1; i<=(size-1)/2; i++)
314  {
315  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
316  }
317  return result+filter[0]*I[r][c];
318  }
319 
320  double static inline filterYTopBorder(const vpImage<unsigned char> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
321  {
322  double result;
323 
324  result = 0;
325 
326  for(unsigned int i=1; i<=(size-1)/2; i++)
327  {
328  if(r>i)
329  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
330  else
331  result += filter[i]*(I[r+i][c] + I[i-r][c]) ;
332  }
333  return result+filter[0]*I[r][c];
334  }
335 
336  double static inline filterYBottomBorder(const vpImage<unsigned char> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
337  {
338  double result;
339 
340  result = 0;
341 
342  for(unsigned int i=1; i<=(size-1)/2; i++)
343  {
344  if(r+i<I.getHeight())
345  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
346  else
347  result += filter[i]*(I[2*I.getHeight()-r-i-1][c] + I[r-i][c]) ;
348  }
349  return result+filter[0]*I[r][c];
350  }
351 
352  static inline double filterYTopBorder(const vpImage<double> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
353  {
354  double result;
355 
356  result = 0;
357 
358  for(unsigned int i=1; i<=(size-1)/2; i++)
359  {
360  if(r>i)
361  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
362  else
363  result += filter[i]*(I[r+i][c] + I[i-r][c]) ;
364  }
365  return result+filter[0]*I[r][c];
366  }
367 
368  static inline double filterYBottomBorder(const vpImage<double> &I, unsigned int r, unsigned int c, const double *filter,unsigned int size)
369  {
370  double result;
371 
372  result = 0;
373 
374  for(unsigned int i=1; i<=(size-1)/2; i++)
375  {
376  if(r+i<I.getHeight())
377  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
378  else
379  result += filter[i]*(I[2*I.getHeight()-r-i-1][c] + I[r-i][c]) ;
380  }
381  return result+filter[0]*I[r][c];
382  }
383 
384  static inline double filterY(const vpImage<double> &I,
385  unsigned int r, unsigned int c,
386  const double *filter,unsigned int size)
387  {
388  double result;
389 
390  result = 0;
391 
392  for(unsigned int i=1; i<=(size-1)/2; i++)
393  {
394  result += filter[i]*(I[r+i][c] + I[r-i][c]) ;
395  }
396  return result+filter[0]*I[r][c];
397  }
398 
399  static void gaussianBlur(const vpImage<unsigned char> &I, vpImage<double>& GI, unsigned int size=7, double sigma=0., bool normalize=true);
407  template<class T>
408  static double gaussianFilter(const vpImage<T> & fr,
409  const unsigned int r, const unsigned int c)
410  {
411  //filter Gaussien
412  return (
413  15.0 * fr[r][c]
414  + 12.0 * ( fr[r-1][c] + fr[r][c-1] + fr[r+1][c] + fr[r][c+1] )
415  + 9.0 * ( fr[r-1][c-1] + fr[r+1][c-1] + fr[r-1][c+1] + fr[r+1][c+1])
416  + 5.0 * ( fr[r-2][c] + fr[r][c-2] + fr[r+2][c] + fr[r][c+2] )
417  + 4.0 * ( fr[r-2][c+1] + fr[r-2][c-1] + fr[r-1][c-2] + fr[r+1][c-2] +
418  fr[r+2][c-1] + fr[r+2][c+1] + fr[r-1][c+2] + fr[r+1][c+2] )
419  + 2.0 * ( fr[r-2][c-2] + fr[r+2][c-2] + fr[r-2][c+2] + fr[r+2][c+2] )
420  )
421  /159.0;
422  }
423  //operation pour pyramide gaussienne
424  static void getGaussPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
425  static void getGaussXPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
426  static void getGaussYPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
427 
428  static void getGaussianKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true);
429  static void getGaussianDerivativeKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true);
430 
431  //fonction renvoyant le gradient en X de l'image I pour traitement pyramidal => dimension /2
432  static void getGradX(const vpImage<unsigned char> &I, vpImage<double>& dIx);
433  static void getGradX(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter, unsigned int size);
434  static void getGradX(const vpImage<double> &I, vpImage<double>& dIx, const double *filter, unsigned int size);
435  static void getGradXGauss2D(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *gaussianKernel,
436  const double *gaussianDerivativeKernel, unsigned int size);
437 
438  //fonction renvoyant le gradient en Y de l'image I
439  static void getGradY(const vpImage<unsigned char> &I, vpImage<double>& dIy);
440  static void getGradY(const vpImage<unsigned char> &I, vpImage<double>& dIy, const double *filter, unsigned int size);
441  static void getGradY(const vpImage<double> &I, vpImage<double>& dIy, const double *filter, unsigned int size);
442  static void getGradYGauss2D(const vpImage<unsigned char> &I, vpImage<double>& dIy, const double *gaussianKernel,
443  const double *gaussianDerivativeKernel,unsigned int size);
444 
445 } ;
446 
447 
448 #endif
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
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:159
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:150
static double derivativeFilterX(const vpImage< T > &I, const unsigned int r, const unsigned int c)
Definition: vpImageFilter.h:91
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:71