# Stain Defect Classification by Gabor Filter and Dual-Stream Convolutional Neural Network
## 3 Gabor Image from Stain Defect Image  
Gabor filter is used for image processing for reasons like how the human visual system responds. Gabor filter is a Gaussian filter modulated by a sine function. The Gabor filter image is the result of a convolution operation between the Gabor filter and the image. Because the Gabor filter can acquire the result according to the direction and frequency, it is possible to check what features exist in the input image in a specific direction and frequency band. Let \(x\) and \(y\) be the pixel coordinates of the Gabor filter. Gabor filter can be defined as the following equation. \(\sigma\) is a variable that determines the width of the Gabor filter kernel. \(\lambda\) is a variable that determines the addition/subtraction period of the Gabor filter kernel. \(\theta\) is a variable that determines the direction of the Gabor filter kernel, and \(\psi\) is a mobility variable of the filter. \(\gamma\) is a variable for adjusting the horizontal and vertical ratio of the filter. Lastly, \(x^{\prime}\) and \(y^{\prime}\) are the \(x\) and \(y\) coordinates of the Gabor filter rotated by \(\theta\), respectively. \[ \begin{split} g(x,y;\lambda,\theta,\psi,\sigma,\gamma)=\exp(- \frac{x^{\prime 2}+\gamma^{2}y^{\prime 2}}{2\sigma^{2}})\cos(2\pi\frac{x^{ \prime}}{\lambda}+\psi)\\ where,\,x^{\prime}=x\cos\theta+y\sin\theta\,y^{\prime}=-x\sin \theta+y\cos\theta\end{split}  \tag{5}\]  
Gabor filter can obtain various result values according to internal parameters. Among these results, to select an image to improve the stain defect classification performance, the GLCM and the Haralick texture feature based on it were used. GLCM transformation is an algorithm that transforms two neighboring pixel values in an image into spatial coordinates of an \(M\times N\) matrix. Let the \(\triangle\)m and \(\triangle\)n are offsets in the width and height directions, respectively. the GLCM result matrix \(C_{\triangle m,\triangle n}\) can be defined as follows. \[ \begin{split} C_{\triangle m,\triangle n}(i,j)=\sum_{m=1}^{M} \sum_{n=1}^{N}=\begin{cases}1\text{ {if }}I_{Gray}(m,n)=i\text{ {and }}I_{Gray}(n,\hat{n})=j\\ 0\text{ {otherwise}}\\ \text{ {where, }}\hat{m}=m+\ \triangle\ m\ \hat{n}=n+\ \triangle\ n \end{split}  \tag{6}\]  
The GLCM matrices for \(0^{\circ},45^{\circ},90^{\circ},\) and \(135^{\circ}\) directions can be obtained, respectively, according to the \(\triangle\)m and \(\triangle\)n. The \(\triangle\)m and \(\triangle\)n values for each angle are respectively (0,1), (1,0), (\(-\)1,1), (\(-\)1,\(-\)1). As the stain defect has an unspecified texture, features for all four directions should be used. Therefore, the GLCM matrix for four directions was obtained, and then the average value was used. The average matrix \(P\) for the GLCM can be calculated as follows. \[P=\frac{C_{0,1}+C_{1,0}+C_{-1,1}+C_{-1,-1}}{4} \tag{7}\]  
This paper used the Inverse Difference Moment (IDM) feature, one of the Haralick texture features, as a selection criterion for the generated Gabor filter image. IDM is an index indicating the local homogeneity of an image and it can be expressed as the following equation. Let \(W\) and \(H\) be the width and height of GLCM, respectively, and \(W\) and \(H\) have the same value (\(W=H\)). \[IDM=\sum_{i=0}^{W}\sum_{j=0}^{H}\frac{P(i,j)}{1+(i-j)^{2}} \tag{8}\]  
The IDM value was used to select the Gabor filter images for stain defect classification. The denominator of the IDM equation becomes 1 in the diagonal component of the GLCM, having the same brightness value as the neighboring pixels, and the IDM value becomes the maximum in the main diagonal section. Meanwhile, as the distance from the diagonal component of the GLCM increases, the denominator's value rises, and the IDM value decreases. In other words, a large IDM value suggests the presence of multiple pixels having similar brightness, and a small IDM value means that there are many pixels with a difference between neighboring pixels.