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

Authors: Xiaoguang Li; Juan Zhu; Haoran Shi; Zijian Cong
Journal: Scientific Programming
 (2021)

---

## Abstract

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. Then, 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. Through iteration, optimize the initial clustering center of K-Means algorithm. The efficiency of K-Means algorithm clustering iteration is improved. The 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.


---

## Body

## 1. 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]. This 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. The image features are classified and recognized by constructing a classifier. Zhang Hongjie et al. [3] used principal component analysis to eliminate the cross-correlation 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. The likelihood calculation is completed by the coupled hidden Markov random field. The detection of billet surface defects is realized.Through 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.


## 2. Design of the Visual Inspection System

The seal visual defect detection system described in this article mainly includes three modules: image acquisition, image processing, and image display. The functions of each module are shown in Figure1. The 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. Then 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.Figure 1
The overall architecture diagram of the visual inspection system.The specific experimental process is as follows:(1)
Image acquisition: the first is the construction of the experimental platform. The 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. Through 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. This 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. Figure2 shows the structure diagram of the image acquisition system of the visual inspection system in this article and the physical picture of the laboratory.Figure 2
Schematic diagram and physical image of the image acquisition system.


### 2.1. Design of the Optical Scheme for Surface Defect Detection

The related hardware content design of the laboratory lighting scheme include the selection of cameras, lenses, and light sources. The specific design principles of the lighting scheme will be introduced below based on optical principles. Figure3 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.Figure 3
Schematic diagram of the defect lighting optical path.In the schematic diagram in Figure3 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. The 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.


### 2.2. 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. Therefore, 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. The result is shown in Figure4.Figure 4
B component image of the RGB image. (a) Scratches. (b) Melting point.
(a)(b)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 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]. Therefore, consider the use of a nonlinear median filtering method. The filtered image is shown in Figure 5.Figure 5
The filtered image. (a) Scratches. (b) Melting point.
(a)(b)Generally, the quality of image filtering is evaluated by calculating the similarity before and after image processing. The parameters commonly used in image similarity calculation include mean absolute error (MAE), root mean square error (MSE), normalized mean square error (NMSE), signal-to-noise ratio (SNR), and peak signal-to-noise ratio (PSNR) [8]. This 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.The root means square error (MSE) calculation formula of the image is shown as follows:(1)MSE=1M×N∑i=1M∑j=1Nfi,j−gi,j2,where fi,j and gi,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. The dimensions are the same.The formula for calculating the peak signal-to-noise ratio (PSNR) is as follows:(2)PSNR=10log10Q2M×N∑i=1M∑j=1Nfi,j−gi,j2=10log10Q2MSE=10log102n−1MSE.Q is the gray level of the image pixel, generally 255;n is the binary digits used by a pixel, generally 8 bits.The signal-to-noise ratio (SNR) formula is as follows:(3)SNRdB=10log10∑x=1M∑y=1Nfx,y2∑x=1M∑y=1Nfx,y−gx,y2.Here,fx,y is the sum of the original image gx,y and the noise signal ex,y the MSE, PSNR and SNR of Scratches and Melting point is shown in Table 1.Table 1
Comparison of the effects of the two templates of median filtering.
Defect categoryTemplate sizeMSEPSNRSNRScratches3 × 3291.647123.482222.51175 × 5284.596223.588522.6180Melting point3 × 3100.996728.087727.06365 × 5149.381126.387825.3637Table 2
Principles of image defect classification.
Characteristic parametersClassification rulesClassification resultDispersion and rectangularityDispersion <2.05ScratchesRectangularity <7.17Aspect ratio <6.05Rectangularity and aspect ratioRectangularity <3.56Melting pointAspect ratio <2.28Compare 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. The lower the root mean square error value, the smaller the difference between the corresponding two images, and the less the distortion of the image. The peak signal-to-noise ratio is calculated by the ratio of the maximum signal amount of the image to the noise intensity. The higher the value of the peak signal-to-noise ratio [9], the closer the quality between the two images before and after filtering. The signal-to-noise ratio is used to compare the qualities of the two images before and after the filtering operation. The larger the value of the signal-to-noise ratio, the better the image quality after filtering. Therefore, this paper selects a filter template with a size of 3 × 3 to filter the seal image.


### 2.3. Feature Extraction of the Seal Image Surface

The 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]. The 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. The algorithm implementation process is as follows.


#### 2.3.1. Construction of the Hessian Matrix to Generate the Feature Points of the Image

