# 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
The angle range relates to the accuracy and speed of recognition, while the angle step size is crucial for accuracy and speed. Too large a range or too small a step can enhance recognition accuracy, but it will lead to too many templates and a significant decrease in recognition speed. If the range is too small, it will not cover all the angles of the contact, rendering recognition impossible. If the step is too large, it will affect recognition accuracy and increase recognition errors. To enhance recognition speed while maintaining the algorithm's accuracy, employing multi-threading in parallel could be considered. Each target angle could be assigned a separate thread for recognition. This approach enables simultaneous recognition of multiple angles, followed by result aggregation for comparison and analysis to accomplish recognition. Additionally, adopting an image pyramid can expedite algorithm processing. Initially, recognition can be performed on the top-level, low-resolution image of the pyramid, followed by recognition around the target position in the next lower level, until recognition is completed in the original image at the bottom level. After several tests for the relay studied in this paper, the angle range of the stationary contact rotation template was set between \(-10^{\circ}\) and \(5^{\circ}\), and the step size of the rotation angle was 0.1\({}^{\circ}\). This can cover all the tilt angles of the stationary contact during the action process, ensuring recognition accuracy and also improving recognition speed as much as possible. The adaptive rotary template matching algorithm is used to recognize the relay part of the image sequence, and the recognition results are shown in Figure 11.  
To validate the algorithm's recognition performance, both the traditional normalized cross-correlation method (Equation (1)) and the improved formula (Equation (13)) were separately utilized as similarity calculation formulas for the rotation template matching algorithm. These were then compared with the traditional template matching algorithm. A set of complete images from the relay's operational process were selected for testing, with IoU (Intersection over Union) used as the accuracy evaluation metric. IoU represents the ratio of the intersection area between the predicted bounding box and the ground truth bounding box to the union area. The calculation formula is as follows:  
\[IoU=\frac{Intersection}{Union} \tag{18}\]  
where \"_Intersection_\" represents the area of overlap between the detection box and the ground truth box, while \"_Union_\" represents the area of union between the detection box and the ground truth box. When IoU is greater than 0.9, it is considered a successful match. Using this criterion, we can calculate the recognition accuracy of each algorithm. The calculation formula is as follows:  
\[Accuracy=\frac{TP+TN}{TP+TN+FP+FN} \tag{19}\]  
where \(TP\) represents true positives, \(TN\) represents true negatives, \(FP\) represents false positives, and \(FN\) represents false negatives. The final test results are shown in Table 3. It can be observed from the table that the accuracy of the rotation template matching algorithm, after the improvement of the similarity calculation formula, can reach over 98%. This represents a significant improvement  
Figure 11: Effect of identifying relay stationary contacts at different angles. in accuracy compared to both traditional template matching algorithms and traditional formula-based template matching algorithms.