Research on the combination of improved Sobel operator and ant colony algorithm for defect detection

There are many aspects in the defect detection system. Any deviation in any link will affect the accuracy of the final detection, and edge detection is very important in the image preprocessing stage. In this paper, a new edge detection algorithm is proposed. Firstly, the improved Sobel operator is used to detect the image contour, and then the position of the contour is taken as the initial position of ant colony algorithm. The experimental results show that the algorithm can extract the contour with uniform thickness and length from the original image collected by the industrial camera, and the running time of the algorithm is almost the same as that of the traditional ant colony algorithm, thus providing more accurate data for the defect detection of products in the later stage.


Introduction
Nowadays, with the rapid development of digital image processing and computer vision technology, more and more researchers use the camera as the sensing sensor to detect product defects. This is mainly because the original ultrasonic or infrared sensors have limited information and poor robustness, and the vision system can make up for these shortcomings.
Before using industrial camera to detect defects, the most important thing is to extract the product edge completely. In reference [1][2], the traditional Sobel algorithm is improved, from the original horizontal and vertical templates to four direction templates, the integrity of the edge has been improved to a certain extent. In reference [3], the adaptive OSTU is used the algorithm is used to detect the edge of ship target, and the segmentation effect of binary target is better, but it is affected by uneven illumination and other factors. In the traditional ant colony algorithm, he initial position of ants is randomly distributed. In reference [4], the ant colony algorithm is optimized, and the initial position of the ant is placed near the edge, which can significantly improve the running time of the algorithm, but there is no clear explanation on how to determine the initial position. In this paper, Sobel operator is used to determine the specific position of the edge, and then the ant colony algorithm is used for edge detection. Finally, experiments show that this method can effectively extract the complete edge contour.

Analysis of Sobel operator edge detection
There are many algorithms for edge detection, each has its own advantages and disadvantages, and Sobel operator is the most commonly used edge detection operator in one step algorithm.

Traditional Sobel edge detection algorithm
The traditional Sobel operator uses two templates in horizontal and vertical directions for edge detection. It takes a short time to process the image because of the simple formula. The operator contains two groups of 3 * 3 matrices, and the convolution template is shown in Figure 1:

Improved Sobel edge detection algorithm
Sobel operator approximately estimates the edge of the image by calculating the gradient of image brightness, so only considering the horizontal direction and vertical direction will make the detected edge image lose some details. Many scholars propose to increase the direction of 45 ° and 135 ° to make the algorithm more accurate for the extraction of oblique edge features. The improved convolution templates in four directions are shown in Figure 2: After each pixel of the image is convoluted with the convolution template, the pixel value corresponding to the center of the convolution template is replaced by the maximum value of the convolution result, and all the pixels that have been convoluted are merged into a new gray image. the most important thing to extract the contour in edge detection is to determine the value of threshold T. the threshold of Sobel operator refers to the = + obtained from the partial derivative calculation of each pixel, and the edge is obtained by thresholding the grad. Pixels larger than the threshold will be displayed, and smaller than the threshold will no longer be displayed, so the selection of T will determine whether the image will lose some edges or leave more noise. Because the weight is added to Sobel operator, the result will result in a wider edge. Therefore, in order to get a better threshold T, Otsu can be used to find out the gray histogram of the image, and then use this threshold to demarcate the respective variances on both sides of the boundary, and then calculate the absolute value of the difference between the two variances. When the absolute value is the maximum, the boundary gray level is the threshold gray level. The variance formula of foreground and background image is: The proportion of foreground points to the image is 0 , and the average gray level is 0 ; the proportion of background points to the image is 1 , the average gray level is 1 , the total average gray level of the image is , and the inter class variance is g. Finally, the threshold T with the largest variance between classes can be obtained by traversing. Although the improved Sobel operator can effectively improve the effect of edge detection, it is still unable to extract continuous and complete edges.

Using ant colony algorithm to optimize
Ant colony algorithm (ACO) is a probabilistic algorithm for finding the optimal path in graphs. It was proposed by Dorigo M. in 1991 [5]. After setting the initial point, each ant begins to search for food in different places, and releases pheromones in the surrounding environment. After finding the food, it will attract more and more ants. At the same time, they will take different routes until they find the shortest path.
If ant colony algorithm is applied to edge detection, the initial position of ants should be placed near the edge of the image detected by the improved Sobel algorithm. According to the strength of fixed-point pheromone and the probability calculated by heuristic guidance function, each ant will choose one of eight directions as the next position to move. After many iterations, most of the ants will move and concentrate on the edge of the image, which further improves the continuity of the image edge. The specific process is as follows:

