# Detection of Defects on Metal Surfaces Based on Deep Learning
## 2 Methodology
### Image Processing  
Image processing methods are applied to reduce the size of image data, make defects more prominent, and obtain a pattern that is independent of the background. In this algorithm, the NEU dataset containing 1800 images with defects is used. The images are first converted to grayscale. \[I(x,y)=0.2989\cdot R(x,y)+0.5870\cdot G(x,y)+0.1140\cdot B(x,y) \tag{1}\]  
while \(I(x,y)\) represents the grayscale image, \(R(x,y)\), \(G(x,y)\), and \(B(x,y)\) represent the red, green, and blue components of each pixel in the input image, respectively. To fill in the gaps in the grayscale image and make the defective areas more prominent, morphological operations [21,22] are applied to the image. A structural element is defined during the operations, and in the related study, a structural element of size 3 \(\times\) 3 was used. The first step in the sequence of morphological operations is the closing phase. This process involves applying dilation followed by erosion. The dilation operation expands the white pixels of the object and turns each black pixel that intersects with the structuring element's white pixels into white. In this way, the boundaries of the object are expanded. The erosion operation is applied to the expanded image obtained after the dilation step. Erosion shrinks the boundaries of the object, ensuring that the entire structuring element fits within the object. Thus, it preserves only those pixels where the structuring element is completely within the object. \[I(x,y)\cdot B=(I(x,y)\oplus B)\odot B \tag{2}\]  
\(I(x,y)\) represents the grayscale image. \(B\) is the structuring element used to perform operations on the pixels in the image. The structuring element slides over the image. The symbol \(\oplus\) denotes the dilation operation. \(\ominus\) denotes the erosion operation. The second step of morphological operations is the Opening operation, which consists of erosion followed by dilation. Erosion is used to shrink objects in the image and to eliminate small objects or thin structures. Dilation then enlarges the image and increases the size of the objects, partially reversing the \"shrinking\" effect left by the erosion process, but without restoring the removed noise. \[I(x,y)\circ B=(I(x,y)\ominus B)\oplus B \tag{3}\]  
Figure 1: Flow diagram of the proposed method. \(I(x,y)\) represents the image resulting from the closing operation and serves as the input for the opening operation. B is the structuring element. \(\ominus\) denotes the erosion operation, while \(\oplus\) represents the dilation operation. \(I(x,y)\circ B\) represents the opening operation. After these operations, Gaussian blur is applied to the image to remove any remaining noise and smooth out non-defective details. This process helps eliminate details that could potentially hinder defect detection on the image. \[G(x,y)=\frac{1}{2\pi\sigma^{2}}\exp\biggl{(}-\frac{x^{2}+y^{2}}{2\sigma^{2}} \biggr{)} \tag{4}\]  
\[I^{\prime}(x,y)=I*G(x,y)=\sum_{m=-\infty}^{\infty}\sum_{n=-\infty}^{\infty}I(m,n)\cdot G(x-m,y-n) \tag{5}\]  
\(I^{\prime}(x,y)\) represents the output image, \(I(x,y)\) represents the input image, and \(G(x,y)\) denotes the Gaussian function. A convolution operation is performed between the image and the filter. The convolution operation applies the filter to each pixel of the image, resulting in a new image. After this stage, Principal Component Analysis (PCA) [23] is applied. The aim here is to reduce the dimensionality of the image data and to enhance the visibility of the defects. The image matrix was flattened and normalized by zero-centering its mean. Subsequently, the covariance matrix was constructed, and eigenvalues and eigenvectors were computed to project the data onto a lower-dimensional space, thereby enhancing the efficiency of feature extraction. PCA reduced data density and processing costs through dimensionality reduction. With a value of 0.8, sufficient information from the image was retained.