A method and system for automatically detecting surface defects of a workpiece under test
Patent Information
- Application Number
- CN202611098271.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-07-23
- Publication Date
- 2026-08-18
AI Technical Summary
[0003]本发明提供一种待测工件表面缺陷自动检测方法及系统,以解决现有检测方法中固定尺度处理方式无法适应缺陷多样性,以及多波段融合时易受噪声波段干扰导致缺陷边界模糊和伪缺陷的问题
[0015]Based on the statistical distribution of pixel grayscale values within each image block, a dynamic coding length is assigned to each image block. In this scheme, the grayscale center and grayscale divergence of the image block are calculated, and the dynamic coding cardinality is obtained by combining the variance of pixel grayscale values. Then, the dynamic coding length of the image block is obtained through logarithmic operations. Grayscale divergence characterizes the total deviation of pixel grayscale values from their mean, while variance reflects the degree of dispersion of the deviation. The product of the two can amplify the subtle grayscale structure changes caused by texture destruction or abnormal reflection in defective regions. The use of logarithmic mapping compresses the numerical range of the dynamic coding cardinality, resulting in shorter coding lengths in smooth background regions and longer coding lengths in grayscale disordered regions caused by defects. This coding length allocation is not based on a fixed threshold but is entirely driven by the grayscale statistical characteristics of local image blocks, and can adaptively adjust with the image content. This method transforms the grayscale anomalies of minor defects into significant coding length increments, while large areas of gently changing pseudo-defect regions are assigned short codes similar to the background. The difference in coding length directly quantifies the degree to which the structure of each region differs from a normal surface. When performing layered fusion of coded field maps of surface images from different bands, the coded field maps of all bands are stacked into three-dimensional data blocks in wavelength order. After extracting the field strength sequence along the wavelength dimension at each spatial location, the maximum and minimum values in the sequence are removed, and the median value of the remaining values is calculated as the fused field strength value. The field strength value is obtained by taking the square root of the sum of the squares of the differences in the dynamic coding lengths of adjacent image blocks, which reflects the degree of drastic change in coding length in space. Different bands have significantly different sensitivities to workpiece surface materials, coatings, and various defects. Extreme field strength values in a certain band due to specular reflection or material absorption characteristics often do not represent true defect edge information. By removing the extreme values at both ends of the sequence, interference caused by abnormal saturation or complete absence of response in a specific band at a local location is eliminated, preventing it from dominating the fusion result. The median value is taken as the final representation of that spatial location, making the fused field strength value approach the stable response of the coded edge commonly indicated by most bands. This fusion strategy suppresses the weight of single-band noise or reflection artifacts in the final defect identification, and while preserving the consistency of real defect edges, it significantly reduces the pseudo-defect edge response introduced by non-uniform interference between bands.
Smart Images

