Template matching is a basic and simple pattern matching technique in digital signal processing, in particular in digital image processing.
Matching is an operation to determine the similarity between two entities (a template/reference signal/entity and a target signal/entity).
For 2D images, template matching uses a reference image (the template), which can be a sample of a real image or, for some applications, a synthetized prototype of the pattern. The pattern is usually smaller than the image. The problem is to find if and where there is an ocurrence (or at least a similar enough occurrence) of the template in the target image.
The correlation approach uses the correlation coefficient as a measure of similarity between the reference (template) for each location (x,y) in the target image. The result will be maximum for locations where the template have correspondence (pixel by pixel) to the subimage located at (x,y).
In the image domain, this aproach can be seen as if the template t(x,y) is a spatial filter, and then the computation of (a normalized form of) the sum of products of pixel values, for every location at the target image f(x,y). Maximum values occurs for location where the subimage of f(x,y) perfectly matches the template, results will be as high as the subimage better matches the template, for each location.
Images involved are usually normalized by subtracting the mean pixel value and dividing by the standard deviation.
As some templates can occur in several orientations / scales, the process must be conducted for every template tranformation. This makes this process computationally expensive. Faster processors and parallel processing, available for instance at consumer level GPUs, made this process more feasible for pratical applications.
Correlation can be also done in the frequency domain, as correlation is related to the product of image (Fourier) transforms by the convolution theorem. Thus, correlation is obtained by multiplying the image (Fourier) transform with the complex conjugate of the template (Fourier) transform. Peak locations (high correlations -> matches) at spatial domain are then obtained using the inverse (Fourier) transform.
"Correlation is the process of moving the template or subimage w around the image area and computing the value C in that area. This involves multiplying each pixel in the template by the image pixel that it overlaps and then summing the results over all the pixels of the template. The maximum value of C indicates the position where w best matches f." (or peaks in the matrix you get back are then locations of a pattern in the searched imagematrix)
Correlation is intensity dependant, so better use normalized Xcorr. Try to have the ROI filled with information.. so don't have too much border/frame in the image.
In pattern matching Xcorr is used to initially find good matches in an easy way.
for 1d Xcorr see: https://en.wikipedia.org/wiki/Cross-correlation
A good free prog you may try if you dont use special programming language is ImageJ there you can find plugins also for Xcorr: e.g.: https://imagej.nih.gov/ij/plugins/image_correlator.html