Surface Defect Detection of Seals Based on K-Means Clustering Algorithm and Particle Swarm Optimization

As an important part of automobile, the quality and safety of automobile engine high-pressure oil circuit seal parts are an important indicator of the manufacturer’s production process. In order to improve the detection accuracy and efficiency of seal parts in the traditional production process, the defect detection method on the surface of the seal was studied. A K-Means clustering image segmentation algorithm based on particle swarm optimization was proposed. To detect the surface defects of seals, first, preprocess the seal image. ,en, use the SURF algorithm to extract the feature points of the seal image. Finally, according to the particle swarm fitness variance function, select the insertion point calculated by combining particle swarm optimization and K-Means algorithm. ,rough iteration, optimize the initial clustering center of K-Means algorithm. ,e efficiency of K-Means algorithm clustering iteration is improved. ,e test verifies the applicability of the algorithm in the actual process, and it can be used to accurately detect seals. Experimental results show that the detection accuracy rate reaches 98%, which is highly applicable to the actual production.


Introduction
As an important part of the automobile engine oil circuit, the high-pressure oil circuit seal of the automobile engine is very important for the entire driving process of the automobile to ensure the integrity of the seal. In the production process, it is of great significance to ensure the accuracy and efficiency of seal defect detection. With the development of industrial automation technology, the application range of machine vision technology has been continuously expanded, and it has achieved greater application and development in the application of industrial product inspection [1]. is article takes automotive engine high-pressure oil circuit seals as the research object and aims at the disadvantages of traditional observer visual inspection, inspector touch, and surface whetstone polishing stampings, which are labor-intensive, time-consuming, low detection accuracy, and efficiency [2]. A machine vision-based surface defect detection system for high-pressure oil circuit seals of automobile engines is established.
Surface defect detection based on machine vision is realized by pattern recognition of surface defect characteristic parameters through image processing algorithm. Pattern recognition includes supervised and unsupervised learning modes. e image features are classified and recognized by constructing a classifier. Zhang Hongjie et al. [3] used principal component analysis to eliminate the crosscorrelation between image features. Build a solder joint quality classifier based on minimum risk Bayesian image recognition technology. Effectively evaluate the solder joint quality. In order to improve the reliability and stability of traditional hub defect detection based on X-ray image, Wang and Zhang [4] proposed an automatic hub defect recognition algorithm based on improved fuzzy pattern classification. Classify and grade hub defects. Wiltschi et al. [5] tested the surface quality of steel plate image based on the minimum distance. Pernkopf [6] constructed Bayesian network classifier. e likelihood calculation is completed by the coupled hidden Markov random field. e detection of billet surface defects is realized.
rough the collection and analysis of the seal image data samples, it is found that, during the production process in the factory, the process technology and the cutting and handling of the parts will cause defects such as scratches and melting points on the surface of the seal. Starting from the feature extraction, defect classification, and detection algorithm of the surface defects of the seal, this paper proposes a K-Means clustering image segmentation algorithm based on particle swarm optimization to detect surface defects of seals.

Design of the Visual Inspection System
e seal visual defect detection system described in this article mainly includes three modules: image acquisition, image processing, and image display. e functions of each module are shown in Figure 1. e main operation process of the inspection system is to collect the digital image of the seal through the image acquisition module and store the image in the computer memory through the conversion of the photoelectric signal to obtain the real-time image of the target part.
en the image is processed by the image processing software in the image processing module to detect the integrity of the target seal parts. Finally, through the image display module, the defect characteristics of the seal and the result image of the inspection are displayed on the human-computer interaction interface.
e specific experimental process is as follows: (1) Image acquisition: the first is the construction of the experimental platform. e selected camera and light source are fixed through the bracket, and the height of the camera and the illumination angle of the light source are adjusted according to the designed lighting scheme, so that the camera can collect a clear seal within the field of view. rough the host computer processor, save the collected image data.
(2) Image processing: the image algorithm processing process of automobile engine oil circuit seals includes image preprocessing, image target area feature extraction, and defect area target segmentation. is paper analyzes the characteristics of the seal itself and the characteristics of surface defects and uses the operations of extracting RGB image components, image filtering, and background removal to achieve image preprocessing operations. After that, the SURF (Speeded-Up Robust Features) algorithm is used to extract the feature point data of the image surface, and the initial coordinate data set is established. According to the extracted image feature data, the initial center is optimized by the particle swarm optimization algorithm, and the image segmentation is realized by the K-Means clustering algorithm. (3) Image display: in the process of experimental image acquisition, combined with the SDK development kit that comes with the camera, a human-computer interaction interface based on MFC is established, which displays the seal image under the camera lens in real time and can display the processed seal image and the processing result, to facilitate the statistics and analysis of experimental results. Figure 2 shows the structure diagram of the image acquisition system of the visual inspection system in this article and the physical picture of the laboratory.

