Template matching is a method of searching for a similar pattern between pictures. This is accomplished by taking a reference image, and sliding it around another comparison image, while taking differences at each position. The result of these differences indicate how close a particular feature of the comparing image can match the template.
- [Narrator] When it comes to feature detection, template matching is a readily available and straightforward method. The way template matching works, is it searches for a similar pattern between two images. This is accomplished by taking a reference image, called a template and sliding it around the other comparison image, taking a difference at every position. The result, is a black and white gray scale image with varying intensities showing how well it matched at each position. Using a 1D, somewhat modified example, we can see that as we slide our template, which is a triangle, across the screen, it does a comparison in each spot.
On the far left there are no matches, so it has a perfectly black value, as it goes to the right and starts to overlap it gets a gray value, indicating a partial match at some of the pixels of the template itself. Then when it perfectly over laps the red triangle in the image, it gives a perfectly white value. As it continues unto the right, it will go back to black indicating that there was a no match. Typically, template matching is actually applied in a two-dimensional format but the concept is the same, your source template image will scroll horizontally and vertically across the entire image, taking a difference at each location.
The sum result of that difference is put into the pixel value, where a zero sum difference mean the exact same images becomes white and a perfect difference become black. Typically you'll find there's lots of gray in your image as there are always going to be partial matches of some of the pixels in the template versus your image. The example shown here, we have a yellow ball used as the template. Of course it's only one yellow ball in the actual scene and we can see very clearly in the output that the brightest spot of the image is exactly where we expect the ball to be. However there are partial matches elsewhere in the image most likely where the yellow channels seem to overlap.
Typically with template matching you don't actually use an element from the source image yourself but something that is predetermined, such as a face or a known generic object that is expected to be found in the scene. Given that, it's important to understand a few of the limitations of template matching. If your template is scaled compared to your Source image then it will not work very well, likewise if your template is rotated and the template looks different at those different rotations, it may reduce the effectiveness of the template matching. Despite that it's still a very efficient algorithm and can be very useful in some scenarios.
- Installing and configuring OpenCV
- Data types and structures
- Image types
- Manipulating pixels
- Scaling and rotating images
- Using video inputs
- Creating custom interfaces
- Object detection
- Face and feature detection
- Template matching