# Feature-Model-Based In-Process Measurement of Machining Precision Using Computer Vision
## 2 Measurement Algorithm
### Matching of Machining Feature Model with Part Drawing  
The matching of the processing feature model with the part image is crucial for the subsequent ROI extraction and part recognition. However, the processing feature models are typically represented as line segments in white pixels, and they lack the surface characteristics, while the actual part images in the background may contain various colors and patterns, making it difficult to directly match the processing feature model with the part image. To solve this problem, it is necessary to fill the processing feature model to enhance the contrast with the background, thereby facilitating the subsequent matching and ROI extraction. The first step in the filling process is to expand the processing feature model. The black pixels adjacent to the white pixels are converted to white pixels, thereby expanding the white regions in the image. An n \(\times\) n window is set to slide on the image of the machining feature model, and if the area covered by the window contains white pixels, the pixel at the center of the window is set to white. Through the expansion operation, the small holes and narrow gaps in the processing feature model are filled, making it a continuous and distinct white region. Next, an erosion operation is performed on the expanded processing feature model, causing white areas to contract inward. The erosion operation uses an n \(\times\) n window that slides across the image. This eliminates small objects, isolated points, and spikes in the image, while also smoothing the boundaries of larger objects. Since the dilation operation filled the edges of the model, the erosion operation will not remove these connected regions, as shown in Figure 4.  
Figure 4: Filling of image: Image (**a**) results in Image (**b**) after filling. The matching of the fabrication feature model and the part image is based on centroids. First, the centroid of the part image is calculated by taking the weighted average of the positions of all the points in the image. Secondly, the Euclidean distance between the centroid of the fabrication feature model and the centroid of the part image is calculated to determine the offset between the two. Then, based on the calculated centroid offset, a translation operation is performed on the fabrication feature model to align its centroid with the centroid of the part image. This aligns the two images on the same plane. Finally, rotation-invariant features are introduced, and the fabrication feature model is rotated counterclockwise around the centroid to begin matching overlapping pixels between the model and the part image. During rotation, the set of overlapping points between the fabrication feature model and the part image is calculated to find the optimal matching position between the model image and the part image. Because the fabrication feature model is an ideal representation, while the part image has noise and machining marks as interfering factors, the overlay of the images cannot be a complete match. Therefore, when the matching rate reaches 90% or higher, the ROI can be selected based on the dimensional elements within the model. The position relationship between the fabrication feature model and the part image is used to determine the ROI. The ROI contains the features to be measured, and the location and size information of the original ROI image is determined. The range of the ROI in the image is calculated based on the width d of the part ROI. For example, in Figure 5, the ROI range can be determined based on the dimensional information of the fabrication feature model. The ROI boundaries or rectangular area are adaptively determined based on the size of the part, with the specific formula being as follows. \[d=\frac{w}{h}(l\times gridsize) \tag{12}\]  
where \(w\) and \(h\) are the length and width of the photo, respectively, and \(l\) is the design length of the part.