Main Content

conv2

2-D convolution

Description

example

C= conv2(A,B)returns thetwo-dimensional convolution矩阵的AandB.

example

C= conv2(u,v,A)first convolves each column ofAwith the vectoru, and then it convolves each row of the result with the vectorv.

example

C= conv2(___,shape)returns a subsection of the convolution according toshape. For example,C = conv2(A,B,'same')returns the central part of the convolution, which is the same size asA.

Examples

collapse all

In applications such as image processing, it can be useful to compare the input of a convolution directly to the output. Theconv2function allows you to control the size of the output.

Create a 3-by-3 random matrixAand a 4-by-4 random matrixB. Compute the full convolution ofAandB, which is a 6-by-6 matrix.

A = rand(3); B = rand(4); Cfull = conv2(A,B)
Cfull =6×60.7861 1.2768 1.4581 1.0007 0.2876 0.0099 1.0024 1.8458 3.0844 2.5151 1.5196 0.2560 1.0561 1.9824 3.5790 3.9432 2.9708 0.7587 1.6790 2.0772 3.0052 3.7511 2.7593 1.5129 0.9902 1.1000 2.4492 1.6082 1.7976 1.2655 0.1215 0.1469 1.0409 0.5540 0.6941 0.6499

Compute the central part of the convolutionCsame, which is a submatrix ofCfullwith the same size asA.Csameis equal toCfull(3:5,3:5).

Csame = conv2(A,B,'same')
Csame =3×33.5790 3.9432 2.9708 3.0052 3.7511 2.7593 2.4492 1.6082 1.7976

The Sobel edge-finding operation uses a 2-D convolution to detect edges in images and other 2-D data.

Create and plot a 2-D pedestal with interior height equal to one.

A = zeros(10); A(3:7,3:7) = ones(5); mesh(A)

Figure contains an axes object. The axes object contains an object of type surface.

Convolve the rows ofAwith the vectoru, and then convolve the rows of the result with the vectorv. The convolution extracts the horizontal edges of the pedestal.

u = [1 0 -1]'; v = [1 2 1]; Ch = conv2(u,v,A); mesh(Ch)

Figure contains an axes object. The axes object contains an object of type surface.

To extract the vertical edges of the pedestal, reverse the order of convolution withuandv.

Cv = conv2(v,u,A); mesh(Cv)

Figure contains an axes object. The axes object contains an object of type surface.

Compute and plot the combined edges of the pedestal.

figure mesh(sqrt(Ch.^2 + Cv.^2))

Figure contains an axes object. The axes object contains an object of type surface.

Input Arguments

collapse all

Input array, specified as a vector or matrix.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Second input array, specified as a vector or a matrix to convolve withA. The arrayBdoes not have to be the same size asA.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Input vector, specified as a row or column vector.uconvolves with each column ofA.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Second input vector, specified as a row or column vector.vconvolves with each row of the convolution ofuwith the columns ofA.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Subsection of the convolution, specified as one of these values:

  • 'full'— Return the full 2-D convolution.

  • 'same'— Return the central part of the convolution, which is the same size asA.

  • 'valid'— Return only parts of the convolution that are computed without zero-padded edges.

Output Arguments

collapse all

2-D convolution, returned as a vector or matrix. WhenAandBare matrices, then the convolutionC = conv2(A,B)has sizesize(A)+size(B)-1. When[m,n] = size(A),p = length(u), andq = length(v), then the convolutionC = conv2(u,v,A)hasm+p-1rows andn+q-1columns.

When one or more input arguments toconv2are of typesingle, then the output is of typesingle. Otherwise,conv2converts inputs to typedoubleand returns typedouble.

Data Types:double|single

More About

collapse all

2-D Convolution

For discrete, two-dimensional variablesAandB, the following equation defines the convolution ofAandB:

C ( j , k ) = p q A ( p , q ) B ( j p + 1 , k q + 1 )

pandqrun over all values that lead to legal subscripts ofA(p,q)andB(j-p+1,k-q+1).

Extended Capabilities

C / C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a

See Also

|