x <= S_cols - T_cols; x++ ) { for ( size_t y = 0; y <= S_rows - T_rows; y++ ) { SAD = 0.0; // loop through the template image for ( size_t j = 0; j < T_cols; j++ ) for ( size_t i = 0; i < T_rows; i++ ) { pixel p_SearchIMG = S[y+i][x+j]; pixel p_TemplateIMG = T[i][j]; SAD += abs( p_SearchIMG.Grey - p_TemplateIMG.Grey ); } // save the best found position if ( minSAD > SAD ) { minSAD = SAD; // give me min SAD position.bestRow = y; position.bestCol = x; position.bestSAD = SAD; } } } One way to perform template matching on color images is to decompose the pixels into their color components and measure the quality of match between the color template and search image using the sum of the SAD computed for each color separately. Speeding up the process In the past, this type of spatial filtering was normally only used in dedicated hardware solutions because of the computational complexity of the operation, however we can lessen this complexity by filtering it in the frequency domain of the image, referred to as 'frequency domain filtering,' this is done through the use of the convolution theorem. Another way of speeding up the matching process is through the use of an image pyramid. This is a series of images, at different scales, which are formed by repeatedly filtering and subsampling the original image in order to generate a sequence of reduced resolution images. These lower resolution images can then be searched for the template (with a similarly reduced resolution), in order to yield possible start positions for searching at the larger scales. The larger images can then be searched in a small window around the start position to find the best template location. Other methods can handle problems such as translation, scale, image rotation and even all affine transformations. Improving the accuracy of the matching Improvements can be made to the matching method by using more than one template (eigenspaces), these other templates can have different scales and rotations. It is also possible to improve the accuracy of the matching method by hybridizing the feature-based and template-based approaches. Naturally, this requires that the search and template images have features that are apparent enough to support feature matching. Similar methods Other methods which are similar include 'Stereo matching', 'Image registration' and 'Scale-invariant feature