filter2

Two-dimensional correlation filter of a matrix with a kernel.

Category: Mathematics > Fourier Analysis and Filtering

Syntax

Y = filter2(B, X)

Filters X with kernel B and returns the central part, the same size as X.

Y = filter2(B, X, shape)

Returns the 'same' (default), 'full' or 'valid' part.

Description

filter2(B, X) is correlation: the kernel slides over X without being flipped. It is computed as conv2(X, rot90(B, 2), shape). For symmetric kernels the two operations coincide.

Values outside the edges of X are taken as zero.

Input arguments

B
Filter kernel, a matrix.
X
Data to filter.
shape
Part of the result to return, as in conv2.

Output arguments

Y
Filtered matrix.

Example

Sum of 2-by-2 neighborhoods

M = magic(3)
filter2(ones(2), M)
filter2(ones(2), M, 'valid')
Output
M = 3×3

     8     1     6
     3     5     7
     4     9     2
ans = 3×3

    17    19    13
    21    23     9
    13    11     2
ans = 2×2

    17    19
    21    23

See also

conv2 filter fft2