The construction process of the Hessian matrix is equivalent to the Gaussian convolution in the SIFT algorithm. The purpose is to generate a relatively stable feature point in the target image to prepare for the feature extraction below. The function is similar to that of Laplacian and Canny edge detection.Edge Features. The Hessian matrix is a square matrix composed of the second-order 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 imagefx,y, and its Hessian matrix is as follows:(4)Hfx,y=∂2f∂x2∂2f∂x∂y∂2f∂x∂y∂2f∂y2.Construct a Hessian matrix for the image after Gaussian filtering and convolution, which is expressed as(5)Hx,σ=Lxxx,σLxyx,σLxyx,σLyyx,σ.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 first derivative is defined as the gray level difference of adjacent pixels:(6)Dx=fx+1,y−fx,y.The second derivative is the second derivative of the first derivative:(7)Dxx=fx+1,y−fx,y−fx,y−x−1,y=fx+1,y+fx−1,y−2×fx,y.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:(8)detH=Dxx×Dyy−Dxy×Dxy.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]. The value of ω is approximately 0.9.(9)detH=Dxx×Dyy−0.9×Dxy2.The schematic diagrams of Gaussian filter and box filter are shown in Figure6. Figure 6(a) shows the value of the second derivative of the Gaussian filter template in the vertical direction of the image Dyy and Dxy. Figure 6(b) shows the result of the approximate replacement of the box filter. The pixel value of each color area is shown in the figure.Figure 6
Schematic diagram of Gaussian filter and box filter. (a) Gaussian filter. (b) Box filter.


#### 2.3.2. 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 Figure7.Figure 7
SIFT and SURF algorithm pyramid.


#### 2.3.3. Precise Positioning of Feature Points

The 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 realize the preliminary positioning of the key points, and then screen the wrong and weaker key points, and get the final feature point. Figure8 shows the detection principle of the 3 × 3 filter.Figure 8
Feature point positioning.


#### 2.3.4. Determination of the Main Direction

In order to have rotation invariance, each feature point needs to be assigned a reproducible direction [15]. The 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. The 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. The vector direction locates the main direction of the feature point [16]. The whole process is shown in Figure 9.Figure 9
Determination of the main direction of feature points.


#### 2.3.5. SURF’s Feature Point Descriptor

First divide a rectangular area with a feature scale of 20 in the feature area, rotate it to the main direction of the feature point, then divide the rectangular area into 4 × 4 subareas along the main direction, and use the Haar template with a scale of 2 to calculate each subarea. Wavelet response values are within the range. Count the wavelet response values of∑dx,∑dx,∑dy,∑dy as the feature vector of the subregion.(10)Vi=∑dx,∑dx,∑dy,∑dy.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.Figure 10
The structure of the SURF feature descriptor.The feature points on the surface of the seal are extracted by the SURF algorithm, as shown in Figure11.Figure 11
Image feature point extraction based on SURF algorithm.
(a)(b)(c)(d)Figure11(a) is the origin image of seal with scratch, Figure 11(b) is the feature of scratch, Figure 11(c) is the original image of seal with melting point, and Figure 11(d) is the feature of melting point. From the experimental result image, it can be seen that the feature points of the image area can be effectively extracted by the SURF algorithm, and the 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.


## 2.1. Design of the Optical Scheme for Surface Defect Detection

The related hardware content design of the laboratory lighting scheme include the selection of cameras, lenses, and light sources. The specific design principles of the lighting scheme will be introduced below based on optical principles. Figure3 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.Figure 3
Schematic diagram of the defect lighting optical path.In the schematic diagram in Figure3 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. The 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.


