CrossCorrelation2D - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


SignalProcessing

  

CrossCorrelation2D

  

calculate the cross-correlation between a pair of one- or two-dimensional rtables

 

Calling Sequence

Parameters

Description

Examples

Compatibility

Calling Sequence

CrossCorrelation2D( A, B, options )

Parameters

A, B

-

two one- or two-dimensional rtables

container

-

(optional) container to compute and store the cross-correlation

Description

• 

The CrossCorrelation2D command takes two 1-D or 2-D rtables and computes their 2-D cross-correlation.

• 

The inputs are converted to Matrices of complex[8] datatype, and an error will be thrown if this is not possible. For this reason, it is most efficient for the inputs to already be Matrices having datatype complex[8].

• 

The cross-correlation is computed via Discrete or Fast Fourier Transforms (DFTs or FFTs), but the result is equivalent to the standard definition (see example below). However, there may be numerical artifacts in the result that are small in size.

• 

Suppose Matrices A and B, respectively, have dimensions (a,b) and (c,d), and define p=a+c-1 and q=b+d-1. Then, the cross-correlation Matrix of dimension (p,q) is given by the formula

Ci,j=r=1as=1bAr,sBri+c,sj+d&conjugate0;

  

where A is assumed to be zero outside of the range of indices (1..a,1..b), and B is assumed to be zero outside of the range of indices (1..c,1..d).

• 

FFTs are used when p and q, where p and q are as in the definition above, are powers of 2 and no less than 4 in value. Consequently, if p and q are both a little smaller than a power of 2, the user may want to pad the original Matrices with zeros. See below for an example.

• 

If the container=C option is provided, then the results are stored in C. The container must have dimensions (p,q), and datatype complex[8].

• 

If A is empty with no elements, a and b are both taken to be zero. Similarly, c and d are both taken to be zero if B has no elements. If both A and B are empty, an error will be thrown because the cross-correlation would have no meaning. However, if, say, B is empty but A is not, then the cross-correlation would be a zero Matrix of size (a-1,b-1).

Examples

withSignalProcessing:

Simple Examples

Example 1

AVectorrow1,2

A

(1)

BMatrix3,4,5I,6

B

(2)

CCrossCorrelation2DA,B

C

(3)

Example 2

AMatrix1,2,3,4

A

(4)

BMatrix1,2,3,4,5,6,7,8,9

B

(5)

C1CrossCorrelation2DA,B

C1

(6)

C2CrossCorrelation2DB,A

C2

(7)

Example 3

AVectorrowI,5,3I

A

(8)

BVectorrow60.5I,7

B

(9)

CCrossCorrelation2DA,B

C

(10)

Example 4

AMatrix1,2+7I,3I,4

A

(11)

BArray5,6,7,8+3I

B

(12)

CMatrix3,3,datatype=complex8

C

(13)

CrossCorrelation2DA,B,container=C:

C=C

C=

(14)

Direct Computation

• 

To compute the cross-correlation directly from the definition, first take:

XMatrix3,2,2,2+I,I,2I,0,22I

X

(15)

YMatrix4,3,1+I,2+2I,1I,2I,1I,2I,2I,1+2I,1+I,1,I,2I

Y

(16)

a,bupperboundX

a,b3,2

(17)

c,dupperboundY

c,d4,3

(18)

pa+c1

p6

(19)

qb+d1

q4

(20)
• 

We will need to assume X and Y are both 0 when their indices are out of bounds, and take the complex conjugate of the Y values:

XXi&comma;j`if`i<1ora<iorj<1orb<j&comma;0&comma;Xi,j&colon;

YYi&comma;j`if`i<1orc<iorj<1ord<j&comma;0&comma;conjugateYi,j&colon;

• 

The cross-correlation can now be found directly:

C1 := Matrix( p, q, proc(i,j) local r, s; add( add( XX(r,s) * YY(r-i+c,s-j+d), s = 1 .. b ), r = 1 .. a ); end proc ):

• 

This corresponds to that found with the command:

C2CrossCorrelation2DX&comma;Y&colon;

errmaxabsC1C2

err3.55271367880050×10−15

(21)

Computation with Fast Fourier Transforms (FFTs)

• 

The cross-correlation is computed with FFTs when the dimensions of the final Matrix, namely p and q above, are both powers of 2 and no smaller than 4 in size, and DFTs in the remainder of cases. In the following example, we will compute a cross-correlation both directly (which will use DFTs) and by padding one of the input Matrices (which will use FFTs). The indices 1 and 2 will be used to distinguish the original and padded versions of quantities.

