# Study on Relay Contact Bounce Based on the Adaptive Weight Rotation Template Matching Algorithm
## 3 Adaptive Weight Rotation Template Matching Algorithm
### Improvement of Similarity Formula Based on Adaptive Weight  
To address the above situation, the paper proposes improving the similarity metric formula using the method of adaptive weight, as illustrated in Figure 9. Assuming that the size of the original template is \(H^{\star}W\) and the rotation angle is \(\theta\), the size of the new template generated can be calculated with the trigonometric formula, and its width and height are, respectively:  
\[W^{\prime}=H*\sin\theta+W*\cos\theta \tag{6}\]  
\[H^{\prime}=W*\sin\theta+H*\cos\theta \tag{7}\]  
Simultaneously with the generation of rotated templates, an adaptive weight matrix, \(\omega\), corresponding to each template is dynamically generated. This means that based on the size of the template image and the angle of rotation, the weight matrix corresponding to the template at a specific rotation angle is adaptively constructed. This weight matrix is crucial for calculating the similarity between the template and the target image during the template matching process. The size of the matrix is the same as the size of the template, and the elements within the matrix are 0 and 1. The matrix elements within the original image region M (the rectangular dashed box region in the figure) are all 1 after rotation, and the matrix elements within the filled regions N\({}_{1}\), N\({}_{2}\), N\({}_{3}\), and N\({}_{4}\) (the region between the rectangular dashed box and the rectangular solid box in the figure) outside of the original image after rotation are all 0. Next, the similarity formula needs to be updated by incorporating the rotated template. Figure 8: Comparison of templates before and after rotation: (a) the template image before rotation; (b) the template image generated after rotation. Figure 9: Schematic diagram of weight matrix generation. of the templates in the image for each angle, \(\theta_{i}\), using the same method, and summarize them to obtain a similarity matrix:  
\[Vals=[val_{\theta 1},val_{\theta 2},\dots,val_{\theta i},\dots] \tag{13}\]  
Calculate the maximum value of similarity, \(val_{max}\), in the similarity matrix. The template corresponding to this similarity is the target template, and the matching coordinates of this template are the target region matched by the template. The rotation angle corresponding to this template is the tilt angle of the contact obtained. However, the size of this target region is the overall size of the image corresponding to the new template generated after the rotation, as shown in the blue rectangular box in the figure. In order to obtain the corresponding contact edges completely, we need to restore the original template region from the region after the rotation. The method of restoration can be realized by calculating the vertices, as shown in the figure. Assuming that the coordinates of the lower left vertex, D\({}_{0}\), of the matching region corresponding to the new template are (\(x_{0}\),\(y_{0}\)), the coordinates of the four vertices of the original template after the rotation, A\({}_{1}\), B\({}_{1}\), C\({}_{1}\), and D\({}_{1}\), can be calculated as follows:  
\[\left\{ \begin{aligned} x_{1}=x_{0}+H*\sin\theta\\ y_{1}=y_{0}+W*\sin\theta+H*\cos\theta\end{aligned} \right. \tag{14}\]  
\[\left\{ \begin{aligned} x_{2}=x_{0}+H*\sin\theta+W*\cos\theta\\ y_{2}=y_{0}+H*\cos\theta\end{aligned} \right. \tag{15}\]  
\[\left\{ \begin{aligned} x_{3}=x_{0}+W*\cos\theta\\ y_{3}=y_{0}\end{aligned} \right. \tag{16}\]  
\[\left\{ \begin{aligned} x_{4}=x_{0}\\ y_{4}=y_{0}+W*\sin\theta\end{aligned} \right. \tag{17}\]  
By connecting the four vertices A\({}_{1}\), B\({}_{1}\), C\({}_{1}\), and D\({}_{1}\) in turn, the final matched target region can be obtained. As shown in the red dashed rectangular box in Figure 10, the connecting line of the four vertices forms the edge of the target stationary contact. Figure 10: Schematic of target framing after rotation. In the actual recognition process, it is necessary to set the angle range and step size of the generated rotary template.