Filter Images Using imfilter with Convolution

This example shows how to perform image filtering using convolution with imfilter. By default, imfilter uses correlation because the toolbox filter design functions produce correlation kernels.

Create a sample matrix.

A = magic(5)
A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

Create a filter.

h = [-1 0 1];

Filter using correlation, the default.

imfilter(A,h)
ans =

    24   -16   -16    14    -8
     5   -16     9     9   -14
     6     9    14     9   -20
    12     9     9   -16   -21
    18    14   -16   -16    -2

Filter using convolution, using the parameter.

imfilter(A,h,'conv')
ans =

   -24    16    16   -14     8
    -5    16    -9    -9    14
    -6    -9   -14    -9    20
   -12    -9    -9    16    21
   -18   -14    16    16     2
Was this topic helpful?