Figure CN122591681A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of workpiece surface defect detection technology, specifically to an automatic detection method and system for surface defects of a workpiece. Background Technology
[0002] In industrial automated production, defect detection on workpiece surfaces is a crucial aspect of quality control. Machine vision-based automated inspection technology is gradually replacing manual visual inspection and becoming the mainstream solution. Traditional single-band grayscale or color image detection methods rely on the contrast difference in brightness or color between the defect and the background. When the workpiece surface material is complex, or contains oil stains, watermarks, or texture interference, the visible features of the defect are easily obscured, leading to frequent missed or false detections. Multispectral imaging technology provides a way to address the problem of insufficient information from a single band by acquiring surface reflection information across multiple bands. However, existing multispectral detection methods typically perform simple pixel-level fusion or band optimization on images from different bands, and then use fixed-scale edge detection or threshold segmentation algorithms to extract defects. This fixed-scale processing approach cannot adapt to the diversity of different types of defects in size, shape, and grayscale distribution. For defects with insignificant grayscale differences, such as microcracks or large-area shallow scratches, it is difficult to simultaneously achieve both detection sensitivity and spatial positioning accuracy. Meanwhile, during multi-band information fusion, the confidence differences in detection results across different bands are not adequately measured. Simple weighting or voting strategies can easily amplify the influence of noisy bands, leading to blurred defect boundaries or a large number of false defects. To address these issues, there is an urgent need for a method that can adaptively characterize the degree of defect grayscale distribution anomalies within a single band and effectively suppress inter-band noise interference during multi-band information fusion. Summary of the Invention
[0003] This invention provides an automatic detection method and system for surface defects of a workpiece, which solves the problems of existing detection methods where fixed-scale processing cannot adapt to the diversity of defects, and where multi-band fusion is easily affected by noise band interference, resulting in blurred defect boundaries and false defects.
[0004] To achieve the above objectives, the present invention provides the following technical solution: The present invention provides an automatic detection method and system for surface defects of a workpiece, which aims to solve the technical problem that existing detection methods are unable to effectively identify minute defects under complex textures or low contrast conditions.
[0005] This method acquires a multispectral image sequence of the surface of the workpiece under test, which contains surface images in at least two different bands. Each band's surface image is decomposed into multiple image blocks. Based on the statistical distribution of pixel grayscale values within each image block, a dynamic coding length is assigned to each image block. Preferably, for any image block, the mean of all pixel grayscale values within that block is calculated as the grayscale center, and the sum of the absolute values of the differences between each pixel's grayscale value and the grayscale center is calculated as the grayscale divergence. This grayscale divergence is multiplied by the variance of the pixel grayscale values within the image block to obtain the dynamic coding cardinality. The logarithm of this dynamic coding cardinality is then taken with respect to the natural constant, rounded, and incremented by one to obtain the dynamic coding length of the image block. This coding length adaptively reflects the complexity of the texture within the image block.
[0006] Based on the difference in dynamic coding length between adjacent image blocks in a surface image of the same band, a coded field map of the surface image of that band is constructed. As a technical solution of this invention, the absolute value of the difference in dynamic coding length between each image block and its left and upper adjacent image blocks is obtained, and the square root of the sum of the squares of the two absolute values is taken as the field strength value of that image block. The field strength values of all image blocks are arranged according to their original spatial positions to form a two-dimensional matrix, which is the coded field map of the surface image of that band. The coded field map can highlight areas with drastic changes in coding length and accentuate the boundary features of defects.
[0007] The coded field maps of surface images from different bands are stacked and fused to obtain a fused coded field map. The coded field maps of all surface images from all bands are stacked into a three-dimensional coded field data block in ascending order of wavelength. For each spatial location in the three-dimensional data block, field strength values are extracted along the wavelength dimension to form a field strength sequence. The median value of this field strength sequence is calculated as the fused field strength value for that spatial location, thus forming the fused coded field map. Preferably, before calculating the median value, the maximum and minimum values in the sequence are removed to improve the robustness of the fusion result. The fused coded field map integrates the performance information of defects under different bands, enhancing the contrast between defects and the background.
[0008] The detection of coding length anomaly regions in the fused coding field map is specifically as follows: Calculate the global mean of all fused field strength values and the absolute value of the deviation between each fused field strength value and the global mean; mark the spatial locations corresponding to the top 5% with the largest absolute deviation values as high anomalies; using each high anomaly as a seed point, employ the four-neighborhood region growing method to merge pixels in the neighborhood whose absolute deviation between the fused field strength value and the seed point's fused field strength value is less than a preset deviation threshold, forming high anomaly regions; remove high anomaly regions with an area smaller than a preset area threshold, and use the remaining high anomaly regions as coding length anomaly regions.
[0009] The detected coded length anomaly regions are mapped back to the surface of the workpiece under test to obtain the initial defect region. Based on the pixel resolution mapping relationship between the multispectral image sequence and the surface of the workpiece under test, the spatial coordinates of the coded length anomaly regions are converted into physical coordinates of the workpiece surface. After merging the surface regions corresponding to all physical coordinates, morphological closing operations are performed to eliminate small voids and connect adjacent regions to form a continuous initial defect region.
[0010] Edge chain codes are extracted from the initial defect region, and individual defect contours are segmented based on the curvature abrupt change points of the edge chain codes. Preferably, the initial defect region is first binarized to obtain a binary defect mask; an eight-neighbor boundary tracking algorithm is used to extract the outer boundary contour, resulting in a sequence of boundary pixels, which are then chain-coded to obtain the edge chain codes. The chain code curvature at each boundary pixel is then calculated, and local maxima of the chain code curvature are used as candidate abrupt change points. The difference in chain code curvature between a candidate abrupt change point and its adjacent candidate abrupt change points is calculated, and candidate abrupt change points with differences greater than a preset curvature difference threshold are identified as curvature abrupt change points. Sub-chain codes are extracted between every two adjacent curvature abrupt change points in the edge chain codes, and the region enclosed by the boundary pixel sequence corresponding to each sub-chain code segment constitutes a single defect contour. This method can accurately separate multiple mutually adhered defects into independent individuals.
[0011] The method of this invention also includes a step of identifying the authenticity of individual defect contours. For each individual defect contour, the aspect ratio of its minimum bounding rectangle and the ratio of the contour area to the area of the minimum bounding rectangle are calculated as the rectangularity. Only when the aspect ratio is greater than a first preset threshold and the rectangularity is less than a second preset threshold is the contour determined to be a false defect contour and discarded. This process effectively filters out shapes that are not true defects, such as forked ends of scratches or edges of stains.
[0012] The method of this invention also includes a step of classifying defect types. For each individual defect contour that is finally confirmed, its Fourier descriptor is calculated, and the low-frequency components are normalized and used as shape feature vectors; at the same time, the mean and variance of the pixel grayscale values within the region enclosed by the contour are calculated as texture feature vectors; the shape feature vector and the texture feature vector are concatenated into a fused feature vector, which is then input into a pre-trained classifier to obtain the defect type label corresponding to the individual defect contour, thus achieving refined defect classification.
[0013] This invention also includes an automatic detection system for surface defects of a workpiece under test. The system includes a memory, a processor, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, it implements the steps of the aforementioned automatic detection method for surface defects of a workpiece under test. This system can automatically execute the entire process of multispectral image acquisition, dynamic encoding, field map construction and fusion, abnormal region detection and mapping, contour segmentation, and defect identification and classification, enabling efficient and accurate automated detection of surface defects in the workpiece under test.
[0014] The technical effects and advantages provided by the present invention in the above technical solution are as follows:
[0015] Based on the statistical distribution of pixel grayscale values within each image block, a dynamic coding length is assigned to each image block. In this scheme, the grayscale center and grayscale divergence of the image block are calculated, and the dynamic coding cardinality is obtained by combining the variance of pixel grayscale values. Then, the dynamic coding length of the image block is obtained through logarithmic operations. Grayscale divergence characterizes the total deviation of pixel grayscale values from their mean, while variance reflects the degree of dispersion of the deviation. The product of the two can amplify the subtle grayscale structure changes caused by texture destruction or abnormal reflection in defective regions. The use of logarithmic mapping compresses the numerical range of the dynamic coding cardinality, resulting in shorter coding lengths in smooth background regions and longer coding lengths in grayscale disordered regions caused by defects. This coding length allocation is not based on a fixed threshold but is entirely driven by the grayscale statistical characteristics of local image blocks, and can adaptively adjust with the image content. This method transforms the grayscale anomalies of minor defects into significant coding length increments, while large areas of gently changing pseudo-defect regions are assigned short codes similar to the background. The difference in coding length directly quantifies the degree to which the structure of each region differs from a normal surface. When performing layered fusion of coded field maps of surface images from different bands, the coded field maps of all bands are stacked into three-dimensional data blocks in wavelength order. After extracting the field strength sequence along the wavelength dimension at each spatial location, the maximum and minimum values in the sequence are removed, and the median value of the remaining values is calculated as the fused field strength value. The field strength value is obtained by taking the square root of the sum of the squares of the differences in the dynamic coding lengths of adjacent image blocks, which reflects the degree of drastic change in coding length in space. Different bands have significantly different sensitivities to workpiece surface materials, coatings, and various defects. Extreme field strength values in a certain band due to specular reflection or material absorption characteristics often do not represent true defect edge information. By removing the extreme values at both ends of the sequence, interference caused by abnormal saturation or complete absence of response in a specific band at a local location is eliminated, preventing it from dominating the fusion result. The median value is taken as the final representation of that spatial location, making the fused field strength value approach the stable response of the coded edge commonly indicated by most bands. This fusion strategy suppresses the weight of single-band noise or reflection artifacts in the final defect identification, and while preserving the consistency of real defect edges, it significantly reduces the pseudo-defect edge response introduced by non-uniform interference between bands. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this invention. For those skilled in the art, other drawings can be obtained based on these drawings.
[0017] Figure 1 This is a flowchart of an automatic detection method for surface defects on a workpiece.
[0018] Figure 2 This is a flowchart of the image block dynamic coding length allocation and coding field map construction process;
[0019] Figure 3 This is a flowchart of multi-band coded field map overlay fusion and anomaly region detection;
[0020] Figure 4 This is a flowchart of the initial defect region acquisition and edge chain code extraction process;
[0021] Figure 5 This is a flowchart of a defect contour segmentation method based on chain code curvature;
[0022] Figure 6 This is a flowchart for identifying the authenticity of defect outlines and classifying defect types;
[0023] Figure 7 It is the defect contour direction coding change curve;
[0024] Figure 8 This is a schematic diagram of the curvature of the defect contour chain code and the curvature abrupt change point. Detailed Implementation
[0025] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0026] See Figure 1 This invention provides an automatic detection method for surface defects of a workpiece, comprising the following steps: acquiring a multispectral image sequence of the workpiece surface, wherein the multispectral image sequence contains surface images of at least two different bands; decomposing each band surface image into multiple image blocks, and assigning a dynamic coding length to each image block according to the statistical distribution of pixel grayscale values within each image block; constructing a coding field map of the band surface image based on the difference in dynamic coding length between adjacent image blocks in the same band surface image; performing layered fusion of the coding field maps of different band surface images to obtain a fused coding field map; detecting coding length aberration regions in the fused coding field map, and mapping the detected coding length aberration regions back to the workpiece surface as initial defect regions; extracting edge chain codes from the initial defect regions, and segmenting individual defect contours according to the curvature abrupt change points of the edge chain codes as the final defect detection result of the workpiece surface.
[0027] Example 1:
[0028] In this embodiment, refer toFigure 2 This paper describes in detail the process of processing each band of the surface image in the multispectral image sequence of the workpiece surface under test, allocating dynamic coding length to the image block and constructing the coding field map.
[0029] A multispectral image sequence is acquired of the surface of the workpiece under test. The multispectral image sequence contains surface images in at least two different spectral bands. For each spectral band surface image, it is decomposed into multiple image blocks. The image blocks can be decomposed using a fixed-size grid, so that the spectral band surface image is divided into several image blocks of the same size that do not overlap. Each image block consists of several rows and columns of pixels.
[0030] For any image patch, perform the following operations to allocate the dynamic coding length for that patch. Calculate the mean of the grayscale values of all pixels within the image patch, and use this mean as the grayscale center of the image patch. The formula for calculating the grayscale center is:
[0031]
[0032] in, This indicates the grayscale center of the image patch. This indicates the total number of pixels contained within the image block. Indicates the first [image block] within this image block The grayscale value of each pixel The pixel number The range of values is from arrive Integers.
[0033] Calculate the difference between the grayscale value of each pixel within the image patch and the grayscale center. Take the absolute value of each difference and sum the absolute values of all differences. The sum is used as the grayscale divergence of the image patch. The grayscale divergence is obtained as follows:
[0034]
[0035] in, This represents the grayscale divergence of the image patch. This indicates the grayscale center of the image patch. Indicates the first [image block] within this image block The grayscale value of each pixel This indicates the operation of taking the absolute value.
[0036] Calculate the variance of the pixel grayscale values within this image patch. The variance is calculated as follows:
[0037]
[0038] in, This represents the variance of the pixel grayscale values within the image block. This indicates the grayscale center of the image patch. Indicates the first [image block] within this image block The grayscale value of each pixel.
[0039] The gray-level divergence of the image patch is multiplied by the variance of the gray-level values of the pixels within the patch; the product is used as the dynamic coding cardinality of the image patch. The dynamic coding cardinality is represented by the symbol... The calculation method is as follows:
[0040]
[0041] in, This represents the grayscale divergence of the image patch. This represents the variance of the pixel grayscale values within the image block.
[0042] Using the natural constant as the base of the logarithm, the logarithm of the dynamic coding radix is taken, rounded down, and then incremented by one to obtain the dynamic coding length of the image patch. The dynamic coding length is represented by the sign language. The calculation formula is as follows:
[0043]
[0044] in, This represents the dynamic coding cardinality of the image patch. Represented by natural constant The natural logarithm function with base 0. Indicates the floor operation. Natural constant. The value is approximately 2.71828. When the dynamic coding radix... When the value is less than or equal to 0, the dynamic encoding length can be directly set. Set to 1. Dynamic encoding length. It is a positive integer that reflects the complexity of the grayscale distribution within the image patch.
[0045] In practice, each image block in the surface image of the same band is traversed in the manner described above, and a corresponding dynamic coding length is assigned to each image block.
[0046] After obtaining the dynamic coding length of all image blocks in the same band surface image, the step of constructing the coded field map of the band surface image is performed.
[0047] For each image patch in a surface image of the same band, obtain the first absolute value of the difference between the dynamic coding length of the image patch and its left adjacent image patch, and obtain the second absolute value of the difference between the dynamic coding length of the image patch and its upper adjacent image patch. If an image patch has no left adjacent image patch, the first absolute value is set to 0; if an image patch has no upper adjacent image patch, the second absolute value is set to 0. The left adjacent image patch refers to the image patch in the image patch array that is in the same row as the current image patch and has a column number one lower than the current image patch. The upper adjacent image patch refers to the image patch in the image patch array that is in the same column as the current image patch and has a row number one lower than the current image patch.
[0048] The square root of the sum of the squares of the first and second absolute values is taken as the field strength value of the image patch. The field strength value is calculated as follows: for coordinates... The image patch, its field strength value Determined by the difference in adjacent code lengths, i.e. ,in, This represents the first absolute value of the difference between the dynamic coding length of this image block and its left-adjacent image block. It represents the second absolute value of the difference between the dynamic coding length of this image block and the image block above it. Indicates the row number of the image patch. Indicates the column number of the image block.
[0049] The field intensity values of all image patches in the surface image of this band are arranged according to their original spatial positions to form a two-dimensional matrix. The number of rows in the two-dimensional matrix equals the number of rows and columns of the image patches in the surface image of this band. This two-dimensional matrix serves as the coded field map of the surface image of this band. The position of each element in the coded field map corresponds one-to-one with the position of an image patch in the surface image of this band, and the element value is the field intensity value of the corresponding image patch.
[0050] See Figure 7 The figure shows a histogram of the frequency distribution of field strength values in the coded field map and a schematic diagram of its global mean. On the horizontal axis, the field strength values range from 0 to approximately 10.5, and the vertical axis represents the frequency of the corresponding field strength value, with a maximum frequency exceeding 50. The gray bars represent the frequency distribution of the field strength values, exhibiting an overall right-skewed distribution. The peaks are concentrated between field strength values of approximately 1.5 and 3.0, indicating that the coding length differences of most image patches are small, and the field strength values are relatively low. As the field strength value increases, the frequency gradually decreases, with a small number of large field strength values indicating potential areas of abnormal coding length.
[0051] The global mean, marked by a black dashed line in the figure, is located at a field strength of approximately 3.0. This value serves as the benchmark for subsequent anomaly detection. Using this mean, the absolute value of the deviation between the field strength at each spatial location and the global mean can be calculated. This allows for the selection of high-frequency outliers with significant deviations as seed points for region growing. Overall, this frequency distribution reflects that the field strength values of most image patches in the coded field map are stable and concentrated, while a small number of image patches exhibit significant differences in coding length, providing a basis for preliminary defect localization.
[0052] Example 2:
[0053] In specific implementation, please refer to Figure 3 The coded field maps of surface images in different bands are stacked and fused to obtain a fused coded field map.
[0054] The coded field maps of all band surface images are stacked in ascending order of band wavelength to form a three-dimensional coded field data block. The three dimensions of the three-dimensional coded field data block are: row direction corresponding to the row position in the coded field map, column direction corresponding to the column position in the coded field map, and layer direction corresponding to the band wavelength order. Assume there are a total of... Surface images of different bands, then the three-dimensional coded field data block contains Each layer is a coded field map of a complete band surface image.
[0055] For each spatial location in the 3D coded field data block, the field intensity values on all coded field maps at that spatial location are extracted along the wavelength dimension, forming a field intensity sequence. The spatial location is determined by the row and column numbers, and the wavelength dimension corresponds to the band layer number. For a row number of... Column number is Spatial location, from layer number The floor number is In all coded field diagrams, the row number is obtained respectively. Column number is The field strength values at each location are arranged in order of layer number to form a field strength sequence. This field strength sequence contains... Each field strength value element.
[0056] In some embodiments, after extracting the field strength sequence along the wavelength dimension, the maximum and minimum values in the field strength sequence are first removed, and then the median value of the remaining values is calculated as the fused field strength value. Specifically, the operation involves: extracting the median value from the field strength sequence... The field strength values are sorted according to their numerical values. The first element (minimum) and the last element (maximum) of the sorted sequence are removed, and the remaining values are retained. Individual field strength value. If Less than or equal to If no removal operation is performed, all will be used directly. The median value is calculated for each field strength value.
[0057] Calculate the median value of the field strength sequence, for the remaining values after removing the maximum and minimum values. Given the individual field strength values, the remaining The field strength values are sorted by numerical value. If the number is odd, the median value is the field strength value at the middle position of the sorted sequence; if... If the median is even, it is taken as the arithmetic mean of the two field strength values at the middle position of the sorted sequence. The calculated median is used as the fused field strength value at that spatial position in the fused coded field map. This operation is performed on all spatial positions, arranging the fused field strength values according to their row and column numbers to form a two-dimensional matrix, which serves as the fused coded field map. The number of rows and columns in the fused coded field map are the same as those in the coded field map.
[0058] After obtaining the fused coding field map, regions with abnormal coding lengths are detected in the fused coding field map.
[0059] Calculate the global mean of all fused field strength values in the fused coded field map, and then calculate the absolute value of the deviation between each fused field strength value and the global mean. For row number ... Column number is Spatial location, combined with field strength value denoted as The global mean is denoted as The absolute value of the deviation is Sort all spatial locations by absolute deviation values in descending order of magnitude. Mark the spatial locations corresponding to the top 5% of the largest absolute deviation values as high anomalies. The 5% threshold is determined based on the proportion of defects to the overall surface area of the workpiece in the actual testing scenario, and can be used as an adjustable parameter to adapt to different workpiece types.
[0060] Using each high-dissimilar pixel as a seed point, the four-neighbor region growing method is employed for region merging. The growth criterion for the four-neighbor region growing method is as follows: for an unprocessed four-neighbor pixel on the current region boundary, calculate the absolute value of the deviation between the fused field strength value of that four-neighbor pixel and the fused field strength value of the seed point. If this absolute value is less than a preset deviation threshold, the four-neighbor pixel is merged into the current region, and that pixel is used as a new region boundary point for further growth. A four-neighbor refers to the four pixels directly adjacent to the current pixel in the row or column direction (up, down, left, right). The preset deviation threshold is determined based on the statistical distribution characteristics of the fused field strength values in the fused coding field map and can be set as a multiple of the global standard deviation. Each high-dissimilar pixel undergoes region growing independently until no more new pixels can be merged, resulting in multiple high-dissimilar regions. Each high-dissimilar region consists of a set of interconnected spatial locations.
[0061] The areas of all highly anomaly regions are statistically analyzed, with the area defined as the number of spatial locations contained within each highly anomaly region. Highly anomaly regions with areas smaller than a preset area threshold are removed. This preset area threshold is determined based on the number of pixels corresponding to the minimum size of the defect of interest on the surface of the workpiece in the fused coding field map. The remaining highly anomaly regions are designated as coding length anomaly regions. Coding length anomaly regions represent a set of anomalous locations in the fused coding field map where the fused field strength value deviates significantly from the global statistical characteristics and exhibits a continuous spatial distribution.
[0062] Example 3:
[0063] In specific implementation, please refer to Figure 4 The detected abnormal coding length region is mapped back to the surface of the workpiece under test as the initial defect region.
[0064] Obtain all spatial coordinates of each code length aberration region in the fused coding field map. Spatial coordinates in the fused coding field map are represented by row and column numbers, with the row number denoted as... The column number is recorded as For each spatial location marked as an abnormal coding length region, its row number is recorded. and column number This forms a set of spatial coordinates for the region with the abnormal coding length.
[0065] Based on the pixel resolution mapping relationship between the multispectral image sequence and the surface of the workpiece under test, the coordinates of each spatial location are converted into the physical coordinates of the workpiece surface. The pixel resolution mapping relationship is predetermined through camera calibration and the geometric parameters of the multispectral imaging system. In practice, the pixel size of each band of the surface image in the multispectral image sequence corresponds to the actual physical size of the workpiece surface. Based on the magnification or calibration coefficient of the imaging system, the image pixel coordinates are mapped to the coordinate values of the physical coordinate system of the workpiece surface. For the row number in the fused coded field image... Column number is The spatial position of the workpiece and its corresponding physical coordinates on the surface of the workpiece being measured. By row number and column number The physical resolution factor is obtained by multiplying by the physical resolution factors in the row direction and the column direction respectively, and then superimposed with the starting reference point offset. The physical resolution factor represents the actual physical length corresponding to a single spatial position on the surface of the workpiece under test in the fused coded field map.
[0066] The surface regions of the workpiece to be measured corresponding to all physical coordinates are merged. Each physical coordinate corresponds to a tiny surface cell on the surface of the workpiece. The physical surface cells corresponding to all spatial positions covered by the abnormal coding length region are combined to obtain a preliminary mapped region. This preliminary mapped region may have discontinuous boundaries or internal holes due to the discretization effect between image resolution and physical coordinate transformation.
[0067] Morphological closing operations are performed on the merged region. Specifically, the morphological closing operation involves first performing a morphological dilation operation on the merged region, followed by a morphological erosion operation on the dilated region. The morphological dilation operation expands the boundaries of the merged region outwards, filling any small holes or narrow discontinuities within the region; the morphological erosion operation contracts the boundaries of the dilated region inwards, restoring the original shape and size characteristics of the region. Both the morphological dilation and morphological erosion operations use structuring elements of the same size. The shape of the structuring element can be circular or rectangular, and its size is set according to the spatial resolution of the multispectral image sequence and the actual size of the smallest defect on the surface of the workpiece. The continuous region obtained after the morphological closing operation is used as the initial defect region.
[0068] After obtaining the initial defect region, the edge chain code is extracted from the initial defect region.
[0069] The initial defect region is binarized by setting pixels inside the region to a first grayscale value and pixels outside the region to a second grayscale value, resulting in a binary defect mask. In the binary defect mask, pixels with the first grayscale value represent points belonging to the initial defect region, and pixels with the second grayscale value represent background points. The first grayscale value can be 255, and the second grayscale value can be 0.
[0070] In practical implementation, when binarizing the initial defect region, an adaptive threshold segmentation method is used. This method dynamically determines the boundary threshold between the first and second grayscale values based on the local mean of the pixel grayscale values within the initial defect region. The specific operation of the adaptive threshold segmentation method is as follows: For each pixel within the initial defect region, a local neighborhood window is taken centered on that pixel. The local mean of the grayscale values of all pixels within this local neighborhood window is calculated. This local mean is subtracted by a fixed offset to obtain the boundary threshold for that pixel's position. If the grayscale value of the pixel is greater than the boundary threshold, the pixel is set to the first grayscale value; otherwise, it is set to the second grayscale value. The size of the local neighborhood window is determined based on the defect feature size within the initial defect region, and can be taken as one-tenth of the shorter side length of the initial defect region. The fixed offset is used to control the segmentation sensitivity and can be set as a percentage of the image's grayscale dynamic range.
[0071] An eight-neighbor boundary tracking algorithm is used to extract the outer boundary contour from a binary defect mask, resulting in a sequence of boundary pixels. The algorithm's execution process is as follows: The binary defect mask is scanned line by line to find the first pixel with the first grayscale value as the starting point for boundary tracking. Starting from the starting point, neighboring pixels are explored according to eight directions defined by the eight-neighbor direction chain code. These eight directions are coded as 0 to 7: up, upper right, right, lower right, lower, lower left, left, and upper left. The next boundary pixel with the first grayscale value is searched within the eight-neighbor area in either a counter-clockwise or clockwise direction. The found boundary pixel is marked as tracked and used as the current point to continue searching for the next boundary pixel in a fixed search direction until the starting point is reached, completing the tracking of a closed boundary contour. The coordinates of all boundary pixels traversed during the tracking process are recorded to form a boundary pixel sequence.
[0072] Chain code encoding is performed on the boundary pixel sequence. Starting from the initial point on the boundary, the direction code of each movement step is recorded to obtain the edge chain code. Specifically, the encoding method is as follows: using the initial point in the boundary pixel sequence as the reference point, the movement direction from the current boundary pixel to the next boundary pixel is obtained. This movement direction is mapped to the corresponding value in the eight-neighborhood direction code. The direction code values are recorded sequentially to form the edge chain code. The edge chain code is a sequence of direction code values, where each element is an integer between 0 and 7. Each direction code value represents the direction of movement from the current boundary pixel to the next boundary pixel in the eight-neighborhood.
[0073] Example 4:
[0074] In specific implementation, please refer to Figure 5 The edge chain code, which is a sequence of directional encoded values, is used as input. Each element of the directional encoded value sequence is denoted as... ,in This indicates the sequential number of the boundary pixel within the boundary pixel sequence. The value is from arrive integers, The direction-coded value represents the total number of pixels contained in the boundary pixel sequence. The range of values is These correspond to eight different movement directions in the eight neighboring directions.
[0075] Calculate the chain code curvature at each boundary pixel based on the difference between the encoding values in each direction of the edge chain code. For the sequence number... The boundary pixels, their chain code curvature Determined by the difference between adjacent direction codes, the chain code curvature is calculated using the center difference method, as follows:
[0076]
[0077] in, Indicates the sequence number is The chain code curvature at the boundary pixels, Indicates the sequence number is The orientation encoding value of the boundary pixel, Indicates the sequence number is The orientation encoding value of the boundary pixel, This indicates that a modulo-8 operation is performed on the difference result, resulting in the chain code curvature. The value remains arrive Within the range of integer values, modulo 8 arithmetic is performed according to... This is executed in a manner that ensures the result is a non-negative integer. When hour, use Instead; when hour, use Instead, to maintain the closure of the edge chain code. Chain code curvature. The magnitude of the value reflects the position of the ordinal number. At the boundary pixel point, the degree of drastic local change in the edge direction.
[0078] Local maxima of the chain code curvature are considered as candidate mutation points. The method for determining local maxima is as follows: for the index... Chain code curvature at boundary pixels ,if Simultaneously satisfy and ,and If it is greater than zero, then the sequence number will be... The boundary pixels are marked as candidate mutation points. The serial number is The chain code curvature at the boundary pixels, The serial number is The chain code curvature at the boundary pixels. For closed-edge chain codes, the comparison at the sequence boundary is also handled using a circular indexing method.
[0079] Calculate the chain code curvature difference between each candidate mutation point and its immediate and adjacent candidate mutation points. For each candidate mutation point, find its immediate and adjacent candidate mutation points in the directions before and after it. For the sequence number... The candidate mutation points are identified by searching the edge chain code. The first candidate mutation point found clockwise is the next adjacent candidate mutation point, and the first candidate mutation point found counterclockwise is the previous adjacent candidate mutation point. The absolute value of the difference between the chain code curvature of the candidate mutation point itself and that of the next adjacent candidate mutation point is calculated and denoted as the first curvature difference. The absolute value of the difference between the chain code curvature of the candidate mutation point itself and that of the previous adjacent candidate mutation point is also calculated and denoted as the second curvature difference. The chain code curvature difference is the larger of the first and second curvature differences.
[0080] Candidate abrupt changes in chain code curvature differences exceeding a preset curvature difference threshold are identified as curvature abrupt changes. The preset curvature difference threshold is a control parameter used to filter out locations with significant curvature changes. In some embodiments, the preset curvature difference threshold can be determined by: statistically analyzing the mean of the chain code curvature differences corresponding to all candidate abrupt changes, and setting the preset curvature difference threshold to a fixed multiple of this mean, such as 1.5. If the chain code curvature difference of a candidate abrupt change exceeds the preset curvature difference threshold, it indicates that the curvature change at that candidate abrupt change is prominent relative to adjacent curvature changes, and the candidate abrupt change is thus identified as a curvature abrupt change. Curvature abrupt changes divide the edge chain code into multiple edge segments.
[0081] In the edge chain code, a sub-chain code is extracted between every two adjacent curvature abrupt change points. Following the encoding order of the edge chain code, from each curvature abrupt change point to the next curvature abrupt change point, a sub-sequence of directional encoded values is extracted; this sub-chain code constitutes a segment of the code. The extraction includes the curvature abrupt change point as the starting point but excludes the curvature abrupt change point as the ending point to ensure that the final segmented regions are continuous and non-overlapping.
[0082] The region enclosed by the sequence of boundary pixels corresponding to each sub-chain code is considered as a single defect profile. For each sub-chain code, starting from the boundary pixel corresponding to the initial curvature abrupt change point, the boundary pixel sequence is sequentially recovered based on the direction encoding value in the sub-chain code. The starting pixel is then connected to the ending pixel to form a closed profile. The region inside this closed profile is the segmented single defect profile. The set of all single defect profiles constitutes the defect profile set of the surface of the workpiece under test.
[0083] See Figure 8 In the figure, the horizontal axis represents the index of the boundary pixels, and the vertical axis represents the chain code curvature value at the corresponding boundary pixel. The solid curve depicts the trend of chain code curvature at the defect edge as the index of the boundary pixels changes. The overall curvature value is mostly concentrated between 1 and 2, showing relatively stable fluctuations, reflecting that the curvature change is small in most areas of the defect edge and the edge direction is relatively gentle. The curvature value shows significant peaks at certain specific index points, with peak heights reaching 5 to 7, indicating that there are large curvature abrupt changes at these points, and the edge direction undergoes a sharp turn.
[0084] In the figure, the points marked with hollow circles correspond to local maxima of the chain code curvature, i.e., candidate abrupt change points. These maxima are distributed at the curvature peaks, are numerous, and unevenly spaced, indicating that there are multiple potential curvature abrupt change locations at the edge. The points marked with hollow boxes represent curvature abrupt change points after being filtered by the curvature difference threshold. These points correspond to local maxima with significantly large curvature differences, representing abrupt and representative change locations where the edge curvature changes significantly.
[0085] Curvature abrupt change points exhibit a discrete distribution within the boundary pixel index range. Some abrupt change points are closely adjacent, forming dense curvature abrupt change regions, indicating the presence of complex or detailed defect structures at the edges in these areas. Overall, the selection of curvature abrupt change points effectively identifies key turning points on the defect edges, providing an accurate basis for subsequent edge chain code segmentation based on curvature abrupt change points. The curve and labeling results conform to the description of chain code curvature and curvature abrupt change point calculation in Example 4, verifying the application effect of the chain code curvature difference and local maximum determination method in defect edge feature extraction.
[0086] Example 5:
[0087] In specific implementation, please refer to Figure 6 For each individual defect contour that is segmented, a true / false identification operation is performed.
[0088] Obtain the minimum bounding rectangle of a single defect profile. The minimum bounding rectangle is calculated using the rotating caliper method. Specifically, the convex hull of the single defect profile is calculated, and a bounding rectangle is constructed along each edge of the convex hull. The bounding rectangle with the smallest area is selected as the minimum bounding rectangle. The minimum bounding rectangle is represented by four parameters: the coordinates of the center point, the length of the length side, the length of the width side, and the rotation angle.
[0089] Calculate the aspect ratio of the minimum bounding rectangle. The aspect ratio is defined as the ratio of the length of the longest side to the width of the minimum bounding rectangle, where the length is the longer side and the width is the shorter side. The aspect ratio is represented by the symbol... The calculation method is as follows: ,in, This represents the length of the longest side of the smallest bounding rectangle, i.e., the length of the longer side. This represents the width of the smallest bounding rectangle, i.e., the length of the shorter side. The value of is greater than or equal to 1.
[0090] The ratio of the area enclosed by a single defect profile to the area of its minimum bounding rectangle is calculated, and this ratio is used as the rectangularity. The area of the region enclosed by a single defect profile is obtained using Green's formula or pixel counting. The area of the minimum bounding rectangle is the product of the length and width sides. Rectangularity is represented by the symbol... The calculation method is as follows: ,in, This represents the area enclosed by the contour of a single defect. This represents the length of the side of the smallest bounding rectangle. This represents the width and side length of the smallest bounding rectangle. The value range is between 0 and 1.
[0091] The aspect ratio of a single defect profile is compared with a first preset threshold, and its rectangularity is compared with a second preset threshold. When the aspect ratio of a single defect profile is greater than the first preset threshold and its rectangularity is less than the second preset threshold, the single defect profile is determined as a pseudo-defect profile and removed from the final defect detection results. The first preset threshold is used to filter regions with elongated shape characteristics. The first preset threshold is determined based on the typical shape statistical characteristics of real defects on the surface of the workpiece. The value of the first preset threshold can be set to 3.0; when the aspect ratio is greater than 3.0, it indicates that the region exhibits a significant elongated shape. The second preset threshold is used to filter regions with low fill degree characteristics. The second preset threshold is determined based on the fact that real defect regions usually have high fill degree. The value of the second preset threshold can be set to 0.6; when the rectangularity is less than 0.6, it indicates that the region is sparsely distributed within the minimum bounding rectangle. A single defect profile that simultaneously satisfies an aspect ratio greater than the first preset threshold and a rectangularity less than the second preset threshold, and whose morphological characteristics are consistent with the pseudo-defect morphology other than scratches, is determined as a pseudo-defect profile.
[0092] After the pseudo-defect contours are removed, a defect type classification operation is performed on each individual defect contour that remains in the final defect detection result.
[0093] Calculate the Fourier descriptor of a single defect contour. The Fourier descriptor is calculated by representing the sequence of boundary pixels of a single defect contour as a complex sequence, where the first pixel is the first line of the descriptor. The complex coordinates of the boundary pixels are marked as follows: ,in, For the first The x-coordinates of the boundary pixels For the first The ordinate of each boundary pixel. The imaginary unit, The range of values is from arrive integers, This represents the total number of boundary pixels of a single defect contour. A one-dimensional discrete Fourier transform is performed on the complex sequence to obtain the Fourier coefficient sequence; each coefficient in the Fourier coefficient sequence is a Fourier descriptor. The formula for calculating the Fourier descriptor is:
[0094]
[0095] in, Indicates the first Fourier descriptor, The frequency order, The range of values is from arrive integers, Indicates the first Complex coordinates of each boundary pixel. The total number of boundary pixels of a single defect contour. It is a natural constant. The imaginary unit, Pi is used as the mathematical constant. The low-frequency components of the Fourier descriptor are normalized and used as the shape feature vector. The low-frequency components refer to... The value ranges from arrive Fourier description of the sub-sub ... The preset low-frequency retention order, The value of is determined based on the complexity of the defect shape and can be set to 10. The normalization method is as follows: [The text abruptly ends here, so the translation stops.] to Divide each Fourier descriptor by The modulus is determined to eliminate the effects of contour scale, rotation, and starting point. The dimension of the shape feature vector is... .
[0096] The mean and variance of pixel grayscale values within the region enclosed by a single defect contour are calculated and used as the texture feature vector. A reference band image corresponding to the surface of the workpiece is extracted from the multispectral image sequence. All pixels within the region enclosed by a single defect contour are extracted from the reference band image. The mean and variance of these pixel grayscale values are calculated, and the mean and variance are concatenated into a two-dimensional vector, which is used as the texture feature vector.
[0097] The shape feature vector and texture feature vector are concatenated into a fused feature vector. The dimension of the fused feature vector is the sum of the dimensions of the shape feature vector and the texture feature vector. The element is the shape feature vector, the _th element is the shape feature vector, the _th ... The nth element is the average pixel grayscale value, and the th element is the mean pixel grayscale value. Each element represents the variance of the pixel grayscale values.
[0098] The fused feature vector is input into a pre-trained classifier to obtain the defect type label corresponding to a single defect contour. The pre-trained classifier uses a fully connected neural network structure, which includes an input layer, hidden layers, and an output layer. The number of neurons in the input layer is the same as the dimension of the fused feature vector. The hidden layer is set to one layer containing 128 neurons, and the activation function is the rectified linear unit function. The number of neurons in the output layer is the same as the preset number of defect type labels, and the activation function of the output layer is the flexible maximum function. The output value of each neuron in the output layer represents the probability value of a single defect contour belonging to the corresponding defect type label. The defect type label corresponding to the output layer neuron with the highest probability value is taken as the defect type label corresponding to a single defect contour. The training process of the pre-trained classifier is as follows: collect sample defect contours labeled with defect type labels, extract the fused feature vector of each sample defect contour as a training sample, and use the cross-entropy loss function and gradient descent optimization algorithm to iteratively update the weight parameters and bias parameters of the fully connected neural network until the cross-entropy loss function value converges or the preset number of iterations is reached.
[0099] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. An automatic detection method for surface defects of a workpiece, characterized in that, The method includes the following steps: Acquire a multispectral image sequence of the surface of the workpiece to be tested, wherein the multispectral image sequence contains surface images of at least two different spectral bands; Each band surface image is decomposed into multiple image blocks, and a dynamic coding length is assigned to each image block based on the statistical distribution of pixel gray values within each image block. Based on the difference in dynamic coding length between adjacent image blocks in the same band surface image, a coding field map of the band surface image is constructed; By overlaying and fusing the coded field maps of surface images from different bands, a fused coded field map is obtained. In the fused coding field map, abnormal coding length regions are detected and mapped back to the surface of the workpiece to be tested as initial defect regions. Edge chain codes are extracted from the initial defect region, and individual defect contours are segmented based on the curvature abrupt change points of the edge chain codes, which serve as the final defect detection result for the surface of the workpiece under test.
2. The automatic detection method for surface defects of a workpiece according to claim 1, characterized in that, Based on the statistical distribution of pixel grayscale values within each image block, the method for allocating a dynamic coding length to each image block is as follows: For any image patch, calculate the mean of the gray values of all pixels within the image patch, and use it as the gray center of the image patch; Calculate the difference between the gray value of each pixel in the image block and the gray center, and sum the absolute values of all the differences to get the gray divergence of the image block. The grayscale divergence of the image block is multiplied by the variance of the grayscale values of the pixels within the image block, and the product is used as the dynamic coding base of the image block. Using the natural constant as the base of the logarithm, take the logarithm of the dynamic coding base, round the logarithm value and add one to it, and use this logarithm as the dynamic coding length of the image block.
3. The automatic detection method for surface defects of a workpiece according to claim 2, characterized in that, The method for constructing the coded field map of a surface image in the same band, based on the difference in dynamic coding length between adjacent image patches, is as follows: For each image patch in the same band surface image, obtain the first absolute value of the difference between the dynamic coding length of the image patch and its left adjacent image patch, and obtain the second absolute value of the difference between the dynamic coding length of the image patch and its upper adjacent image patch. The square root of the sum of the squares of the first absolute value and the second absolute value is taken as the field strength value of the image patch; The field strength values of all image blocks in the surface image of this band are arranged according to the original spatial position of the image blocks to form a two-dimensional matrix, which serves as the coded field map of the surface image of this band.
4. The automatic detection method for surface defects of a workpiece according to claim 3, characterized in that, The method for overlaying and fusing coded field maps of surface images from different bands to obtain a fused coded field map is as follows: The coded field maps of all band surface images are stacked into three-dimensional coded field data blocks in ascending order of band wavelength; For each spatial location in the three-dimensional coded field data block, extract the field strength values on all coded field maps at that location along the wavelength dimension to form a field strength sequence. Calculate the median value of the field strength sequence and use the median value as the fused field strength value at that spatial location in the fused coded field map; Arrange the fused field strength values of all spatial locations according to their spatial location to form a fused coded field map.
5. The automatic detection method for surface defects of a workpiece according to claim 4, characterized in that, After extracting the field strength sequence along the wavelength dimension, the maximum and minimum values in the sequence are first removed, and then the median value of the remaining values is calculated as the fused field strength value.
6. The automatic detection method for surface defects of a workpiece according to claim 4, characterized in that, The method for detecting regions with abnormal coding lengths in the fused coding field map is as follows: Calculate the global mean of all fused field strength values in the fused coded field map, and calculate the absolute value of the deviation between each fused field strength value and the global mean; The spatial locations corresponding to the top 5% of the absolute values of all deviations are marked as high outliers; Using each high heterogeneous point as a seed point, the four-neighborhood region growing method is used to merge pixels whose absolute value of the fusion field strength value in the four neighborhoods is less than the fusion field strength value of the seed point into the same region until they can no longer be merged, thus obtaining multiple high heterogeneous regions. Highly abnormal regions with an area smaller than a preset area threshold are removed, and the remaining highly abnormal regions are treated as regions with abnormal coding length.
7. The automatic detection method for surface defects of a workpiece according to claim 5, characterized in that, The method for mapping the detected abnormal code length region back to the surface of the workpiece under test as the initial defect region is as follows: Obtain the spatial coordinates of all locations in the fused coding field map for each region with an abnormal coding length. Based on the pixel resolution mapping relationship between the multispectral image sequence and the surface of the workpiece under test, each spatial position coordinate is converted into the physical coordinates of the surface of the workpiece under test. All physical coordinates corresponding to the surface regions of the workpiece to be measured are merged, and morphological closing operations are performed on the merged regions. The continuous region obtained after the closing operation is used as the initial defect region.
8. The automatic detection method for surface defects of a workpiece according to claim 7, characterized in that, The method for extracting edge chain codes from the initial defect region is as follows: The initial defect region is binarized, with pixels inside the initial defect region set to the first grayscale value and pixels outside the initial defect region set to the second grayscale value, to obtain a binary defect mask. An eight-neighbor boundary tracking algorithm is used to extract the outer boundary contour from a binary defect mask, resulting in a sequence of boundary pixels. Chain code encoding is performed on the boundary pixel sequence. Starting from the starting point on the boundary, the direction code of each step of movement is recorded to obtain the edge chain code.
9. The automatic detection method for surface defects of a workpiece according to claim 8, characterized in that, When performing binarization on the initial defect region, an adaptive threshold segmentation method is used to dynamically determine the boundary threshold between the first gray value and the second gray value based on the local mean of the pixel gray values within the initial defect region.
10. An automatic detection system for surface defects of a workpiece, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the automatic detection method for surface defects of a workpiece as described in any one of claims 1 to 9.