Design of the Optical Scheme for Surface Defect Detection.
e related hardware content design of the laboratory lighting scheme include the selection of cameras, lenses, and light sources. e specific design principles of the lighting scheme will be introduced below based on optical principles. Figure 3 shows the optical path principle diagram of the scratches on the surface of the seal and the melting point defect under the laboratory ring light.
In the schematic diagram in Figure 3 on the left, position A in the figure is the defect position, which is recessed downward for the complete surface. As shown in the route of light path 2 in the figure, the incident light enters the defect area, and there is a pit at A, making the incident angle change and enter the area of the camera lens photosensitive chip, and it receives a stronger signal and the brightness is stronger. e scratches on the surface of the collected seals have high brightness and obvious defect features. For the schematic on the right, point B in the figure is the melting point defect area. After being illuminated by the ring light source shown in the figure, the melting point of the defect in the image can make the light path reflect vertically into the lens area, and the melting point area is brighter.

Image Preprocessing.
For seals under experimental illumination, light reflection is easily caused by the influence of their own metal properties, forming a bright area on the surface of the seal, which affects the detection of defect features. erefore, the pixel value of the seal image is first transformed through the image inversion, and then the B color component of the RGB image of the seal is extracted to highlight the two defect features on the surface of the seal. e result is shown in Figure 4. In order to effectively extract the characteristic information of the surface of the seal image, it is necessary to perform further filtering processing on the seal image to   Scientific Programming reduce the noise information introduced by the environment and the equipment itself as much as possible and obtain high-quality sample images. Comparing the advantages and disadvantages of several commonly used filtering operations, such as two linear filtering methods, mean filtering, and Gaussian filtering, the filtering operation of the target image is realized through the principle of averaging, which is easy to filter out the boundary information of the image. But it is not conductive to the image feature information extraction [7]. erefore, consider the use of a nonlinear median filtering method. e filtered image is shown in Figure 5.
Generally, the quality of image filtering is evaluated by calculating the similarity before and after image processing. e parameters commonly used in image similarity calculation include mean absolute error (MAE), root mean square error (MSE), normalized mean square error (NMSE), signalto-noise ratio (SNR), and peak signal-to-noise ratio (PSNR) [8].
is paper compares the filtering effects of the two templates of median filtering by calculating the values of the root mean square error (MSE), peak signal-to-noise ratio (PSNR), and signal-to-noise ratio (SNR) before and after image processing. e root means square error (MSE) calculation formula of the image is shown as follows: where f(i, j) and g(i, j), respectively, represent the original image before and after filtering and the image to be evaluated. M and N, respectively, represent the length and width of the two images before and after processing, and the length and width of the two images. e dimensions are the same. e formula for calculating the peak signal-to-noise ratio (PSNR) is as follows: Q is the gray level of the image pixel, generally 255; n is the binary digits used by a pixel, generally 8 bits.
e signal-to-noise ratio (SNR) formula is as follows: Here, f(x, y) is the sum of the original image g(x, y) and the noise signal e(x, y) the MSE, PSNR and SNR of Scratches and Melting point is shown in Table 1.
Compare the calculation results in the table, where the root mean square error calculates the mean square value of the image before and after the filtering process, and judge the degree of distortion of the processed image from this. e lower the root mean square error value, the smaller the difference between the corresponding two images, and the less the distortion of the image. e peak signal-to-noise ratio is calculated by the ratio of the maximum signal amount of the image to the noise intensity. e higher the value of the peak signal-to-noise ratio [9], the closer the quality between the two images before and after filtering. e signal-to-noise ratio is used to compare the qualities of the two images before and after the filtering operation. e larger the value of the signal-to-noise ratio, the better the image quality after filtering. erefore, this paper selects a filter template with a size of 3 × 3 to filter the seal image.

