Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpImageFilter.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  * 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 public:
70 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
71  static void canny(const vpImage<unsigned char>& I,
73  const unsigned int gaussianFilterSize,
74  const double thresholdCanny,
75  const unsigned int apertureSobel);
76 #endif
77 
85  template<class T>
86  static double derivativeFilterX(const vpImage<T> &I,
87  const unsigned int r, const unsigned int c)
88  {
89  return (2047.0 *(I[r][c+1] - I[r][c-1])
90  +913.0 *(I[r][c+2] - I[r][c-2])
91  +112.0 *(I[r][c+3] - I[r][c-3]))/8418.0;
92  }
93 
101  template<class T>
102  static double derivativeFilterY(const vpImage<T> &I,
103  const unsigned int r, const unsigned int c)
104  {
105  return (2047.0 *(I[r+1][c] - I[r-1][c])
106  +913.0 *(I[r+2][c] - I[r-2][c])
107  +112.0 *(I[r+3][c] - I[r-3][c]))/8418.0;
108  }
109 
122  template<class T>
123  static double derivativeFilterX(const vpImage<T> &I,
124  const unsigned int r, const unsigned int c,
125  const double *filter, const unsigned int size)
126  {
127  unsigned int i;
128  double result;
129 
130  result = 0;
131 
132  for(i=1; i<=(size-1)/2; i++)
133  {
134  result += filter[i]*(I[r][c+i] - I[r][c-i]) ;
135  }
136  return result;
137  }
138 
139 
140 
152  template<class T>
153  static double derivativeFilterY(const vpImage<T> &I,
154  const unsigned int r, const unsigned int c,
155  const double *filter, const unsigned int size)
156  {
157  unsigned int i;
158  double result;
159 
160  result = 0;
161 
162  for(i=1; i<=(size-1)/2; i++)
163  {
164  result += filter[i]*(I[r+i][c] - I[r-i][c]) ;
165  }
166  return result;
167  }
168 
169  static void filter(const vpImage<double> &I,
170  vpImage<double>& Iu,
171  vpImage<double>& Iv,
172  const vpMatrix& M,
173  const bool convolve=false);
174 
175  static void filter(const vpImage<unsigned char> &I,
176  vpImage<double>& If,
177  const vpMatrix& M,
178  const bool convolve=false);
179 
180  static void sepFilter(const vpImage<unsigned char> &I,
181  vpImage<double>& If,
182  const vpColVector& kernelH,
183  const vpColVector& kernelV);
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);
400  static void gaussianBlur(const vpImage<double> &I, vpImage<double>& GI, unsigned int size=7, double sigma=0., bool normalize=true);
408  template<class T>
409  static double gaussianFilter(const vpImage<T> & fr,
410  const unsigned int r, const unsigned int c)
411  {
412  //filter Gaussien
413  return (
414  15.0 * fr[r][c]
415  + 12.0 * ( fr[r-1][c] + fr[r][c-1] + fr[r+1][c] + fr[r][c+1] )
416  + 9.0 * ( fr[r-1][c-1] + fr[r+1][c-1] + fr[r-1][c+1] + fr[r+1][c+1])
417  + 5.0 * ( fr[r-2][c] + fr[r][c-2] + fr[r+2][c] + fr[r][c+2] )
418  + 4.0 * ( fr[r-2][c+1] + fr[r-2][c-1] + fr[r-1][c-2] + fr[r+1][c-2] +
419  fr[r+2][c-1] + fr[r+2][c+1] + fr[r-1][c+2] + fr[r+1][c+2] )
420  + 2.0 * ( fr[r-2][c-2] + fr[r+2][c-2] + fr[r-2][c+2] + fr[r+2][c+2] )
421  )
422  /159.0;
423  }
424  //operation pour pyramide gaussienne
425  static void getGaussPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
426  static void getGaussXPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
427  static void getGaussYPyramidal(const vpImage<unsigned char> &I, vpImage<unsigned char>& GI);
428 
429  static void getGaussianKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true);
430  static void getGaussianDerivativeKernel(double *filter, unsigned int size, double sigma=0., bool normalize=true);
431 
432  //fonction renvoyant le gradient en X de l'image I pour traitement pyramidal => dimension /2
433  static void getGradX(const vpImage<unsigned char> &I, vpImage<double>& dIx);
434  static void getGradX(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *filter, unsigned int size);
435  static void getGradX(const vpImage<double> &I, vpImage<double>& dIx, const double *filter, unsigned int size);
436  static void getGradXGauss2D(const vpImage<unsigned char> &I, vpImage<double>& dIx, const double *gaussianKernel,
437  const double *gaussianDerivativeKernel, unsigned int size);
438 
439  //fonction renvoyant le gradient en Y de l'image I
440  static void getGradY(const vpImage<unsigned char> &I, vpImage<double>& dIy);
441  static void getGradY(const vpImage<unsigned char> &I, vpImage<double>& dIy, const double *filter, unsigned int size);
442  static void getGradY(const vpImage<double> &I, vpImage<double>& dIy, const double *filter, unsigned int size);
443  static void getGradYGauss2D(const vpImage<unsigned char> &I, vpImage<double>& dIy, const double *gaussianKernel,
444  const double *gaussianDerivativeKernel,unsigned int size);
445 };
446 
447 
448 #endif
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
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:226
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)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
static unsigned char filterGaussXPyramidal(const vpImage< unsigned char > &I, unsigned int i, unsigned int j)
unsigned int getHeight() const
Definition: vpImage.h:175
static double derivativeFilterX(const vpImage< T > &I, const unsigned int r, const unsigned int c)
Definition: vpImageFilter.h:86
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