The 2-D cross-correlation of an M-by-N matrix, X,
and a P-by-Q matrix, H,
is a matrix, C, of size M+P–1
by N+Q–1. Its elements
are given by
$$C(k,l)={\displaystyle \sum _{m=0}^{M-1}{\displaystyle \sum _{n=0}^{N-1}X(m,n)\text{\hspace{0.17em}}\overline{H}(m-k,n-l),}}\text{\hspace{1em}}\text{\hspace{1em}}\text{\hspace{1em}}\begin{array}{c}-(P-1)\le k\le M-1,\\ -(Q-1)\le l\le N-1,\end{array}$$
where the bar over H denotes complex conjugation.
The output matrix, C(k,l),
has negative and positive row and column indices.
A negative row index corresponds to an upward shift
of the rows of H.
A negative column index corresponds to a leftward
shift of the columns of H.
A positive row index corresponds to a downward shift
of the rows of H.
A positive column index corresponds to a rightward
shift of the columns of H.
To cast the indices in MATLAB form, add the size
of H: the element C(k,l)
corresponds to C(k+P,l+Q)
in the workspace.
For example, consider this 2-D cross-correlation:
C =
6 11 11 5
10 18 18 8
6 10 10 4
2 3 3 1
The C(1,1)
element in the output corresponds
to C(1–3,1–2) = C(–2,–1) in the defining
equation, which uses zero-based indexing. To compute the C(1,1)
element,
shift H
two rows up and one column to the left.
Accordingly, the only product in the cross-correlation sum is X(1,1)*H(3,2)
= 6
. Using the defining equation, you obtain
$$C(-2,-1)={\displaystyle \sum _{m=0}^{1}{\displaystyle \sum _{n=0}^{2}X(m,n)\text{\hspace{0.17em}}\overline{H}(m+2,n+1)}}=X(0,0)\text{\hspace{0.17em}}\overline{H}(2,1)=1\text{\hspace{0.17em}}\times \text{\hspace{0.17em}}6=6,$$
with all other terms
in the double sum equal to zero.