Establish mathematical model
Set the image to be detected as gray image I (i , j), and the size is N * M. The vertex represents the specific position of the ant, and the edge represents the path the ant needs to go.

Initialization process
Ants are randomly placed near the edge of the image detected by the improved Sobel operator to initialize the parameters and the initial pheromone values on the pixel. Since only one ant can be placed in each pixel, N * M ants can be placed at most. The more ants are placed, the faster the search will be, but at the same time, it will also bring negative effects, which makes the number of iterations become more and the time-consuming is slower. Therefore, √ * ants are usually selected.

The choice of ant path
Each ant can choose one of the eight directions as the specific position of the next step, and the selected probability value is obtained according to Let the current position of the kth ant be ( , ) , ( , ) ( ) denotes the intensity of pheromone left by ants on vertex j after t iterations, ( , ) denotes the heuristic guidance information strength on vertex i. Since the algorithm is used to optimize Sobel operator, the heuristic guidance information intensity function is set as the gray gradient value at the vertex ( , ) expressed as follows:

Update of pheromone
Every time an ant moves a pixel, it must update its vertex pheromone intensity. The updating rule on vertex (I, J) is based on Formula 4 where is the pheromone volatilization rate. If there is no ant to select this vertex, the pheromone of this vertex will gradually volatilize over time.

Termination conditions
When the ant has completed the predetermined number of steps, the whole algorithm is over. The number of iterations depends on the number of ants placed and the size of the image pixels. The larger the number of ants placed, the smaller the number of iterations set, and the larger the size of image pixels, the more iterations need to be set.

Results and analysis
Based on the improved Sobel algorithm, the experiment was carried out with MATLAB 2016b under the environment of inteli7-4710HQ 2.5GH processor, 8GB memory and operating system of windows10. The comparison between the traditional Sobel edge detection and the improved Sobel edge detection is shown in Fig. 3. It can be seen from the figure that the traditional Sobel algorithm only uses the horizontal and vertical convolution templates, so the detection of diagonal is weak. The improved Sobel algorithm can get a more complete edge of the can. At the same time, the Otsu algorithm is used to adapt the threshold T, so the middle pull ring becomes clearer.
According to the comparative study of the parameters of ant colony algorithm, when the parameters of ant colony algorithm are set as α = 1, β = 0.01, ρ = 0.01, the best detection effect is obtained and the edge pixels are wider [6]. Due to the large image pixels, the iteration number is set to 300 times in order to control the running time of the algorithm. The comparison between the traditional ant colony algorithm and this algorithm is shown in Fig.  4.
(a) Ant Colony algorithm (b) the algorithm in this paper The results show that the image edge effect of the traditional ant colony algorithm is poor after 300 iterations because the initial position of ants is randomly placed. However, the edge obtained by the improved Sobel algorithm is used as the initial position of the ant, and the image edge is better than the traditional algorithm. The only drawback is that the ant colony algorithm needs a lot of iterations, so the running speed of the algorithm is obviously slow. The time required for each algorithm is shown in Table 1. It can be seen from the data that the improved Sobel algorithm is changed from the original two direction convolution template to the four direction convolution template and the Otsu algorithm is added. Therefore, the time required for the improved Sobel algorithm is slightly longer than that of the traditional Sobel algorithm.This algorithm combines the improved Sobel algorithm and ant colony algorithm, so the time of the algorithm is slightly longer than the traditional ant colony algorithm, but it can achieve better results.

Conclusion
In this paper, aiming at the shortcomings of traditional Sobel edge detection algorithm and ant colony algorithm, a new algorithm combining improved Sobel algorithm and ant colony algorithm is proposed. Two diagonal detection directions are added in the original horizontal and vertical directions to improve the detection effect of oblique edge. Then the edge detected by Sobel algorithm is used as the initial position of ant colony algorithm. The experimental results show that if the algorithm is applied to defect detection, the iteration times of ant colony algorithm and image pixels can be reduced according to the actual situation of