1. Reference materials
2. Introduction to template matching
1. The concept of template matching
In layman's terms, template matching is to search images based on images and find small areas in the entire image area that match a given sub-image.
2. Classification of template matching
2.1 Relevance-based template matching
ALL
2.2 Grayscale-based template matching
Image Matching Algorithm Based on Grayscale and Its Improvement
The template image does not undergo any preprocessing, and its pixel values are directly taken to perform relevant operations on the original image.
Advantages and disadvantages
Advantages: fast.
Disadvantages: Template matching based on grayscale is sensitive to changes in lighting and brightness, and the matching is not robust.
2.3 Edge-based template matching
Edge-based template matching is also called gradient value-based template matching.
principle
Calculate the corresponding gradient map for the template image and the original image respectively, and perform related calculations through the gradient map.
2. Limitations of template matching
Template matching has its own limitations, mainly in that it can only move in parallel. If the matching target in the original image rotates or changes in size, the algorithm will be invalid.
3. Template matching principle
On the image to be detected, from left to right and from top to bottom, the matching degree of the template image and the overlapping sub-image is calculated. The greater the matching degree, the greater the possibility that the two are the same.
4. Calculation process of template matching
In this chapter, the calculation process of template matching is demonstrated digitally with images.
Assume, the reference image matrix G:
The template image matrix T is:
The center point 5 of the template image matrix T starts from the starting point (0,0) of the reference image matrix G, from left to right, and from top to bottom, and the operations are performed sequentially, and the padding of the out-of-boundary part is filled with zeros.
G s ( 0 , 0 ) = 0 ∗ 1 + 0 ∗ 2 + 0 ∗ 3 + 0 ∗ 4 + 0 ∗ 5 + 0 ∗ 6 + 0 ∗ 7 + 0 ∗ 8 + 1 ∗ 9 = 9 Gs(0, 0) = 0*1 + 0*2 + 0*3 + 0*4 + 0*5 + 0*6 + 0*7 + 0*8 + 1*9 = 9 Gs(0,0)=0∗1+0∗2+0∗3+0∗4+0∗5+0∗6+0∗7+0∗8+1∗9=9
G s ( 0 , 1 ) = 0 ∗ 1 + 0 ∗ 2 + 0 ∗ 3 + 0 ∗ 4 + 0 ∗ 5 + 0 ∗ 6 + 0 ∗ 7 + 1 ∗ 8 + 2 ∗ 9 = 26 Gs(0, 1) = 0*1 + 0*2 + 0*3 + 0*4 + 0*5 + 0*6 + 0*7 + 1*8 + 2*9 = 26 Gs(0,1)=0∗1+0∗2+0∗3+0∗4+0∗5+0∗6+0∗7+1∗8+2∗9=26
...
Calculate all correlation coefficient matrices Gs:
It can be seen from the correlation coefficient matrix Gs that the maximum parameter value in the matrix is 285. The maximum parameter value corresponds to the center point of template matching, and its green line frame is the template matching result.