conv2

Purpose

Two-dimensional convolution.

Synopsis

C = conv2(X,Y)
C = conv2(X,Y,'shape')

Description

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

The size in each dimension of the output matrix C is equal to the sum of the corresponding dimensions of the input matrices minus one. That is, if the size of X is[mx nx] and the size of Y is [my ny], then the size of C is [mx+my-1,nx+ny-1].

C = conv2(X,Y,'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 X.
  • valid returns only those parts of the convolution that are computed without the zero-padded edges. Using this option, C has size [mx-my+1,nx-ny+1] when size(X) > size(Y).
    conv2 executes most quickly when X is larger than Y.

    Examples

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

    s = [1 2 1; 0 0 0; -1 -2 -1];
    
    These commands extract the horizontal edges from a raised pedestal:

    A = zeros(10);
    A(3:7,3:7) = ones(5);
    h = conv2(A,s);
    mesh(h)
    
    These commands display first the vertical edges of A, then both horizontal and vertical edges.

    V = conv2(A,s');
    mesh(V)
    mesh(sqrt(h.^2+V.^2))
    

    See Also

    conv, deconv, filter2

    (c) Copyright 1994 by The MathWorks, Inc.