# Study on Relay Contact Bounce Based on the Adaptive Weight Rotation Template Matching Algorithm
## 3 Adaptive Weight Rotation Template Matching Algorithm
### Principle of Template Matching Algorithm  
The template matching algorithm serves as the core algorithm, with the fundamental concept being to slide a fixed-size template across the image to be matched, calculate the similarity between the template and the local region of the image, and identify the position with the highest similarity as the matching result.  
For the target stationary contact in the relay image, its feature points and shape contour are not obvious, especially in the process of relay action where the contact angle changes. The feature points and shape may also change. Choosing the two methods based on feature descriptors and shape-based matching is prone to matching inaccuracy in such a situation. Therefore, we opt for the most traditional method based on pixel comparison for matching.  
Figure 3: Partial image sequence of relay contact action process.  
Figure 2: Schematic diagram of the structure of each part of the relay.  
First, set a sliding window with a size equal to the template size. Apply the sliding window to the image to be matched, starting from the upper-left corner of the image. Move along the horizontal and vertical directions of the image with a certain step size to find the matching position. For each sliding window position, compare the image region in the window pixel by pixel with the template and calculate the similarity between them.  
Zero-mean Normalized Cross-Correlation (ZNCC) is a commonly used similarity metric which calculates the number of correlations between the template and the image regions to be matched and normalizes them, and the number of correlations reflects the linear relationship between them [26]. The specific calculation method is as follows: Firstly, the template image and the target image are de-meaned, and then the pixel values in the corresponding positions of the de-meaned template region and the image region are multiplied. Then, all the product values are summed up, and the variance of the pixel values in the de-meaned image region and the template region is calculated, and the product and the variance are normalized.  
With \(I\) representing the image to be matched and \(T\) representing the template image, let \(I(x,y)\) and \(T(x,y)\) denote the pixel values of the target image and the template image at position (\(x\),\(y\)), respectively. The formula for the normalized inter-correlation method is as follows:  
\[NCC=\frac{\sum\limits_{x=0}^{m-1}\sum\limits_{y=0}^{n-1}\left[(T(x,y)-\mu T) \times(I(x,y)-\mu I)\right]}{\sqrt{\sum\limits_{x=0}^{m-1}\sum\limits_{y=0}^{n -1}\left[(T(x,y)-\mu T)\right]^{2}\times\sum\limits_{x=0}^{m-1}\sum\limits_{y= 0}^{n-1}\left[(I(x,y)-\mu I)\right]^{2}}} \tag{1}\]  
Figure 4: Flowchart of the rotating template matching algorithm.  
where \(m\) and \(n\) denote the size of the template image and the target image, respectively, and \(\mu I\) and \(\mu T\) represent the pixel mean value of the image region and the template region, respectively, calculated as follows:  
\[\mu T =\frac{1}{m\times n}\sum_{x=0}^{m-1}\sum_{y=0}^{n-1}T(x,y) \tag{2}\] \[\mu I =\frac{1}{m\times n}\sum_{x=0}^{m-1}\sum_{y=0}^{n-1}I(x,y) \tag{3}\]