MATLAB Function Reference Previous page   Next Page
conv2

Two-dimensional convolution

Syntax

Description

C = conv2(A,B) computes the two-dimensional convolution of matrices A and B. If one of these matrices describes a two-dimensional finite impulse response (FIR) filter, the other matrix is filtered in two dimensions.

The size of C in each dimension is equal to the sum of the corresponding dimensions of the input matrices, minus one. That is, if the size of A is [ma,na] and the size of B is [mb,nb], then the size of C is [ma+mb-1,na+nb-1].

C = conv2(hcol,hrow,A) convolves A first with the vector hcol along the rows and then with the vector hrow along the columns. If hcol is a column vector and hrow is a row vector, this case is the same as C = conv2(hcol*hrow,A).

C = conv2(...,'shape') returns a subsection of the two-dimensional convolution, as specified by the shape parameter:

full
Returns the full two-dimensional convolution (default).
same
Returns the central part of the convolution of the same size as A.
valid
Returns only those parts of the convolution that are computed without the zero-padded edges. Using this option, C has size [ma-mb+1,na-nb+1] when all(size(A) >= size(B)). Otherwise conv2 returns [].

Algorithm

conv2 uses a straightforward formal implementation of the two-dimensional convolution equation in spatial form. If and are functions of two discrete variables, and , then the formula for the two-dimensional convolution of and is

In practice however, conv2 computes the convolution for finite intervals.

Note that matrix indices in MATLAB always start at 1 rather than 0. Therefore, matrix elements A(1,1), B(1,1), and C(1,1) correspond to mathematical quantities a(0,0), b(0,0), and c(0,0).

Examples

Example 1. For the 'same' case, conv2 returns the central part of the convolution. If there are an odd number of rows or columns, the "center" leaves one more at the beginning than the end.

This example first computes the convolution of A using the default ('full') shape, then computes the convolution using the 'same' shape. Note that the array returned using 'same' corresponds to the underlined elements of the array returned using the default shape.

Example 2. In image processing, the Sobel edge finding operation is a two-dimensional convolution of an input array with the special matrix

These commands extract the horizontal edges from a raised pedestal.

Transposing the filter s extracts the vertical edges of A.

This figure combines both horizontal and vertical edges.

See Also

conv, convn, filter2

xcorr2 in the Signal Processing Toolbox


Previous page  conv convhull Next page

© 1994-2005 The MathWorks, Inc.