## 2.2. 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. Therefore, 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. The result is shown in Figure4.Figure 4
B component image of the RGB image. (a) Scratches. (b) Melting point.
(a)(b)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 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]. Therefore, consider the use of a nonlinear median filtering method. The filtered image is shown in Figure 5.Figure 5
The filtered image. (a) Scratches. (b) Melting point.
(a)(b)Generally, the quality of image filtering is evaluated by calculating the similarity before and after image processing. The parameters commonly used in image similarity calculation include mean absolute error (MAE), root mean square error (MSE), normalized mean square error (NMSE), signal-to-noise ratio (SNR), and peak signal-to-noise ratio (PSNR) [8]. This 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.The root means square error (MSE) calculation formula of the image is shown as follows:(1)MSE=1M×N∑i=1M∑j=1Nfi,j−gi,j2,where fi,j and gi,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. The dimensions are the same.The formula for calculating the peak signal-to-noise ratio (PSNR) is as follows:(2)PSNR=10log10Q2M×N∑i=1M∑j=1Nfi,j−gi,j2=10log10Q2MSE=10log102n−1MSE.Q is the gray level of the image pixel, generally 255;n is the binary digits used by a pixel, generally 8 bits.The signal-to-noise ratio (SNR) formula is as follows:(3)SNRdB=10log10∑x=1M∑y=1Nfx,y2∑x=1M∑y=1Nfx,y−gx,y2.Here,fx,y is the sum of the original image gx,y and the noise signal ex,y the MSE, PSNR and SNR of Scratches and Melting point is shown in Table 1.Table 1
Comparison of the effects of the two templates of median filtering.
Defect categoryTemplate sizeMSEPSNRSNRScratches3 × 3291.647123.482222.51175 × 5284.596223.588522.6180Melting point3 × 3100.996728.087727.06365 × 5149.381126.387825.3637Table 2
Principles of image defect classification.
Characteristic parametersClassification rulesClassification resultDispersion and rectangularityDispersion <2.05ScratchesRectangularity <7.17Aspect ratio <6.05Rectangularity and aspect ratioRectangularity <3.56Melting pointAspect ratio <2.28Compare 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. The lower the root mean square error value, the smaller the difference between the corresponding two images, and the less the distortion of the image. The peak signal-to-noise ratio is calculated by the ratio of the maximum signal amount of the image to the noise intensity. The higher the value of the peak signal-to-noise ratio [9], the closer the quality between the two images before and after filtering. The signal-to-noise ratio is used to compare the qualities of the two images before and after the filtering operation. The larger the value of the signal-to-noise ratio, the better the image quality after filtering. Therefore, this paper selects a filter template with a size of 3 × 3 to filter the seal image.


## 2.3. Feature Extraction of the Seal Image Surface

The 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]. The 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. The algorithm implementation process is as follows.


### 2.3.1. Construction of the Hessian Matrix to Generate the Feature Points of the Image

The construction process of the Hessian matrix is equivalent to the Gaussian convolution in the SIFT algorithm. The purpose is to generate a relatively stable feature point in the target image to prepare for the feature extraction below. The function is similar to that of Laplacian and Canny edge detection.Edge Features. The Hessian matrix is a square matrix composed of the second-order 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 imagefx,y, and its Hessian matrix is as follows:(4)Hfx,y=∂2f∂x2∂2f∂x∂y∂2f∂x∂y∂2f∂y2.Construct a Hessian matrix for the image after Gaussian filtering and convolution, which is expressed as(5)Hx,σ=Lxxx,σLxyx,σLxyx,σLyyx,σ.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 first derivative is defined as the gray level difference of adjacent pixels:(6)Dx=fx+1,y−fx,y.The second derivative is the second derivative of the first derivative:(7)Dxx=fx+1,y−fx,y−fx,y−x−1,y=fx+1,y+fx−1,y−2×fx,y.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:(8)detH=Dxx×Dyy−Dxy×Dxy.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]. The value of ω is approximately 0.9.(9)detH=Dxx×Dyy−0.9×Dxy2.The schematic diagrams of Gaussian filter and box filter are shown in Figure6. Figure 6(a) shows the value of the second derivative of the Gaussian filter template in the vertical direction of the image Dyy and Dxy. Figure 6(b) shows the result of the approximate replacement of the box filter. The pixel value of each color area is shown in the figure.Figure 6
Schematic diagram of Gaussian filter and box filter. (a) Gaussian filter. (b) Box filter.


### 2.3.2. 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 Figure7.Figure 7
SIFT and SURF algorithm pyramid.


### 2.3.3. Precise Positioning of Feature Points

The 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 realize the preliminary positioning of the key points, and then screen the wrong and weaker key points, and get the final feature point. Figure8 shows the detection principle of the 3 × 3 filter.Figure 8
Feature point positioning.


### 2.3.4. Determination of the Main Direction

In order to have rotation invariance, each feature point needs to be assigned a reproducible direction [15]. The 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. The 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. The vector direction locates the main direction of the feature point [16]. The whole process is shown in Figure 9.Figure 9
Determination of the main direction of feature points.


### 2.3.5. SURF’s Feature Point Descriptor

