conv2

Two-dimensional convolution of two matrices.

Category: Mathematics > Fourier Analysis and Filtering

Syntax

C = conv2(A, B)

Returns the full convolution, of size size(A)+size(B)-1.

C = conv2(A, B, shape)

Returns the part selected by shape: 'full', 'same' or 'valid'.

C = conv2(u, v, A)

Convolves the columns of A with the vector u and then the rows with the vector v.

Description

Each output element is a sum of products of A with the kernel B rotated by 180 degrees, with A treated as zero outside its edges. Convolution is the operation behind blurring, edge detection and other image filters.

The three-argument form with two vectors is a separable filter: it gives the same result as convolving with the outer product u*v.', but costs fewer operations.

Input arguments

A, B
Numeric matrices. B is usually the smaller kernel.
shape
'full' (default) returns every point; 'same' returns the central part with the size of A; 'valid' returns only points computed without zero padding.

Output arguments

C
Convolution result.

Example

Full, same and valid

A = [1 2 3; 4 5 6; 7 8 9];
conv2([1 2; 3 4], ones(2))
conv2(A, [0 1 0; 1 1 1; 0 1 0], 'same')
conv2(A, [1 0; 0 -1], 'valid')
Output
ans = 3×3

     1     3     2
     4    10     6
     3     7     4
ans = 3×3

     7    11    11
    17    25    23
    19    29    23
ans = 2×2

     4     4
     4     4

See also

conv filter2 convn filter