Feature Extraction of the Seal Image Surface.
e SURF algorithm is a local feature point detection and description algorithm, proposed by Herbert Bay. SURF is an improvement on the SIFT (Scale-Invariant Feature Transform) algorithm proposed by David Lowe in 1999. It improves the execution efficiency of the algorithm and provides the algorithm for its application in real-time computer vision systems [10,11]. e basic steps of the SURF algorithm for extracting image feature points are the same as the SIFT algorithm, but the SURF algorithm proposes an improvement to the SIFT algorithm feature extraction and description method and adopts a more efficient method. e algorithm implementation process is as follows.

Construction of the Hessian Matrix to Generate the Feature Points of the Image.
e construction process of the Hessian matrix is equivalent to the Gaussian convolution in the SIFT algorithm. e purpose is to generate a relatively stable feature point in the target image to prepare for the feature extraction below. e function is similar to that of Laplacian and Canny edge detection. Edge Features. e Hessian matrix is a square matrix composed of the secondorder partial derivatives of a multivariate function. It describes the local curvature of the function. It was proposed by the German mathematician Ludwin Otto Hessian in the 19th century [12,13].
Set an input digital image f(x, y), and its Hessian matrix is as follows: Construct a Hessian matrix for the image after Gaussian filtering and convolution, which is expressed as When the discriminant of the Hessian matrix takes the local maximum, it means that the current point takes the extreme value among the pixels in the surrounding neighborhood, and the coordinates of the image feature points can be located. In the description of discrete digital images, the 4 Scientific Programming first derivative is defined as the gray level difference of adjacent pixels: e second derivative is the second derivative of the first derivative: On the other hand, look at the discriminant of the Hessian matrix, which is actually the second-order partial derivative in the horizontal direction of the current point multiplied by the second-order partial derivative in the vertical direction and then the quadratic of the horizontal and vertical second-order partial derivatives of the current point: In order to improve the calculation speed, the Gaussian filter in the SIFT algorithm is approximately replaced by a box filter in the SURF algorithm, and a weighting coefficient ω is added to balance the error caused by the box filter approximation [14]. e value of ω is approximately 0.9.
e schematic diagrams of Gaussian filter and box filter are shown in Figure 6. e pixel value of each color area is shown in the figure.

Construction of Scale Space.
Expressed by the algorithm pyramid, in the SIFT algorithm, the original image is continuously Gaussian smoothing and downsampling, and the Gaussian blur coefficient is gradually increasing. In the SIFT algorithm, the image of the next layer is highly dependent on the image of the previous layer, and the size of the image needs to be reset to half of the previous layer, but the image size in the same layer is the same, which makes the algorithm process more computationally expensive. SURF is the opposite. While keeping the original image unchanged, the template size and blur coefficient of the box filter gradually increase, as shown in Figure 7.

Precise Positioning of Feature Points.
e steps of locating feature points in SURF algorithm and SIFT algorithm are the same. Add preset extreme points to filter feature points, and discard smaller feature points. In the detection process, the pixels processed by the Hessian matrix are compared with the remaining points in the own scale layer and the points in the upper and lower scale layers to   Scientific Programming realize the preliminary positioning of the key points, and then screen the wrong and weaker key points, and get the final feature point. Figure 8 shows the detection principle of the 3 × 3 filter.

