Skip to Main Content Skip to Search
Product Documentation

xcorr2 - 2–D cross-correlation

Syntax

C=xcorr2(A,B)
xcorr2(A)

Description

C = xcorr2(A,B) returns the cross-correlation of matrices A and B with no scaling. xcorr2 is the two-dimensional version of xcorr. It has its maximum value when the two matrices are aligned so that they are shaped as similarly as possible.

If matrix A has dimensions (Ma, Na) and matrix B has dimensions (Mb, Nb), The equation for the two-dimensional discrete cross-correlation is

where and .

xcorr2(A) is the autocorrelation matrix of input matrix A. It is identical to xcorr2(A,A).

Examples

Output Matrix Size

If matrix I1 has dimensions (4,3) and matrix I2 has dimensions (2,2), the following equations determine the number of rows and columns of the output matrix:

The resulting matrix is

Computing a Specific Element

In cross-correlation, the value of an output element is computed as a weighted sum of neighboring elements. For example, suppose the first input matrix represents an image and is defined as

I1 = [17  24   1   8  15
      23   5   7  14  16
       4   6  13  20  22
      10  12  19  21   3
      11  18  25   2   9]

The second input matrix also represents an image and is defined as

I2 = [8   1   6
      3   5   7
      4   9   2]

The following figure shows how to compute the (2,4) output element (zero-based indexing) using these steps:

  1. Slide the center element of I2 so that lies on top of the (1,3) element of I1.

  2. Multiply each weight in I2 by the element of I1 underneath.

  3. Sum the individual products from step 2.

The (2,4) output element from the cross-correlation is

The normalized cross-correlation of the (2,4) output element is

585/sqrt(sum(dot(I1p,I1p))*sum(dot(I2,I2))) = 0.8070

where I1p = [1 8 15; 7 14 16; 13 20 22].

See Also

conv2 | filter2 | xcorr

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS