conv2
2-D convolution
Description
returns thetwo-dimensional convolution矩阵的C
= conv2(A
,B
)A
andB
.
Examples
2-D Convolution
In applications such as image processing, it can be useful to compare the input of a convolution directly to the output. Theconv2
function allows you to control the size of the output.
Create a 3-by-3 random matrixA
and a 4-by-4 random matrixB
. Compute the full convolution ofA
andB
, 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 ofCfull
with the same size asA
.Csame
is 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
Extract 2-D Pedestal Edges
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)
Convolve the rows ofA
with 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)
To extract the vertical edges of the pedestal, reverse the order of convolution withu
andv
.
Cv = conv2(v,u,A); mesh(Cv)
Compute and plot the combined edges of the pedestal.
figure mesh(sqrt(Ch.^2 + Cv.^2))
Input Arguments
A
—Input array
vector|matrix
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
B
—Second input array
vector|matrix
Second input array, specified as a vector or a matrix to convolve withA
. The arrayB
does 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
u
—Input vector
row or column vector
Input vector, specified as a row or column vector.u
convolves with each column ofA
.
Data Types:double
|single
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
Complex Number Support:Yes
v
—Second input vector
row or column vector
Second input vector, specified as a row or column vector.v
convolves with each row of the convolution ofu
with the columns ofA
.
Data Types:double
|single
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|logical
Complex Number Support:Yes
shape
—Subsection of convolution
'full'
(default) |'same'
|'valid'
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
C
——二维卷积
vector | matrix
2-D convolution, returned as a vector or matrix. WhenA
andB
are 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-1
rows andn+q-1
columns.
When one or more input arguments toconv2
are of typesingle
, then the output is of typesingle
. Otherwise,conv2
converts inputs to typedouble
and returns typedouble
.
Data Types:double
|single
More About
2-D Convolution
For discrete, two-dimensional variablesAandB, the following equation defines the convolution ofAandB:
pandqrun over all values that lead to legal subscripts ofA(p,q)andB(j-p+1,k-q+1).
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
使用笔记和限制:
If
shape
is'full'
(default), then the inputsA
andB
must not be empty and only one them can be a tall array.If
shape
is'same'
or'valid'
, thenB
cannot be a tall array.u
andv
cannot be tall arrays.
For more information, seeTall Arrays.
C / C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Thread-Based Environment
Run code in the background using MATLAB®backgroundPool
or accelerate code with Parallel Computing Toolbox™ThreadPool
.
This function fully supports thread-based environments. For more information, seeRun MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
使用笔记和限制:
Input vectors
u
andv
must not be distributed arrays.
For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(英语)
- United States(英语)
Europe
- 比利时(英语)
- Denmark(英语)
- Deutschland(Deutsch)
- España(Español)
- Finland(英语)
- France(Français)
- Ireland(英语)
- Italia(Italiano)
- Luxembourg(英语)
- Netherlands(英语)
- Norway(英语)
- Österreich(Deutsch)
- Portugal(英语)
- Sweden(英语)
- Switzerland
- United Kingdom(英语)