# Feature-Model-Based In-Process Measurement of Machining Precision Using Computer Vision
## 3 Measurement Implementation and Verification
### Measurement of Flange Workpieces  
#### 3.4.1 Measurement of Roundness of Flange Workpiece  
Roundness is a measurement criterion used to describe the deviation of a circular workpiece or component from an ideal circle. It is typically defined as the difference between the maximum and minimum distances from each point on the workpiece surface to its best-fit circle. As shown in Figure 13, the contour point set of a flange workpiece forms a set of data points (\(x_{i}\), \(y_{i}\)), where \(i\) = 1, 2,\(\cdots\), \(n\), representing coordinates on the circular workpiece surface. Assuming a circular model, these data points are fitted using the least squares method. The objective of the least squares method is to find the best-fit circle that minimizes the fitting error in terms of its center and radius. Initially, these data points are fitted into contour lines using the least squares method to ensure that the fitted line closely approximates the actual data points. \[S(a,b,R)=\sum_{i=1}^{n}{\left((x_{i}-a)^{2}+(y_{i}-b)^{2}+R^{2}\right)^{2}} \tag{29}\]  
where (\(a\), \(b\)) are the coordinates of the circle's center, and \(R\) is the radius of the circle. \(S(a\), \(b\), \(R)\) represents the sum of squared errors that need to be minimized with respect to \(a\), \(b\), and \(R\). Figure 13: Diagram of flange workpiece sample detection. \begin{table}
\begin{tabular}{c c c c c c} \hline \hline \multirow{2}{*}{**Measurement Position**} & \multirow{2}{*}{**Part**} & **Three Coordinate Measuring Machine (mm)** & **This Article’s Algorithm** & **Sub-Pixel Edge Detection (mm)** & **Otsu–Canny Edge Detection Algorithm (mm)** \\ \hline \multirow{4}{*}{straightness of the outside line 2} & part 1 & 0.047 & **0.043** & 0.039 & 0.041 & 0.041 \\  & part 2 & 0.027 & **0.031** & 0.021 & 0.037 & 0.035 \\ \cline{1-1}  & part 3 & 0.040 & **0.043** & 0.066 & 0.048 & 0.051 \\ \cline{1-1}  & part 4 & 0.059 & **0.052** & 0.050 & 0.049 & 0.050 \\ \cline{1-1}  & part 5 & 0.047 & 0.038 & **0.046** & 0.040 & 0.042 \\ \hline \multirow{4}{*}{straightness of the straight lines inside 1} & part 1 & 0.035 & **0.036** & 0.038 & 0.037 & 0.040 \\ \cline{1-1}  & part 2 & 0.017 & **0.023** & 0.029 & 0.038 & 0.025 \\ \cline{1-1}  & part 3 & 0.015 & 0.025 & **0.023** & 0.026 & 0.030 \\ \cline{1-1}  & part 4 & 0.026 & **0.025** & 0.020 & 0.031 & 0.033 \\ \cline{1-1}  & part 5 & 0.027 & **0.033** & **0.033** & 0.040 & 0.043 \\ \hline \multirow{4}{*}{straightness of the straight lines inside 2 part 5} & part 1 & 0.030 & 0.026 & 0.038 & **0.030** & 0.036 \\ \cline{1-1}  & part 2 & 0.046 & **0.040** & 0.063 & 0.061 & 0.066 \\ \cline{1-1}  & part 3 & 0.016 & **0.026** & 0.038 & 0.028 & 0.035 \\ \cline{1-1}  & part 4 & 0.018 & **0.014** & 0.028 & 0.032 & 0.029 \\ \cline{1-1}  & part 5 & 0.023 & **0.033** & **0.033** & 0.037 & 0.040 \\ \hline \hline \end{tabular}
\end{table} 
Table 3: _Cont._By taking partial derivatives of \(S(a,\,b,\,R)\) with respect to \(a,\,b,\) and \(R\), and setting them equal to zero, we can obtain the estimated values of the circle's center coordinates \(a\) and \(b\), as well as the radius \(R\) for the best-fit circle:  
\[a=\frac{\sum_{i=1}^{n}x_{i}((x_{i}-\overline{x})(x_{i}^{2}+y_{i}^{2}-\overline{ x}^{2}-\overline{y}^{2}))}{\sum_{i=1}^{n}\left(x_{i}^{2}+y_{i}^{2}-\overline{x}^{2}- \overline{y}^{2}\right)^{2}} \tag{30}\]  
\[b=\frac{\sum_{i=1}^{n}y_{i}((y_{i}-\overline{y})(x_{i}^{2}+y_{i}^{2}-\overline {x}^{2}-\overline{y}^{2}))}{\sum_{i=1}^{n}\left(x_{i}^{2}+y_{i}^{2}-\overline {x}^{2}-\overline{y}^{2}\right)^{2}} \tag{31}\]  
\[R=\sqrt{\frac{1}{n}\sum_{i=1}^{n}\left((x_{i}-a\right)^{2}+(y_{i}-b)^{2}} \tag{32}\]  
where \(x,\,y\) are the mean values of the data points. Once the center coordinates \(a,\,b,\) and radius \(R\) of the best-fit circle are obtained, the perpendicular distance di from each data point to the fitted circle can be calculated.