CrossCorrelation2D - Maple Help

SignalProcessing

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

 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

${C}_{i,j}={\sum }_{r=1}^{a}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{\sum }_{s=1}^{b}{A}_{r,s}\stackrel{&conjugate0;}{{B}_{r-i+c,s-j+d}}$

 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).
 • As the underlying implementation of the SignalProcessing package is a module, it is also possible to use the form SignalProcessing:-CrossCorrelation2D to access the command from the package. For more information, see Module Members.

Examples

 > $\mathrm{with}\left(\mathrm{SignalProcessing}\right):$

Simple Examples

Example 1

 > $A≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[1,2\right]\right)$
 $\left[\begin{array}{rr}1& 2\end{array}\right]$ (1)
 > $B≔\mathrm{Matrix}\left(\left[\left[3,-4\right],\left[5I,6\right]\right]\right)$
 $\left[\begin{array}{cc}3& -4\\ 5{}I& 6\end{array}\right]$ (2)
 > $C≔\mathrm{CrossCorrelation2D}\left(A,B\right)$
 $\left[\begin{array}{ccc}5.999999999999999-1.0877919644084146{}{10}^{-15}{}I& 12.0-5.0{}I& 0.0-9.999999999999998{}I\\ -3.999999999999999-3.6259732146947156{}{10}^{-16}{}I& -4.999999999999999+0.0{}I& 5.999999999999999+0.0{}I\end{array}\right]$ (3)

Example 2

 > $A≔\mathrm{Matrix}\left(\left[\left[1,2\right],\left[3,4\right]\right]\right)$
 $\left[\begin{array}{rr}1& 2\\ 3& 4\end{array}\right]$ (4)
 > $B≔\mathrm{Matrix}\left(\left[\left[1,2,3\right],\left[4,5,6\right],\left[7,8,9\right]\right]\right)$
 $\left[\begin{array}{rrr}1& 2& 3\\ 4& 5& 6\\ 7& 8& 9\end{array}\right]$ (5)
 > $\mathrm{C1}≔\mathrm{CrossCorrelation2D}\left(A,B\right)$
 $\left[\begin{array}{cccc}9.0+0.0{}I& 26.0+0.0{}I& 23.0+0.0{}I& 14.0+0.0{}I\\ 33.0+0.0{}I& 77.0+0.0{}I& 67.0+0.0{}I& 36.0+0.0{}I\\ 21.0+0.0{}I& 47.0+0.0{}I& 37.0+0.0{}I& 18.0+0.0{}I\\ 9.0+0.0{}I& 18.0+0.0{}I& 11.0+0.0{}I& 4.0+0.0{}I\end{array}\right]$ (6)
 > $\mathrm{C2}≔\mathrm{CrossCorrelation2D}\left(B,A\right)$
 $\left[\begin{array}{cccc}4.0+0.0{}I& 11.0+0.0{}I& 18.0+0.0{}I& 9.0+0.0{}I\\ 18.0+0.0{}I& 37.0+0.0{}I& 47.0+0.0{}I& 21.0+0.0{}I\\ 36.0+0.0{}I& 67.0+0.0{}I& 77.0+0.0{}I& 33.0+0.0{}I\\ 14.0+0.0{}I& 23.0+0.0{}I& 26.0+0.0{}I& 9.0+0.0{}I\end{array}\right]$ (7)

Example 3

 > $A≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[I,5,3-I\right]\right)$
 $\left[\begin{array}{ccc}I& 5& 3-I\end{array}\right]$ (8)
 > $B≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[6-0.5I,7\right]\right)$
 $\left[\begin{array}{cc}6.-0.5{}I& 7\end{array}\right]$ (9)
 > $C≔\mathrm{CrossCorrelation2D}\left(A,B\right)$
 $\left[\begin{array}{cccc}0.0+7.0{}I& 34.5+6.0{}I& 51.0-4.5{}I& 18.5-4.5{}I\end{array}\right]$ (10)