Determination of the Main Direction.
In order to have rotation invariance, each feature point needs to be assigned a reproducible direction [15]. e SIFT algorithm uses the histograms of the directions near the feature points and selects the largest and occupies a larger proportion of the directions as the main direction. e SURF algorithm first counts the total amount of characteristic responses of Haar wavelets in the horizontal and vertical directions of all feature points in the 60-degree fan-shaped area, then rotates the fan-shaped area to count the characteristic responses of the Haar wavelet in the entire circular area, and finally selects the longest in the fan-shaped direction. e vector direction locates the main direction of the feature point [16]. e whole process is shown in Figure 9.
Here, dx represents the sum of Haar wavelet features in the horizontal direction; |dx| represents the sum of absolute values of Haar wavelet features in the horizontal direction; dy represents the sum of Haar wavelet features in the vertical direction; dy represents the sum of absolute value of Haar wavelet features in the vertical direction [17]; and Vi represents the vector of each subregion. Since the square area contains 4 * 4 subareas, the feature descriptors of SURF have a total of 4 * 4 * 4 dimensional vectors, as shown in Figure 10. e feature points on the surface of the seal are extracted by the SURF algorithm, as shown in Figure 11. Figure 11(a) is the origin image of seal with scratch, Figure 11(b) is the feature of scratch, Figure 11    Scientific Programming coordinate values of the feature points in the image can be obtained. Provide numerical values for the following particle swarm optimization, and select the initial clustering center of K-Means clustering image segmentation.

K-Means Clustering Algorithm.
Under the experimental lighting of the seal, the pixel distribution on the surface of the collected seal image is uneven. Compared with the image segmentation method based on threshold and edge, the K-Means algorithm is a clustering algorithm based on partition, which calculates the distance between clustering objects. e Euclidean distance judges the similarity between cluster objects [18,19]. e larger the Euclidean distance, the higher the similarity between cluster objects, and the easier it is to be classified into the same category; otherwise, the opposite is true. erefore, this paper uses K-Means clustering and segmentation to realize the detection of the surface defect area of the seal. Generally speaking, the K-Means algorithm process is as follows.
If we define the input sample set D � x 1 , x 2 , . . . x m , the number of clusters is k, and the maximum number of iterations is N; the output is the class division C � C 1 , C 2 , . . . C k : (1) Randomly select k samples from the sample data set D as the initial k cluster center vectors: μ 1 , μ 2 , . . . , μ k . (2) For n � 1, 2, ..., N: (1) Initialize the class division C as C t � ∅，t � 1, 2, . . . k.
(2) Let i � 1, 2, ..., m, calculate the distance between sample x i and each cluster center vector μ j (j � 1, 2, . . . k): Mark the sample x i with the smallest distance from the cluster center as d ij , and its corresponding category is λ i , and update the output category by (3) Let j � 1, 2, ..., k, and divide all sample points in the output class C j by formula (13) to recalculate new cluster centers: (4) If the cluster center vectors of all k samples have not changed, repeat step (3). Mark the sample x i with the smallest distance from the cluster center as d ij , and its corresponding category is λ i , and update the output category by (3) Let j � 1, 2, ..., k, and divide all sample points in the output class C j by formula (13) to recalculate new cluster centers:  (13) to recalculate new cluster centers: (4) If the cluster center vectors of all k samples have not changed, repeat step (3).
(3 )Output the result of class division: According to the process of the K-Means algorithm, the clustering process of the K-Means algorithm is easily affected by the initial cluster center, and it is easy to fall into the local optimum during the clustering iteration process. In order to improve the shortcomings of the K-Means algorithm, this paper introduces a particle swarm optimization algorithm to optimize the initial center and iterative process of the K-Means algorithm to improve the clustering accuracy and computational efficiency of the original algorithm.

