You can find an introduction to signals here. This post is part of a series on Image and Signal Processing. If you are looking for convolution, you may find it here.
Cross-correlation is a measure of similarity between two signals; that measure is computed as a function of the displacement of one relative to the other.
Mathematical definition
Cross-correlation is mathematically defined as:
Now, the important part is understanding what’s going on here. The integral is just the multiplication, point-by-point, of the values of the functions — and . That means that you: evaluate at every point from to ; do the same for ; multiply the two values for each point, e.g. , , etc.
As you may see, also features another term: . As explained in the Signals, the basics, subtracting some quantity from the input of a function/signal does nothing but translating it to the right (it get delayed in time). Of course, if is negative, the two signs cancel out and you get a function translated to the left.
As you may have already seen, the only input to the cross-correlation is : you compute the cross-correlation between and with translated left or right. This means that the integral computes the similarity between the two signals once they are fixed on the axis, while lets you decide where to move .
Sliding
Computing cross-correlation is no more than fixing the first signal and making the second signal slide above it and, for every possible lag , computing the integral. This means that computing cross-correlation on a computer is basically impossible: you would need infinite memory.
This is not a problem, however: cross-correlation is widely used to support many image and signal processing techniques. How do we do that? Well, computers work with digital values, which means we sample signals and only have a finite amount data. In fact, what we computed is a discretized version of the cross-correlation (explained below).
Normalized cross-correlation
Signals are often subjected to noise: they are not clear and pure mathematically-defined signals. They come from the real world. Computing a cross-correlation may thus result in incorrect values: it could produce bad results. To try to avoid this, you can use normalized cross-correlation, which basically takes into account the energy of and to make sure that the result is… well, normalized.
Normalized cross-correlation produces values bounded in : . It is mathematically defined as:
Autocorrelation
There’s a particular and special case: it happens when — that is, and are really the exact same signal. In that case, cross-correlation is called autocorrelation, since the signal is cross-correlated with itself.
Informally, this is the measure of similarity between two different observations of the same signal as a function of the time lag between them.
It’s particularly useful to find repeating patterns, period signal obscured by noise, finding the fundamental frequency, etc.
Discrete cross-correlation
Computers work with discrete set of values. That is why we use discrete cross-correlation, which is defined as:
If has length and has length , then the discrete cross-correlation is going to have length .
Cross-correlation with images
Images can be though of as 2D signals. In fact, , where and are the pixel coordinates and is the grayscale or RGB value of that pixel .
It is therefore possible to compute the discrete cross-correlation between two images — call them and . Formula is:
Image is said to be the template or kernel, while is called the image.
2D cross-correlation is particularly useful to find something in an image: if you have a sample of what you are looking for (kernel), you can slide it over the whole image and find where it best matches the image itself. If the value where the cross-correlation is the highest is higher than a threshold, then you can be pretty confident you found the match you were looking for.
2D cross-correlation can also be used for filtering images with “special” kernels: you may use it to blur an image, to remove noise, to reinforce its edges and shapes, etc. Cross-correaltion is a really powerful tool that comes in very handy.