Example 4

 > $A≔\mathrm{Matrix}\left(\left[\left[1,2+7I\right],\left[3-I,4\right]\right]\right)$
 $\left[\begin{array}{cc}1& 2+7{}I\\ 3-I& 4\end{array}\right]$ (11)
 > $B≔\mathrm{Array}\left(\left[\left[5,6\right],\left[7,8+3I\right]\right]\right)$
 $\left[\begin{array}{cc}5& 6\\ 7& 8+3{}I\end{array}\right]$ (12)
 > $C≔\mathrm{Matrix}\left(3,3,\mathrm{datatype}=\mathrm{complex}\left[8\right]\right)$
 $\left[\begin{array}{ccc}0.0+0.0{}I& 0.0+0.0{}I& 0.0+0.0{}I\\ 0.0+0.0{}I& 0.0+0.0{}I& 0.0+0.0{}I\\ 0.0+0.0{}I& 0.0+0.0{}I& 0.0+0.0{}I\end{array}\right]$ (13)
 > $\mathrm{CrossCorrelation2D}\left(A,B,\mathrm{container}=C\right):$
 > $'C'=C$
 ${C}{=}\left(\left[\begin{array}{ccc}8.000000000000004-2.999999999999995{}I& 43.999999999999986+50.0{}I& 14.000000000000004+49.0{}I\\ 27.0-16.999999999999993{}I& 69.99999999999999+23.0{}I& 38.0+34.999999999999986{}I\\ 18.0-5.999999999999995{}I& 38.999999999999986-4.999999999999997{}I& 20.0+7.105427357601002{}{10}^{-15}{}I\end{array}\right]\right)$ (14)