Particle Swarm Optimization Algorithm.
e basic idea of the particle swarm optimization algorithm is to simulate the migration and gathering behavior of a flock of birds in the process of foraging [20,21]. Each individual in the flock of birds is regarded as a particle (particles have only two attributes: speed and position). e collection of individuals is the population. e specific calculation process is as follows: (1) Initialization: at the beginning of the algorithm, each particle is given a random initial position and velocity to form an initial population. (2) Find the individual extreme value and the global optimal solution: calculate the fitness value of each particle according to the defined fitness function, and 8 Scientific Programming filter out a global value by comparing the fitness value corresponding to the historical best position, which is called the current iteration number, the optimal solution. en, the iterative calculation process is repeated, compared with the previously recorded global optimal value, and the individual extreme value and the global optimal solution of the search space are updated.
(3) e formula for speed and position update is as follows: Here, ω(ω ≥ 0) represents the inertia factor. When the value of ω is large, the local optimization ability and global optimization ability of the algorithm are stronger; when the value of ω is small, the local optimization ability of the algorithm is stronger. e global optimization capability is the opposite. erefore, the local optimization performance and global optimization performance of the algorithm flow can be adjusted by adjusting the size of ω. C 1 and C 2 represent acceleration constants, C 1 represents the individual learning factor of each particle in the search space, and C 2 represents the social learning factor of each particle in the search space. Generally, take C 1 � C 2 ∈ [0，4]. Random (0,1) represents a random number on the interval [0,1], P best id represents the d-th dimension of the individual extreme value of the i-th variable, and gbest d represents the d-th dimension of the global optimal solution. V id and X id , respectively, represent the d-th dimension component of the flight velocity vector and position vector of the iterated particle i.
Termination conditions are as follows: (1) Reach the set number of iterations.
(2) e difference between the algebras satisfies the minimum limit

K-Means Clustering Algorithm Based on Particle Swarm
Optimization. Comparing the calculation processes of the above two intelligent algorithms, in order to achieve a reasonable combination of the two algorithm principles to achieve efficient segmentation and recognition of seal images, a K-Means clustering image segmentation method based on particle swarm optimization is proposed. e algorithm principles are as follows. First, by extracting the feature points on the surface of the seal image, the coordinate data of the feature points on the surface of the seal image is obtained, and the coordinates of all the feature points are regarded as a data set. e advantage of the particle swarm optimization algorithm is that it can search for the global optimal value in the entire image space. e particle swarm algorithm is used to optimize the calculation of the seal image feature coordinate data set, and the optimal value of the coordinate data of the surface defect area of the seal can be obtained, and the feature point data coordinate closest to the optimal solution is selected as the initial of the K-Means clustering algorithm. For the cluster center, the number of cluster categories is determined by the number of optimal solutions after the final optimization. At the same time, the clustering iteration process of the K-Means algorithm improves the shortcomings of the slower convergence speed of the particle swarm algorithm in the later stage and improves the computational efficiency of the algorithm in the later stage [20]. According to the relevant references, the particle swarm algorithm does not need to perform clustering operations during the global search process. It is necessary to start the clustering process of the K-Means algorithm when the particle swarm algorithm reaches the convergence of the initial data processing and start the local search. Among them, the starting point of particle swarm algorithm convergence needs to be judged based on the overall change of the fitness of all particles. e criterion function for evaluating the clustering effect of the K-Means algorithm is used as the fitness function for evaluating the particle position performance of the particle swarm optimization algorithm, and the fitness value is used to determine the quality of the k clustering centers corresponding to the particle position in the particle swarm optimization algorithm [22]. e fitness function f(x) can be defined as Here, f(x) represents the value of the fitness function when the particle position is x, C j (1 ≤ j ≤ k) represents the jth class in the clustering calculation process, Z j represents the cluster center of the cluster category C j , and X i represents the data sample in the feature data set. e smaller the fitness value, the tighter the combination of the data sample objects within the class, and the better the effect of the clustering algorithm. When the particle i is in the t + 1th iteration, if f(x i (t + 1)) < f(p best i (t)), then p best i (t + 1) � x i (t + 1); otherwise, p best i (t + 1) � p best i (t). If min f(p best i (t + 1)) < f(G best (t)), then G best (t + 1) � p best min (t + 1); otherwise, G best (t + 1) � G best (t).
e overall fitness variance is defined as Here, n is the number of particles; f i is the fitness value of the i-th particle; f avg is the current average fitness of the particle swarm. When σ 2 < m < m, m is a certain threshold (20-30), indicating that the particle swarm optimization algorithm has entered the convergence stage and can start to execute the subsequent K-Means clustering algorithm to achieve the target seal defect area segmentation. e processing effect of the seal image is shown in Figure 12.

