# Detection of Defects in Warp Knitted Fabrics Based on Local Feature Scale Adaptive Comparison
## 2 Scale Adaptive Contrast Method
### Most Similar Neighborhood  
To accurately estimate the center point pixel, find the most similar neighborhood first. Each pixel in the image shall be taken as the center point, the area within a certain range around it shall be taken as the center area, and the area around the center area with the same size as the adjacent area shall be taken as the adjacent area. Meanwhile, the protection area shall be set around the center area to avoid noise pollution. The scale value \(r_{s}(c)\) of each pixel point is determined according to the previous summary. If the scale of the center pixel point \((x_{0},y_{0})\) is \(r_{0}(c)\), make the adjacent center pixel points \((x,y)\) outside the scale have the same scale with them, and set the protection scale \(m=1\), then the scale of the protection area \(E_{0}\) is \(r_{p}(c)=r_{0}(c)+1\). Suppose the central area is \(V_{0}\), and there are \(n\) adjacent areas \(V_{n}\) (\(n\) is a positive integer) with the same scale around it. The principle of the algorithm is shown in Figure 2.  
In Figure 2, the scale of pixel \((x_{0},y_{0})\) is 2, and its central region \(V_{0}\) has 13 pixels in total. If the size of the protection area is set to \(r_{p}(c)=r_{0}(c)+1=3\), the dimension of the protection area is \(E_{0}\). Outside the protection area \(E_{0}\), n neigh  
Figure 2: Schematic diagram of algorithm.  
\(k=1,\cdots,n\)) with the same size as \(V_{0}\) can be found, and its central pixel point is \((x_{k},y_{k})\), forming a black pixel diamond.  
Let \(D_{r}f_{i}(x_{0},y_{0})(i=1,\cdots,13)\) denote the gray value of each pixel in the central region, \(D_{r}f_{i}(x_{k},y_{k})(i=1,\cdots,13)\) denote the gray value of the pixel corresponding to each pixel in the central region in the surrounding \(k\) neighborhood, and set the similarity between the central region and this neighborhood to \(S_{k}\), then \(S_{k}\) can be calculated from the following formula:  
\[S_{k}=\frac{1}{\sum\limits_{i=1}^{13}\left(D_{r}f_{i}(x_{0},y_{0})-D_{r}f_{i}( x_{k},y_{k})\right)^{2}+1} \tag{3}\]  
In the formula, the value range of \(S_{k}\) is [0, 1]. When the value of \(S_{k}\) tends to 0, it means that the gray values of all pixels in the central neighborhood and the corresponding pixels in the surrounding \(k\) neighborhood differ greatly, that is, the similarity between the two regions is weak. When the value of \(S_{k}\) tends to 1, it means that the gray values of each pixel in the central neighborhood and the corresponding pixels in the surrounding \(k\) neighborhood differ very little, that is, the similarity between the two regions is high.  
Calculate the value of similarity \(S_{k}\) according to Equation (3), and find the neighborhood with the greatest similarity with the central region, that is, the most similar neighborhood \(V_{s}\):  
\[\begin{array}{c}i=\underset{k=1,2\cdots n}{\operatorname{argmax}}(S_{k})\\ V_{s}=V_{i}\end{array} \tag{4}\]  
where \(i\) means that the \(i\)-th neighborhood image has the highest similarity with the central region; \(V_{i}\) represents the \(i\)-th block neighborhood image.  
If the current pixel is a defective pixel, a central region is determined by the pixel, and no region with high similarity with it can be found around it. The gray value of the current pixel evaluated by all pixels in the most similar neighborhood will differ greatly from the gray value of the pixel in the original image, so the current pixel is identified as a defective pixel according to the method in the subsequent steps.  
In addition, the current pixel is a non-defective pixel (background region pixel), which is used to determine a central region, and a neighborhood with high similarity can be found in its surrounding neighborhood. The difference between the gray value of the current pixel evaluated by all pixels in the most similar neighborhood and the gray value of the pixel in the original image will be very small, so the current pixel is determined to be a non-defective pixel according to the method in the subsequent steps.