Direct Computation

 • To compute the cross-correlation directly from the definition, first take:
 > $X≔\mathrm{Matrix}\left(3,2,\left[\left[2,-2+I\right],\left[-I,2-I\right],\left[0,2-2I\right]\right]\right)$
 $\left[\begin{array}{cc}2& -2+I\\ -I& 2-I\\ 0& 2-2{}I\end{array}\right]$ (15)
 > $Y≔\mathrm{Matrix}\left(4,3,\left[\left[1+I,2+2I,-1-I\right],\left[-2-I,1-I,-2I\right],\left[2I,1+2I,1+I\right],\left[-1,-I,2-I\right]\right]\right)$
 $\left[\begin{array}{ccc}1+I& 2+2{}I& -1-I\\ -2-I& 1-I& -2{}I\\ 2{}I& 1+2{}I& 1+I\\ -1& -I& 2-I\end{array}\right]$ (16)
 > $a,b≔\mathrm{upperbound}\left(X\right)$
 ${a}{,}{b}{≔}{3}{,}{2}$ (17)
 > $c,d≔\mathrm{upperbound}\left(Y\right)$
 ${c}{,}{d}{≔}{4}{,}{3}$ (18)
 > $p≔a+c-1$
 ${p}{≔}{6}$ (19)
 > $q≔b+d-1$
 ${q}{≔}{4}$ (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:
 > $\mathrm{XX}≔\left(i,j\right)↦\mathrm{if}\left(i<1\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{or}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}a
 > $\mathrm{YY}≔\left(i,j\right)↦\mathrm{if}\left(i<1\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{or}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}c
 • 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:
 > $\mathrm{C2}≔\mathrm{CrossCorrelation2D}\left(X,Y\right):$
 > $\mathrm{err}≔\mathrm{max}\left(\mathrm{abs}\left(\mathrm{C1}-\mathrm{C2}\right)\right)$
 ${\mathrm{err}}{≔}{3.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:
 > $\mathrm{unassign}\left('a','b','c','d','p','q','A','B','C'\right)$
 > $a\left[1\right]≔60:$
 > $b\left[1\right]≔62:$
 > $A\left[1\right]≔\mathrm{LinearAlgebra}:-\mathrm{RandomMatrix}\left(a\left[1\right],b\left[1\right],\mathrm{datatype}=\mathrm{complex}\left[8\right]\right):$
 > $c≔65:$
 > $d≔60:$
 > $B≔\mathrm{LinearAlgebra}:-\mathrm{RandomMatrix}\left(c,d,\mathrm{datatype}=\mathrm{complex}\left[8\right]\right):$
 • Second, compute the cross-correlation with no padding:
 > $C\left[1\right]≔\mathrm{CrossCorrelation2D}\left(A\left[1\right],B\right):$
 • Now, the dimensions are just a little smaller than powers of 2:
 > $p\left[1\right]≔a\left[1\right]+c-1$
 ${{p}}_{{1}}{≔}{124}$ (22)
 > $q\left[1\right]≔b\left[1\right]+d-1$
 ${{q}}_{{1}}{≔}{121}$ (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:
 > $a\left[2\right]≔a\left[1\right]+{2}^{-\mathrm{ilog2}\left(\frac{1}{\mathrm{max}\left(4,p\left[1\right]\right)}\right)}-p\left[1\right]$
 ${{a}}_{{2}}{≔}{64}$ (24)
 > $b\left[2\right]≔b\left[1\right]+{2}^{-\mathrm{ilog2}\left(\frac{1}{\mathrm{max}\left(4,q\left[1\right]\right)}\right)}-q\left[1\right]$
 ${{b}}_{{2}}{≔}{69}$ (25)
 > $p\left[2\right]≔a\left[2\right]+c-1$
 ${{p}}_{{2}}{≔}{128}$ (26)
 > $q\left[2\right]≔b\left[2\right]+d-1$
 ${{q}}_{{2}}{≔}{128}$ (27)
 > $A\left[2\right]≔\mathrm{ArrayTools}:-\mathrm{Alias}\left(A\left[1\right]\right):$
 > $A\left[2\right]\left(a\left[1\right]+1..a\left[2\right],..\right)≔0:$
 > $A\left[2\right]\left(..,b\left[1\right]+1..b\left[2\right]\right)≔0:$
 • Finally, find the cross-correlation using FFTs, and extract the appropriate submatrix:
 > $C\left[2\right]≔\mathrm{CrossCorrelation2D}\left(A\left[2\right],B\right)\left[..p\left[1\right],..q\left[1\right]\right]:$
 • Both results are the same:
 > $\mathrm{err}≔\mathrm{max}\left(\mathrm{abs}\left(C\left[1\right]-C\left[2\right]\right)\right)$
 ${\mathrm{err}}{≔}{8.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:
 > $A≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[5,-2,3,4,1\right]\right)$
 $\left[\begin{array}{rrrrr}5& -2& 3& 4& 1\end{array}\right]$ (29)
 > $B≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[7,0,2\right]\right)$
 $\left[\begin{array}{rrr}7& 0& 2\end{array}\right]$ (30)
 > $b≔\mathrm{numelems}\left(B\right)$
 ${b}{≔}{3}$ (31)
 • The 2-D cross-correlation returns the following:
 > $\mathrm{C__2D}≔\mathrm{CrossCorrelation2D}\left(A,B\right)$
 $\left[\begin{array}{ccccccc}9.999999999999998-3.3569988834012605{}{10}^{-16}{}I& -4.000000000000003+0.0{}I& 41.0+0.0{}I& -5.999999999999999+0.0{}I& 22.999999999999993+0.0{}I& 27.999999999999996+0.0{}I& 7.000000000000003+0.0{}I\end{array}\right]$ (32)
 • The 1-D cross-correlation, however, gives:
 > $\mathrm{CrossCorrelation}\left(A,B\right)$
 $\left[\begin{array}{ccccccc}41.0& -4.0& 10.0& 0.0& 0.0& 0.0& 0.0\end{array}\right]$ (33)
 • If we switch the arguments for CrossCorrelation, and choose the appropriate value of lowerlag, the two cross-correlations agree:
 > $\mathrm{C__1D}≔\mathrm{CrossCorrelation}\left(B,A,1-b\right)$
 $\left[\begin{array}{ccccccc}10.0& -4.0& 41.0& -6.0& 23.0& 28.0& 7.0\end{array}\right]$ (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:
 > $\mathrm{with}\left(\mathrm{ImageTools}\right):$
 > $\mathrm{imgfile}≔\mathrm{cat}\left(\mathrm{kernelopts}\left(\mathrm{mapledir}\right),\mathrm{kernelopts}\left(\mathrm{dirsep}\right),\mathrm{data},\mathrm{kernelopts}\left(\mathrm{dirsep}\right),"images",\mathrm{kernelopts}\left(\mathrm{dirsep}\right),"fingerprint.jpg"\right):$
 > $\mathrm{img1}≔\mathrm{Matrix}\left(\mathrm{Read}\left(\mathrm{imgfile}\right),\mathrm{datatype}=\mathrm{float}\left[8\right]\right):$
 > $\mathrm{Preview}\left(\mathrm{img1}\right)$
 • First, determine the dimensions:
 > $a,b≔\mathrm{upperbound}\left(\mathrm{img1}\right)$
 ${a}{,}{b}{≔}{240}{,}{256}$ (35)
 • We will also use the mean later:
 > $\mathrm{\mu }≔\frac{\mathrm{add}\left(\mathrm{img1}\right)}{\mathrm{numelems}\left(\mathrm{img1}\right)}:$
 • 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):
 > $\mathrm{cL}≔85:$
 > $\mathrm{cU}≔205:$
 > $\mathrm{dL}≔120:$
 > $\mathrm{dU}≔180:$
 > $\mathrm{img2}≔\mathrm{Matrix}\left(\mathrm{img1}\left[\mathrm{cL}..\mathrm{cU},\mathrm{dL}..\mathrm{dU}\right]+0.5\mathrm{\mu }\mathrm{ArrayTools}:-\mathrm{RandomArray}\left(\mathrm{cU}-\mathrm{cL}+1,\mathrm{dU}-\mathrm{dL}+1,\mathrm{distribution}=\mathrm{normal}\right),\mathrm{datatype}=\mathrm{float}\left[8\right]\right):$
 > $\mathrm{Preview}\left(\mathrm{img2}\right)$
 • 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):
 > $\mathrm{\mu }≔\frac{\mathrm{add}\left(\mathrm{img1}\right)}{\mathrm{numelems}\left(\mathrm{img1}\right)}:$
 > $C≔\mathrm{Matrix}\left(\mathrm{\Re }\left(\mathrm{CrossCorrelation2D}\left(\mathrm{~}\left[\mathrm{-}\right]\left(\mathrm{img1},\mathrm{ },\mathrm{\mu }\right),\mathrm{~}\left[\mathrm{-}\right]\left(\mathrm{img2},\mathrm{ },\mathrm{\mu }\right)\right)\right),\mathrm{datatype}=\mathrm{sfloat}\right):$
 • The maximum correlation occurs here, which, as expected, coincides with the bottom-right corner of the fragment:
 > $\mathrm{\alpha },\mathrm{\beta }≔\mathrm{max}\left[\mathrm{index}\right]\left(C\right)$
 ${\mathrm{\alpha }}{,}{\mathrm{\beta }}{≔}{205}{,}{180}$ (36)
 • Visually:
 > $\mathrm{matplot}≔\mathrm{plots}:-\mathrm{matrixplot}\left(C\right):$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{matplot},\mathrm{title}="Cross-Correlation - Full"\right)$
 > $r≔25:$
 > $\mathrm{box}≔\left[\mathrm{\alpha }-r..\mathrm{\alpha }+r,\mathrm{\beta }-r..\mathrm{\beta }+r,\mathrm{min}\left(C\right)..\mathrm{max}\left(C\right)\right]:$
 > $\mathrm{plots}:-\mathrm{display}\left(\mathrm{matplot},\mathrm{view}=\mathrm{box},\mathrm{title}="Cross-Correlation - Zoomed"\right)$
 > 

Compatibility

 • The SignalProcessing[CrossCorrelation2D] command was introduced in Maple 2020.
 • For more information on Maple 2020 changes, see Updates in Maple 2020.