Comparison of Various Image Segmentation Algorithms.
e image segmentation method proposed in this paper and several common image segmentation algorithms process the seal image as shown in Figure 13. Figure 13(a) is scale morphological image segmentation; Figure 13(b) is histogram threshold segmentation; Figure 13(c) is OTSU threshold segmentation; Figure 13(d) is image segmentation based on neural network; Figure 13(e) is traditional K-Means clustering algorithm image segmentation; Figure 13(f ) shows the K-Means clustering image segmentation method based on particle swarm optimization proposed in this paper. Combining the result image to analyze the advantages and disadvantages of several image segmentation algorithms, the segmentation algorithm proposed in this paper can accurately segment the defect area of the seal image and realize the defect detection of defective seal parts.
According to the comparison of the results of several image segmentation algorithms, different image segmentation algorithms have different advantages and disadvantages [23]. e scale morphology method is more accurate in the division and positioning of the feature area on the surface of the seal. e feature area is segmented obviously, but there are defects such as discontinuity in the segmentation area and more noise points; the histogram threshold segmentation is greatly affected by the threshold value, and the threshold difference is relatively large. Large area segmentation is more obvious, with less noise; OTSU threshold segmentation method extracts the outline of the entire image more complete, but it is not obvious to extract the surface defect feature of the seal, and the threshold range has a greater impact; image segmentation based on neural network has a greater impact on the image details. Feature segmentation is more obvious, which can separate the feature area of the seal surface defect separately, but it is greatly affected by the pixel value and neural network parameter settings, and some features cannot be recognized; the traditional K-Means clustering segmentation algorithm is affected by the initial clustering center. e image segmentation algorithm proposed in this paper uses the particle swarm algorithm to optimize the selected initial clustering center and improves the convergence speed of the later calculation of the particle swarm algorithm through clustering iteration. e algorithm in this paper has good continuity, high integrity, and less noise in the segmentation of the surface feature area of the seal. It can clearly identify the defect area on the surface of the seal.

Analysis of Results
e K-Means clustering segmentation algorithm combined with particle swarm optimization divides the seal image into several independent regions with their own characteristics and uses colors to mark the different regions. Analyze the characteristic parameter attributes of seal defects (rectangularity, dispersion, and aspect ratio), extract the characteristic parameters of different color areas of the image after segmentation through the feature expression function, and screen out the defect characteristic areas of the seal that meet the defect judgment value to achieve defect sealing. e results of the inspection of parts are shown in Figure 14. Principles of image defect classification is shown in Table 2.
After the system platform based on the machine vision defect detection system is built, it is verified through experiments whether the various indicators of the vision detection system in this paper are in line with the actual production. In this test, 50 defect samples of seals, including scratches, melting point, oxidation, and edge contour defects, were selected. e sample test was carried out by mixing several types of defective seal parts. e results are shown in Table 3.
From the inspection results of the samples, the detection accuracy of the visual inspection system in this paper is 98% for the surface defects of the seals. e reasons for the error in the results are as follows: the image data of the collected test samples is poor, and when there are many bright areas on the surface of the seal under the influence of ambient light, it is easy to determine the scratches and melting point and the high light area on the surface of the seal for the defective part.

Conclusion
According to the characteristics of high-pressure oil circuit seals of automobile engines, this paper proposes a K-Means clustering algorithm image segmentation method based on particle swarm optimization for the scratches and melting point defects on the surface of the seals and realizes the image segmentation of the seals. Detection of Defects. By extracting the RGB image color component image of the seal and image filtering, the seal image is preprocessed; after that, the feature points of the seal image are extracted, and the algorithm ideas proposed in this paper are used to realize the identification of the surface defect area of the seal. After testing, the detection accuracy rate reaches 98%, which is highly applicable to actual production.

Data Availability
No data were used to support this study.

Conflicts of Interest
e authors declare that they have no conflicts of interest.