Engee documentation

conv2

Page in progress.

Two-dimensional convolution.

Library

EngeeDSP

Syntax

Function call

  • C = conv2(A,B) — returns two-dimensional convolution of matrices A and B.

    • If A "the matrix, eh B — vector-string (or A — vector is a string, and B — matrix), then C — this is the convolution of each row of the matrix with a vector.

    • If A "the matrix, eh B — column vector (or A — column vector, and B — matrix), then C — this is the convolution of each column of the matrix with a vector.

  • C = conv2(u,v,A) — first collapses each column of the matrix A with a vector u and then each row of the result with a vector v. This behavior applies regardless of whether u or v a row vector or a column vector.

  • C = conv2(___,shape) — returns a part of the convolution in accordance with the specified shape. For example, C = conv2(A,B,"same") returns the central part of the convolution, which has the same size as A.

Arguments

Input arguments

# A — input array

+ vector | the matrix

Details

An input array specified as a vector or matrix.

Data types

Float64, Float32, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool

Support for complex numbers

Yes

# B is the second input array

+ vector | the matrix

Details

The second input array, set as a vector or matrix for convolution with the array A. Array B it may not be the same size as A.

Data types

Float64, Float32, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool

Support for complex numbers

Yes

# u is the input vector

+ vector string | column vector

Details

The input vector, specified as a row vector or column vector. u performs convolution with each column of the matrix A.

Data types

Float64, Float32, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool

Support for complex numbers

Yes

# v is the second input vector

+ vector string | column vector

Details

The second input vector, specified as a row vector or column vector. v performs convolution with each row of the convolution vector u with matrix columns A.

Data types

Float64, Float32, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Bool

Support for complex numbers

Yes

# shape — the section of the convolution

+ "full" (by default) | "same" | "valid"

Details

A section of the convolution defined by one of the following values:

  • "full" — returns a complete two-dimensional convolution.

  • "same" — returns the central part of the convolution, the same size as the original matrix A.

  • "valid" — returns only the part of the convolution that is calculated without padding with zeros.

Output arguments

# C — two- dimensional convolution

+ vector | the matrix

Details

A two-dimensional convolution returned as a vector or matrix. If A and B — matrices, then convolution C = conv2(A,B) It has a size size(A)+size(B)-1. If m,n = size(A), p = length(u) and q = length(v), then the convolution C = conv2(u,v,A) has m+p-1 lines and n+q-1 columns.

If there are one or more input arguments to the function conv2 have a type Float32, then the output argument will be of type Float32. Otherwise conv2 converts the input data to a type Float64 and returns the type Float64.

Data types

Float64, Float32

Examples

Two-dimensional convolution

Details

It can be useful to compare the input data of a convolution directly with the output data. Function conv2 allows you to control the size of the output data.

Let’s create random matrices A size and B size . Calculate the complete convolution A and B, which is a matrix of size .

import EngeeDSP.Functions: conv2

A = rand(3, 3)
B = rand(4, 4)
Cfull = conv2(A, B)
6×6 Matrix{Float64}:
 0.192995   0.882682  0.775044  0.405206  0.103599  0.0162149
 0.097371   1.17042   1.53107   1.55316   0.520306  0.126147
 0.441852   1.15874   1.98734   3.1404    1.30511   0.484233
 0.27297    0.973765  1.87607   2.67335   1.76588   1.15007
 0.245543   0.433698  1.24257   1.63366   1.89067   1.4068
 0.0713151  0.203243  0.630603  0.901696  1.01698   0.577671

Calculate the central part of the convolution Csame, which is a submatrix of Cfull the same size as A. Csame equal to Cfull[3:5, 3:5].

Csame = conv2(A, B, "same")
3×3 Matrix{Float64}:
 1.98734   3.1404    1.30511
 1.87607   2.67335   1.76588
 1.24257   1.63366   1.89067

Extracting the edges of a two-dimensional pedestal

Details

Sobel’s edge search algorithm uses two-dimensional convolution to detect edges in images and other two-dimensional data.

Let’s create and build a two-dimensional pedestal with an internal height equal to one.

A = zeros(10, 10)
A[3:7, 3:7] .= 1.0
surface(A)

conv2 pedestal

Let’s perform the convolution of the rows of the matrix A with a vector u, and then convolution of the result string with the vector v. The convolution will extract the horizontal edges of the pedestal.

import EngeeDSP.Functions: conv2

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

conv2 pedestal 1

To extract the vertical edges of the pedestal, reverse the convolution order from u and v on the way back.

Cv = conv2(v, u, A)
surface(Cv)

conv2 pedestal 2

Let’s calculate and graph the combined edges of the pedestal.

surface(sqrt.(Ch.^2 .+ Cv.^2))

conv2 pedestal 3

Additional Info

Two-dimensional convolution

Details

For discrete two-dimensional matrices and the following equation defines the convolution and :

and all values that lead to valid indexes are run through. and .

Using this definition, the function conv2 calculates a direct convolution of two matrices, not an FFT-based convolution.