First divide a rectangular area with a feature scale of 20 in the feature area, rotate it to the main direction of the feature point, then divide the rectangular area into 4 × 4 subareas along the main direction, and use the Haar template with a scale of 2 to calculate each subarea. Wavelet response values are within the range. Count the wavelet response values of∑dx,∑dx,∑dy,∑dy as the feature vector of the subregion.(10)Vi=∑dx,∑dx,∑dy,∑dy.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.Figure 10
The structure of the SURF feature descriptor.The feature points on the surface of the seal are extracted by the SURF algorithm, as shown in Figure11.Figure 11
Image feature point extraction based on SURF algorithm.
(a)(b)(c)(d)Figure11(a) is the origin image of seal with scratch, Figure 11(b) is the feature of scratch, Figure 11(c) is the original image of seal with melting point, and Figure 11(d) is the feature of melting point. From the experimental result image, it can be seen that the feature points of the image area can be effectively extracted by the SURF algorithm, and the 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.


## 2.3.1. Construction of the Hessian Matrix to Generate the Feature Points of the Image

The construction process of the Hessian matrix is equivalent to the Gaussian convolution in the SIFT algorithm. The purpose is to generate a relatively stable feature point in the target image to prepare for the feature extraction below. The function is similar to that of Laplacian and Canny edge detection.Edge Features. The Hessian matrix is a square matrix composed of the second-order 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 imagefx,y, and its Hessian matrix is as follows:(4)Hfx,y=∂2f∂x2∂2f∂x∂y∂2f∂x∂y∂2f∂y2.Construct a Hessian matrix for the image after Gaussian filtering and convolution, which is expressed as(5)Hx,σ=Lxxx,σLxyx,σLxyx,σLyyx,σ.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 first derivative is defined as the gray level difference of adjacent pixels:(6)Dx=fx+1,y−fx,y.The second derivative is the second derivative of the first derivative:(7)Dxx=fx+1,y−fx,y−fx,y−x−1,y=fx+1,y+fx−1,y−2×fx,y.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:(8)detH=Dxx×Dyy−Dxy×Dxy.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]. The value of ω is approximately 0.9.(9)detH=Dxx×Dyy−0.9×Dxy2.The schematic diagrams of Gaussian filter and box filter are shown in Figure6. Figure 6(a) shows the value of the second derivative of the Gaussian filter template in the vertical direction of the image Dyy and Dxy. Figure 6(b) shows the result of the approximate replacement of the box filter. The pixel value of each color area is shown in the figure.Figure 6
Schematic diagram of Gaussian filter and box filter. (a) Gaussian filter. (b) Box filter.


## 2.3.2. 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 Figure7.Figure 7
SIFT and SURF algorithm pyramid.


## 2.3.3. Precise Positioning of Feature Points

The 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 realize the preliminary positioning of the key points, and then screen the wrong and weaker key points, and get the final feature point. Figure8 shows the detection principle of the 3 × 3 filter.Figure 8
Feature point positioning.


## 2.3.4. Determination of the Main Direction

In order to have rotation invariance, each feature point needs to be assigned a reproducible direction [15]. The 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. The 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. The vector direction locates the main direction of the feature point [16]. The whole process is shown in Figure 9.Figure 9
Determination of the main direction of feature points.


## 2.3.5. SURF’s Feature Point Descriptor

First divide a rectangular area with a feature scale of 20 in the feature area, rotate it to the main direction of the feature point, then divide the rectangular area into 4 × 4 subareas along the main direction, and use the Haar template with a scale of 2 to calculate each subarea. Wavelet response values are within the range. Count the wavelet response values of∑dx,∑dx,∑dy,∑dy as the feature vector of the subregion.(10)Vi=∑dx,∑dx,∑dy,∑dy.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.Figure 10
The structure of the SURF feature descriptor.The feature points on the surface of the seal are extracted by the SURF algorithm, as shown in Figure11.Figure 11
Image feature point extraction based on SURF algorithm.
(a)(b)(c)(d)Figure11(a) is the origin image of seal with scratch, Figure 11(b) is the feature of scratch, Figure 11(c) is the original image of seal with melting point, and Figure 11(d) is the feature of melting point. From the experimental result image, it can be seen that the feature points of the image area can be effectively extracted by the SURF algorithm, and the 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.


## 3. Research of Image Segmentation Algorithm

