# Automated Optical Inspection System for O-Ring Based on Photometric Stereo and Machine Vision
## 2 The Proposed System
### Processing of Defect Detection  
The photometric stereo method was used to produce the 3D was obtained. Adaptive threshold using sliding windows was employed to attain the final result image \(I_{R}\). The size of the sliding windows was 50 \(\times\) 50 pixels based on the defect size. The flowchart of this study is shown in Figure 5. We used Figure 3a mechanism with a light source controller (OPT dpa2024e-8) to trigger light source and camera respectively for capture images \(I_{i}\) (\(i\) = 1, 2, 3, 4, 5, 6) (Figure 6). The light source of the mechanism came from 6 directions, from light 1 to light 6. We employed those 6-image (\(I_{i}\), \(i\) = 1, 2, 3, 4, 5, 6) to implement a photometric stereo [13] as shown below:  
1. Make a binary mask (Figure 7 \(I_{6}\)) by using \(I_{6}\). Then, we used this mask to flit background information to reduce background noise effectively. 2. Normalize image by using function (1). Which \(I(x,\,y)\) is a grayscale pixel value at position \((x,\,y)\) of image. \[T(x,y)_{i}=\frac{I(x,y)_{i}}{\text{Max}(I(x,y))}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\] (1)
3. Set a threshold in 100 to binary image Ii (\(i\) = 1, 2, 3, 4, 5, 6) (Figure 7) and calculate image moment \((\overline{x},\ \overline{y})_{i}\) with images Ii (\(i\) = 1, 2, 3, 4, 5, 6) (Figure 7) by function (2). \[(\overline{x},\ \overline{y})=(\frac{M_{10}}{M_{00}},\frac{M_{01}}{M_{00}})\] (2)
4. Define image moment \((\overline{x},\ \overline{y})_{6}\) as centroid and calculate maximum distance to mask edge \(r\). \[r=\text{Max}[(x,y)-(\overline{x},\ \overline{y})_{6}]\ \text{when}\ I(x,\,y)\ >\ 100\] (3)
5. Calculate light source direction vectors \(\overset{\rightarrow}{L}\) using the function (8). \[(dx,dy)_{i}=(\overline{x},\ \overline{y})_{i}-(\overline{x},\ \overline{y})_{6}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\] (4) \[n_{i}=\left[\frac{dx_{i}}{r},-\frac{dy_{i}}{r},\frac{\sqrt{r^{2}-dx_{i}^{2}- dy_{i}^{2}}}{r}\right](i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\] (5) \[R=[0,\ 0,\ 1]\] (6) \[L_{i}=2\times(n_{i};R)\times n_{i}-R\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\] (7)  
Figure 4: Schematic diagram of O-Ring dimension. \[\overset{\rightarrow}{L}=[L_{1},\ L_{2},L_{3},L_{4},L_{5},L_{6}] \tag{8}\]
6. Define normal relative surface normal image \(N\) by using the function (10). \[T(x,y)_{i}=\frac{I(x,y)_{i}}{\text{Max}(I(x,y))}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\]0 (9) \[T(x,y)_{i}=\frac{I(x,y)_{i}}{\text{Max}(I(x,y))}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\]1 (10)
7. Calculate unit vector field \(\overline{N}\) (Figure 8) by the function (11). \[T(x,y)_{i}=\frac{I(x,y)_{i}}{\text{Max}(I(x,y))}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\]2 (11)
8. First, we transfer unit vector field \(\overline{N}\) to triangle mesh. Then define each vertex point as \(Pi\) (\(i\) = 0, 1, 2) as function (12). Furthermore, each vertex point's relative surface normal vector \(\overline{N}_{i}\) (\(i\) = 0, 1, 2). At last, we can define \(\left[N_{x},\ N_{y},\ N_{z}\right]\) by using the function (13). \[T(x,y)_{i}=\frac{I(x,y)_{i}}{\text{Max}(I(x,y))}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\]3 (12) \[T(x,y)_{i}=\frac{I(x,y)_{i}}{\text{Max}(I(x,y))}\ (i=\ 1,\ 2,\ 3,\ 4,\ 5,\ 6)\]4 (13)
9. Now, we can solve each vertex point's relative surface normal vector \(\overline{N}_{i}\) (\(i\) = 0, 1, 2) by using the function (14) and unit vector field \(\overline{N}\). \[\overline{N}_{i}=\left[ \begin{array}{c}N_{x}=(y_{1}-y_{0})(z_{2}-z_{0})-(z_ {1}-z_{0})(y_{2}-y_{0})\\ N_{y}=(z_{1}-z_{0})(x_{2}-x_{0})-(x_{1}-x_{0})(y_{2}-y_{0})\\ N_{z}=(x_{1}-x_{0})(y_{2}-y_{0})-(y_{1}-y_{0})(x_{2}-x_{0})\end{array} \right]\] (14)
10. The deep of the triangle mesh can be rewritten as an overdetermined set. So, in the image resolution \(M\times N\) would include \(2(M-1)(N-1)\) the amount triangle. That is \(2(M-1)(N-1)\) amount linear equation. Each triangle contains two linear equations as the function (16). \[\alpha=\frac{\left[(x_{1}-x_{0})(y_{2}-y_{0})-(y_{1}-y_{0})(x_{2}-x_{0})\right]} {N_{z}}\] (15) \[\overline{N}_{i}=\left[