## 3 Image Preprocessing and Shape Measurement
### Filling Area Judgments  
By labelling the lines with numbers increasing counter-clockwise as shown in Figure 2, the divided triangles could fall into two categories. One is that the top side of the triangle is longer than the bottom side; another is that the top one is shorter than the bottom one, as shown in Figure 3. The top side is always labelled with a larger number compared to the bottom side in one triangle. \(S_{1}\) and \(S_{2}\) are known quantities based on the former measurement in Figure 3, and \"\(a\)\" represents the included angle between two adjacent measuring lines with a magnitude of 10 degrees. \(d_{1}\), \(d_{2}\), \(d_{3}\), \(d_{4}\) and \(d_{5}\) were calculated as shown in Equation (2).  
\[\begin{array}{l}d_{1}=S_{2}\times\cos a\\ d_{2}=S_{1}-d_{1}\\ d_{3}=S_{2}\times\sin a\\ d_{4}=S_{2}\times\cos a\\ d_{5}=d_{4}-S_{1}\end{array} \tag{2}\]  
When \(S_{2}\leq S_{1}\) (Figure 3a), the algorithm for filling the triangle area with balls by PFC2D was conducted as following:  
* 1 Start the ball generation from Point A with the coordinate of (0, 0);
* 2 Determine the variable \(x_{1}\). It is calculated as the following equation: \[x_{1}=x_{1f}+2\times rad\] (3)  
Figure 3: Algorithm for filling the triangle area with discrete balls: (**a**) \(S_{2}\leq S_{1}\); (**b**) \(S_{2}>S_{1}\).  
where \(x_{1}\) is the X-coordinate of the next column of balls that will be generated; \(x_{1f}\) is the X-coordinate of the former column of balls that has been generated; \(rad\) is the radius of the filled balls.
* 3 Determine the variable \(h\), calculated as shown in Equation (4): \[h=\left\{\begin{array}{ll}x_{1}\times\text{tan}a&x_{1}\leq d_{1}\\ \frac{S_{1}-x_{1}}{d_{2}}\times d_{3}&d_{1}<x_{1}\leq S_{1}\end{array}\right.\] (4) where \(h\) is the required height of the next column of balls; the others are the same as the former.
* 4 Generate uniform balls with coordinates of (\(x_{1}\), 0), (\(x_{1}\), \(2\times rad\)), (\(x_{1}\), \(4\times rad\)), (\(x_{1}\), \(6\times rad\))... until the Y-coordinate exceeds \(h\);
* 5 Start to generate the next column of balls by cycling from Steps 2-4 until the X-coordinate exceeds \(S_{1}\). When \(S_{2}>S_{1}\) (Figure 3b), the algorithm is conducted as follows:
* 1 Start the ball generation from Point A with the coordinate of (0,0); 2 Determine the variable \(x_{1}\). It is calculated as Equation (3):
* 3 Determine the variable \(h_{i}\), calculated as shown in Equations (5) and (6): \[h_{1}=x_{1}\times\text{tan}a\qquad x_{1}\leq S_{1}\] (5) where \(h_{1}\) is the required height of the next column of balls; the others are the same as the former.
* 4 When \(x_{1}\leq S_{1}\), generate uniform balls with coordinates of (\(x_{1}\), 0), (\(x_{1}\), \(2\times\text{rad}\)), (\(x_{1}\), \(4\times\text{rad}\)), (\(x_{1}\), \(6\times\text{rad}\))... until the Y-coordinate exceeds \(h_{1}\); when \(x_{1}>S_{1}\), generate uniform balls with the coordinates of (\(x_{1}\), \(h_{2}\)), (\(x_{1}\), \(h_{2}+2\times rad\)), (\(x_{1}\), \(h_{2}+4\times rad\)), (\(x_{1}\), \(h_{2}+6\times rad\))... until the Y-coordinate exceeds \(h_{1}\);
* 5 Start to generate the next column of balls by cycling from Step 2-4 until the X-coordinate exceeds \(d_{4}\).