### 3.1. 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. The Euclidean distance judges the similarity between cluster objects [18, 19]. The 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. Therefore, 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 setD=x1,x2,…xm, the number of clusters is k, and the maximum number of iterations is N; the output is the class division C=C1,C2,…Ck:(1)
Randomly selectk samples from the sample data set D as the initial k cluster center vectors: μ1,μ2,…,μk.(2)
Forn = 1, 2, ..., N:(1)
Initialize the class division C asCt=∅，t=1,2,…k.(2)
Leti = 1, 2, ..., m, calculate the distance between sample xi and each cluster center vector μjj=1,2,…k:(11)dij=xi−μj22.(12)Cλi=Cλi∪xi.Mark the samplexi with the smallest distance from the cluster center as dij, and its corresponding category is λi, and update the output category by(13)Cλi=Cλi∪xi.μj=1Cj∑x∈Cjx.(3)
Letj = 1, 2, ..., k, and divide all sample points in the output class Cj by formula (13) to recalculate new cluster centers:(14)μj=1Cj∑x∈Cjx.(4)
If the cluster center vectors of allk samples have not changed, repeat step (3).Mark the samplexi with the smallest distance from the cluster center as dij, and its corresponding category is λi, and update the output category by(15)Cλi=Cλi∪xi.μj=1Cj∑x∈Cjx.(3)
Letj = 1, 2, ..., k, and divide all sample points in the output class Cj by formula (13) to recalculate new cluster centers:(16)μj=1Cj∑x∈Cjx.(4)
If the cluster center vectors of allk samples have not changed, repeat step (3).(3)
Letj = 1, 2, ..., k, and divide all sample points in the output class Cj by formula (13) to recalculate new cluster centers:(17)μj=1Cj∑x∈Cjx.(4)
If the cluster center vectors of allk samples have not changed, repeat step (3).(3
)Output the result of class division:C=C1,C2,…Ck.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.


### 3.2. Particle Swarm Optimization Algorithm

The 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). The collection of individuals is the population. The 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 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. Then, 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)
The formula for speed and position update is as follows:(18)Vid=ωVid+C1random0,1Pbestid−Xid+C2random0,1gbestd−Xid,Xid=Xid+Vid.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. The global optimization capability is the opposite. Therefore, the local optimization performance and global optimization performance of the algorithm flow can be adjusted by adjusting the size of ω. C1 and C2 represent acceleration constants, C1 represents the individual learning factor of each particle in the search space, and C2 represents the social learning factor of each particle in the search space. Generally, take C1=C2∈0，4. Random (0,1) represents a random number on the interval [0,1], Pbestid represents the d-th dimension of the individual extreme value of the i-th variable, and gbestd represents the d-th dimension of the global optimal solution. Vid and Xid, 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)
The difference between the algebras satisfies the minimum limit


### 3.3. 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. The 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. The advantage of the particle swarm optimization algorithm is that it can search for the global optimal value in the entire image space. The 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. The 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]. The fitness function fx can be defined as(19)fx=∑j=1k∑xi∈cjxi−zj.Here,fx represents the value of the fitness function when the particle position is x, Cj1≤j≤k represents the jth class in the clustering calculation process, Zj represents the cluster center of the cluster category Cj, and Xi represents the data sample in the feature data set. The 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 fxit+1<fpbestit, then pbestit+1=xit+1; otherwise, pbestit+1=pbestit. If minfpbestit+1<fGbestt, then Gbestt+1=pbestmint+1; otherwise, Gbestt+1=Gbestt.The overall fitness variance is defined as(20)σ2=−∑i=1nfi−favgf2.Here,n is the number of particles; fi is the fitness value of the i-th particle; favg 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. The processing effect of the seal image is shown in Figure 12. Figures 12(a) and 12(b) are the seals with scratch. Figures 12(c) and 12(d) are seals with melting point.Figure 12
Image segmentation result image.
(a)(b)(c)(d)


### 3.4. Comparison of Various Image Segmentation Algorithms

The image segmentation method proposed in this paper and several common image segmentation algorithms process the seal image as shown in Figure13.Figure 13
Comparison of this algorithm with other image segmentation algorithms.
(a)(b)(c)(d)(e)(f)Figure13(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]. The scale morphology method is more accurate in the division and positioning of the feature area on the surface of the seal. The 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. The 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. The 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.


