Image Processing Toolbox | ![]() ![]() |
Perform two-dimensional convolution
Syntax
C = conv2(A,B)
C = conv2(hcol,hrow,A)
C = conv2(...,shape
)
Description
C = conv2(A,B)
performs the two-dimensional convolution of matrices A
and B
, returning the result in the output matrix C
. The size in each dimension of C
is equal to the sum of the corresponding dimensions of the input matrices minus one. That is, if the size of A
is [ma,mb]
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
separably with hcol
in the column
direction and hrow
in the row direction. hcol
and hrow
are both vectors.
C = conv2(...,
shape
)
returns a subsection of the two-dimensional convolution, as specified by the shape
parameter. shape
is a string with one of these values:
'full'
(the default) returns the full two-dimensional convolution.'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, size(C) = [ma-mb+1,
na-nb+1]
when size(A)
> size(B)
.For image filtering, A
should be the image matrix and B
should be the filter (convolution kernel) if the shape
parameter is 'same'
or 'valid'
. If the shape
parameter is 'full'
, the order does not matter, because full convolution is commutative.
Class Support
All vector and matrix inputs to conv2
can be of class double
or of any integer class. The output matrix C
is of class double
.
Remarks
conv2
is a function in MATLAB.
Example
A = magic(5) A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 B = [1 2 1;0 2 0;3 1 3] B = 1 2 1 0 2 0 3 1 3 C = conv2(A,B) C = 17 58 66 34 32 38 15 23 85 88 35 67 76 16 55 149 117 163 159 135 67 79 78 160 161 187 129 51 23 82 153 199 205 108 75 30 68 135 168 91 84 9 33 65 126 85 104 15 27
See Also
xcorr
, xcorr2
in the Signal Processing Toolbox User's Guide
conv
, deconv
in the MATLAB Function Reference
![]() | colorbar | convmtx2 | ![]() |