of a search image to use as a template. Let represent the value of a search image pixel, where represents the coordinates of the pixel in the search image. For simplicity, assume pixel values are scalar, as in a greyscale image. Similarly, let represent the value of a template pixel, where represents the coordinates of the pixel in the template image. To apply the filter, simply move the center (or origin) of the template image over each point in the search image and calculate the sum of products, similar to a dot product, between the pixel values in the search and template images over the whole area spanned by the template. More formally, if is the center (or origin) of the template image, then the cross correlation at each point in the search image can be computed as:For convenience, denotes both the pixel values of the template image as well as its domain, the bounds of the template. Note that all possible positions of the template with respect to the search image are considered. Since cross correlation values are greatest when the values of the search and template pixels align, the best matching position corresponds to the maximum value of over . Another way to handle translation problems on images using template matching is to compare the intensities of the pixels, using the sum of absolute differences (SAD) measure. To formulate this, let and denote the light intensity of pixels in the search and template images with coordinates and , respectively. Then by moving the center (or origin) of the template to a point in the search image, as before, the sum of absolute differences between the template and search pixel intensities at that point is:With this measure, the lowest SAD gives the best position for the template, rather than the greatest as with cross correlation. SAD tends to be relatively simple to implement and understand, but it also tends to be relatively slow to execute. A simple C++ implementation of SAD template matching is given below. Implementation In this simple implementation, it is assumed that the above described method is applied on grey images: This is why Grey is used as pixel intensity. The final position in this implementation gives the top left location for where the template image best matches the search image. minSAD = VALUE_MAX; // loop through the search image for ( size_t x = 0;