## 3.1. 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. The Euclidean distance judges the similarity between cluster objects [18, 19]. The 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. Therefore, 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 setD=x1,x2,…xm, the number of clusters is k, and the maximum number of iterations is N; the output is the class division C=C1,C2,…Ck:(1)
Randomly selectk samples from the sample data set D as the initial k cluster center vectors: μ1,μ2,…,μk.(2)
Forn = 1, 2, ..., N:(1)
Initialize the class division C asCt=∅，t=1,2,…k.(2)
Leti = 1, 2, ..., m, calculate the distance between sample xi and each cluster center vector μjj=1,2,…k:(11)dij=xi−μj22.(12)Cλi=Cλi∪xi.Mark the samplexi with the smallest distance from the cluster center as dij, and its corresponding category is λi, and update the output category by(13)Cλi=Cλi∪xi.μj=1Cj∑x∈Cjx.(3)
Letj = 1, 2, ..., k, and divide all sample points in the output class Cj by formula (13) to recalculate new cluster centers:(14)μj=1Cj∑x∈Cjx.(4)
If the cluster center vectors of allk samples have not changed, repeat step (3).Mark the samplexi with the smallest distance from the cluster center as dij, and its corresponding category is λi, and update the output category by(15)Cλi=Cλi∪xi.μj=1Cj∑x∈Cjx.(3)
Letj = 1, 2, ..., k, and divide all sample points in the output class Cj by formula (13) to recalculate new cluster centers:(16)μj=1Cj∑x∈Cjx.(4)
If the cluster center vectors of allk samples have not changed, repeat step (3).(3)
Letj = 1, 2, ..., k, and divide all sample points in the output class Cj by formula (13) to recalculate new cluster centers:(17)μj=1Cj∑x∈Cjx.(4)
If the cluster center vectors of allk samples have not changed, repeat step (3).(3
)Output the result of class division:C=C1,C2,…Ck.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.


## 3.2. Particle Swarm Optimization Algorithm

The 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). The collection of individuals is the population. The 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 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. Then, 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)
The formula for speed and position update is as follows:(18)Vid=ωVid+C1random0,1Pbestid−Xid+C2random0,1gbestd−Xid,Xid=Xid+Vid.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. The global optimization capability is the opposite. Therefore, the local optimization performance and global optimization performance of the algorithm flow can be adjusted by adjusting the size of ω. C1 and C2 represent acceleration constants, C1 represents the individual learning factor of each particle in the search space, and C2 represents the social learning factor of each particle in the search space. Generally, take C1=C2∈0，4. Random (0,1) represents a random number on the interval [0,1], Pbestid represents the d-th dimension of the individual extreme value of the i-th variable, and gbestd represents the d-th dimension of the global optimal solution. Vid and Xid, 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)
The difference between the algebras satisfies the minimum limit


## 3.3. 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. The 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. The advantage of the particle swarm optimization algorithm is that it can search for the global optimal value in the entire image space. The 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. The 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]. The fitness function fx can be defined as(19)fx=∑j=1k∑xi∈cjxi−zj.Here,fx represents the value of the fitness function when the particle position is x, Cj1≤j≤k represents the jth class in the clustering calculation process, Zj represents the cluster center of the cluster category Cj, and Xi represents the data sample in the feature data set. The 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 fxit+1<fpbestit, then pbestit+1=xit+1; otherwise, pbestit+1=pbestit. If minfpbestit+1<fGbestt, then Gbestt+1=pbestmint+1; otherwise, Gbestt+1=Gbestt.The overall fitness variance is defined as(20)σ2=−∑i=1nfi−favgf2.Here,n is the number of particles; fi is the fitness value of the i-th particle; favg 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. The processing effect of the seal image is shown in Figure 12. Figures 12(a) and 12(b) are the seals with scratch. Figures 12(c) and 12(d) are seals with melting point.Figure 12
Image segmentation result image.
(a)(b)(c)(d)


## 3.4. Comparison of Various Image Segmentation Algorithms

The image segmentation method proposed in this paper and several common image segmentation algorithms process the seal image as shown in Figure13.Figure 13
Comparison of this algorithm with other image segmentation algorithms.
(a)(b)(c)(d)(e)(f)Figure13(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]. The scale morphology method is more accurate in the division and positioning of the feature area on the surface of the seal. The 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. The 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. The 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.


## 4. Analysis of Results

The 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. The results of the inspection of parts are shown in Figure14. Principles of image defect classification is shown in Table 2.Figure 14
Defect inspection result image.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. The sample test was carried out by mixing several types of defective seal parts. The results are shown in Table3.Table 3
Test and inspection situation of automobile engine oil circuit seals.
Type of defectNumber of inspectionsNumber of errorsAccuracy (%)Time (s)Scratches501986Melting point501986From 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. The 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.


## 5. 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.



---

*Source: 3965247-2021-10-28.xml*