# Image-Based Scratch Detection by Fuzzy Clustering and Morphological Features
## 2 The FCM Algorithm and Scratch Detection by Growth Area  
### The FCM Algorithm  
The fuzzy c-means (FCM) algorithm proposed by [PERSON] [21] and [PERSON] [22] is the best known and most used detection method. It is a method for finding groups within data with the most similarity in the same cluster and the most dissimilarity between different clusters [23]. The piezoelectric ceramic plate image I is an (\(M\), \(N\)) 2D matrix. Let \(x=(x_{1},x_{2},\cdots,x_{n})^{\prime}\) be the dataset constituted by gray values of image I, with \(n=M\times N\). The FCM objective function is defined as follows:  
\[J(u,v)=\sum\
olimits_{k=1}^{2}\sum\
olimits_{i=1}^{n}u_{ik}^{m}(x_{i}-v_{k})^ {2} \tag{1}\]  
subject to  
\[\sum\
olimits_{k=1}^{2}u_{ik}=1\text{ with }u_{ik}\in[0,1],\ 1\leq i\leq n,\ 1 \leq k\leq 2 \tag{2}\]  
where the weighting exponent \(1<m<+\infty\) presents the degree of fuzziness, and \(u_{ik}\) and \(v=(v_{1},v_{2})\) are the fuzzy membership of the value \(x_{i}\) in the \(k\)th cluster and the cluster center vector, respectively. The optimum solution for minimizing \(J(u,v)\) can be calculated by updating the equations as follows:  
\[u_{ik}=\frac{\left(x_{i}-v_{k}\right)^{2/1-m}}{\sum\
olimits_{l=1}^{2}\left(x _{i}-v_{t}\right)^{2/1-m}} \tag{3}\]  
\[v_{k}=\frac{\sum\
olimits_{i=1}^{n}u_{ik}^{m}x_{i}}{\sum\
olimits_{i=1}^{n}u_ {ik}^{m}}. \tag{4}\]  
In industrial visual defect detection, the illumination environment is relatively stable. This means that the spatial distribution of the gray value of the same kind of piezoelectric ceramic plate image is relatively fixed, as in Figure 1. Therefore, a dataset composed of gray images can be collected to analyze the foreground membership of the gray value in the image when the light environment and camera are fixed. This process can be completed offline. Figure 2 shows the scratch images under the imaging system shown in Figure 1. Among them, Figure 2a contains four narrow scratches with different lengths, and Figure 2b contains two wide scratches and one narrow scratch. In the online detection process, if 256 (0-255) gray values are not fully covered, the foreground membership of new gray values can be approximately calculated by the interpolation method. Define the scratch area as the foreground and the non-scratch area as the background. Then, the background and foreground membership of each pixel in the image can be obtained from Formulas (3) and (4). In Figure 3, the FCM algorithm was used to cluster the foreground and background images with scratches in Figure 2. Figure 3a shows the changes in the clustering centers of foreground and background gray values with the number of iterations. It can be found that the clustering centers are basically stable after two iterations. Figure 3b shows the foreground membership of different gray values in the clustering results. It can be found that the membership degree is about 0.5 when the gray value is 117-118. Figure 3: The clustering result by the fuzzy c-means (FCM) algorithm. (**a**) The results of iterative convergence of clustering centers. (**b**) The clustering result of foreground membership. Figure 2: Images of a piezoelectric ceramics plate with typical scratch defects. (**a**) Narrow scratch on the surface of a piezoelectric ceramic plate. (**b**) Wide scratch on the surface of a piezoelectric ceramic plate. Let the foreground membership vector of the gray value calculated by the FCM be \(f\in R^{256}\), \(f_{i}\in[0,1],\ i=1,\cdots,256\). A new image _IF_, obtained by preprocessing, was composed of the product of the gray value of each pixel of the image and its corresponding foreground membership coefficient \((2f_{i}-1)\), such as in Figure 3b. In order to further enhance the foreground gray value in the image, we replaced the product of the gray value and the corresponding foreground membership value by Formula (5), as follows:  
\[IF(i,j)=\left\{ \begin{array}{c}I(i,j)\cdot(2f_{q}-1),\ 1/2\leq f_{q}\leq 1,\ q=I(i,j)+1\\ 0,\ 0\leq f_{q}<1/2,\ q=I(i,j)+1\end{array} \right.