• 

First, define the original Matrices:

unassigna&comma;b&comma;c&comma;d&comma;p&comma;q&comma;A&comma;B&comma;C

a160&colon;

b162&colon;

A1LinearAlgebra:-RandomMatrixa1&comma;b1&comma;datatype=complex8&colon;

c65&colon;

d60&colon;

BLinearAlgebra:-RandomMatrixc&comma;d&comma;datatype=complex8&colon;

• 

Second, compute the cross-correlation with no padding:

C1CrossCorrelation2DA1&comma;B&colon;

• 

Now, the dimensions are just a little smaller than powers of 2:

p1a1+c1

p1124

(22)

q1b1+d1

q1121

(23)
• 

So, we will pad A[1] (padding B would also work) so that the computed cross-correlation Matrix has dimensions that are powers of 2:

a2a1+2ilog21max4&comma;p1p1

a264

(24)

b2b1+2ilog21max4&comma;q1q1

b269

(25)

p2a2+c1

p2128

(26)

q2b2+d1

q2128

(27)

A2ArrayTools:-AliasA1&colon;

A2a1+1..a2&comma;..0&colon;

A2..&comma;b1+1..b20&colon;

• 

Finally, find the cross-correlation using FFTs, and extract the appropriate submatrix:

C2CrossCorrelation2DA2&comma;B..p1,..q1&colon;

• 

Both results are the same:

errmaxabsC1C2

err8.16983452975878×10−10

(28)

Comparison with 1-D CrossCorrelation

• 

The CrossCorrelation2D command can be reduced to the 1-D version with a couple of minor modifications. Take, for example, the following Vectors:

AVectorrow5&comma;2&comma;3&comma;4&comma;1

A

(29)

BVectorrow7&comma;0&comma;2

B

(30)

bnumelemsB

b3

(31)
• 

The 2-D cross-correlation returns the following:

C__2DCrossCorrelation2DA&comma;B

C__2D

(32)
• 

The 1-D cross-correlation, however, gives:

CrossCorrelationA&comma;B

(33)
• 

If we switch the arguments for CrossCorrelation, and choose the appropriate value of lowerlag, the two cross-correlations agree:

C__1DCrossCorrelationB&comma;A&comma;1b

C__1D

(34)

Application to Fingerprint Matching

• 

A classic application of two-dimensional cross-correlation is the lining up of two or more images, for example, images of fingerprint fragments. Consider the following image:

withImageTools&colon;

imgfilecatkerneloptsmapledir&comma;kerneloptsdirsep&comma;data&comma;kerneloptsdirsep&comma;images&comma;kerneloptsdirsep&comma;fingerprint.jpg&colon;

img1MatrixReadimgfile&comma;datatype=float8&colon;

Previewimg1

• 

First, determine the dimensions:

a,bupperboundimg1

a,b240,256

(35)
• 

We will also use the mean later:

μaddimg1numelemsimg1&colon;

• 

Second, let's choose some bounds for a fragment of the fingerprint, extract the subimage (fingerprint fragment), and then add some noise (to make our task a little more realistic):

cL85&colon;

cU205&colon;

dL120&colon;

dU180&colon;

img2Matriximg1cL..cU,dL..dU+0.5μArrayTools:-RandomArraycUcL+1&comma;dUdL+1&comma;distribution=normal&comma;datatype=float8&colon;

Previewimg2

• 

Suppose now that we do not know whether the fragment is part of the larger image, and wish to determine if the fragment is a "match". To do this, compute the cross-correlation of the tempered data (formed by subtracting the mean from all elements):

μaddimg1numelemsimg1&colon;

CRealPartCrossCorrelation2D`~``-`img1&comma;` $`&comma;μ&comma;`~``-`img2&comma;` $`&comma;μ&colon;

• 

The maximum correlation occurs here, which, as expected, coincides with the bottom-right corner of the fragment:

α,βmaxindexC

α,β205,180

(36)
• 

Visually:

plots:-surfdataC&comma;title=Cross-Correlation - Full

r25&colon;

plots:-surfdataCαr..α+r,βr..β+r&comma;title=Cross-Correlation - Zoomed

Compatibility

• 

The SignalProcessing[CrossCorrelation2D] command was introduced in Maple 2020.

• 

For more information on Maple 2020 changes, see Updates in Maple 2020.

See Also

ArrayTools

ImageTools

plots

SignalProcessing