Accurate defect segmentation method based on adaptive gradient enhancement and statistical threshold
By employing adaptive gradient enhancement and statistical thresholding, the accuracy and robustness issues of existing defect segmentation methods in complex environments are addressed, achieving high signal-to-noise ratio defect segmentation and improving the accuracy and stability of defect identification.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-28
- Publication Date
- 2026-04-07
AI Technical Summary
Existing defect segmentation methods lack accuracy and robustness when faced with varying imaging conditions and defect morphologies. In particular, they are prone to missed detections or oversegmentation under conditions of low contrast, complex background textures, or severe noise interference. Furthermore, filtering processes struggle to simultaneously suppress background textures and preserve defect edges.
An adaptive gradient enhancement and statistical thresholding method is adopted. By acquiring a small defect image, binarization and image enhancement are performed. Multi-directional gradient calculation and edge protection filtering are used, combined with adaptive segmentation threshold and contour filtering, to generate accurate defect segmentation results.
It improves the accuracy and stability of defect segmentation, can effectively identify defect edges in complex environments, reduces sensitivity to changes in illumination and noise, and achieves defect segmentation with a high signal-to-noise ratio.
Smart Images

Figure CN121811045A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of digital image processing and artificial intelligence, specifically to a method for accurate defect segmentation based on adaptive gradient enhancement and statistical thresholding. Background Technology
[0002] With the deepening development of precision manufacturing towards intelligence and automation, machine vision technology is playing an increasingly central role in product quality control. Especially in the production of high-end electronic products such as LCD panels, the use of digital image processing and artificial intelligence algorithms to quickly and accurately identify and segment minute defects has become a key technological support for ensuring product yield and improving production efficiency, and is an important direction for continuous research and optimization in this field.
[0003] However, when applying the above technologies to complex industrial environments, the accuracy and robustness of existing defect segmentation methods still need further improvement when faced with varying imaging conditions and defect morphologies. Specifically; Existing technologies, such as those disclosed in Chinese patent document CN115018828B, employ local thresholding (e.g., the Otsu's method) scheme. Their segmentation effectiveness directly depends on the local statistical characteristics of the original image. When the image under test suffers from low contrast, complex background texture, or severe noise interference, the statistical basis of these thresholding algorithms, which rely on original pixel data for decision-making, becomes unreliable, easily leading to unstable segmentation results and causing missed detections or oversegmentation.
[0004] To improve the quality of input images, conventional methods employ preprocessing techniques such as filtering. However, while general smoothing filters (such as Gaussian filters) suppress background textures and random noise, they often blur or erode the true edges of low signal-to-noise ratio defects such as mura (cloudiness) and faint scratches, resulting in the loss of crucial details. Conversely, if weaker filtering is used to preserve details, background interference cannot be effectively removed, leaving existing techniques in a dilemma where neither can be achieved perfectly. Summary of the Invention
[0005] The purpose of this invention is to provide a method for accurate defect segmentation based on adaptive gradient enhancement and statistical thresholding, so as to solve the problems mentioned in the background art.
[0006] To achieve the above objectives, the present invention provides the following technical solution: A method for accurate defect segmentation based on adaptive gradient enhancement and statistical thresholding, comprising the following steps: S1. Obtain a small defect image containing the defect to be segmented; perform binarization on the small defect image to locate the panel region in the image, and generate the region of interest based on the panel region to remove background interference from non-panel regions. S2. Perform image enhancement processing on the region of interest to generate an enhanced image that suppresses background texture and enhances defect edges; the image enhancement processing includes multi-directional gradient calculation and fusion of the region of interest, and applying edge protection filtering to the fused gradient information; S3. Calculate the adaptive segmentation threshold based on the pixel statistical features of the enhanced image; the pixel statistical features include the global standard deviation of the enhanced image and the average value of a preset number of darkest pixels; S4. Binarize the enhanced image based on the adaptive segmentation threshold, and perform contour filtering on the binarization result to output the pixel-level segmentation result of the defect.
[0007] Furthermore, the defects to be segmented are obtained as follows: Acquire high-resolution product images, perform preliminary scanning of the acquired product images using target detection algorithms to identify potential defect targets; output the coordinates of a rectangular box containing defect location information, as well as the corresponding original high-resolution image; the area within the rectangular box coordinates is the small defect image of the defect to be segmented.
[0008] Furthermore, the steps for cropping and preprocessing the defect thumbnail are as follows: Based on the received bounding box coordinates, a small defect image containing the defect target is cropped from the original high-resolution image; the cropped color image is converted into a single-channel grayscale image; and the RGB three-channel image is converted into a grayscale image.
[0009] Furthermore, in S1, the defect mini-image is subjected to global threshold binarization using the Otsu method to distinguish the background of the panel area from that of the non-panel area; and the maximum contour of the panel area is extracted, and the defect mini-image is cropped a second time based on the bounding rectangle of the maximum contour to generate the region of interest.
[0010] Furthermore, in S2, multi-directional gradient calculation and fusion are used to calculate the Sobel gradient maps of the region of interest in the horizontal and vertical directions respectively, and the absolute values of the horizontal and vertical Sobel gradient maps are weighted and summed to generate an omnidirectional gradient map.
[0011] Furthermore, the edge protection filtering in S2 is implemented through an adaptive recursive filter based on domain transformation. When processing each pixel, the adaptive recursive filter dynamically calculates the dynamic feedback coefficient based on the difference between the current pixel and the previous pixel in the gradient map, so as to keep the defect edges from being blurred while smoothing the background texture.
[0012] Furthermore, S2 also includes weighted fusion of the gradient map after edge protection filtering with the original grayscale image of the region of interest to generate an enhanced image.
[0013] Furthermore, the complete calculation steps for edge protection filtering are as follows: Input an omnidirectional gradient map, where pixel values reflect the edge intensity at corresponding locations in the original image; obtain the width and height of the input omnidirectional gradient map; obtain the maximum and minimum values of all pixels in the omnidirectional gradient map; To determine the spatial domain smoothing coefficient, the calculation logic is to multiply the smaller of the width and height values by the calibrated spatial scale scaling factor to obtain the spatial domain smoothing coefficient; to determine the range domain sensitivity coefficient, the calculation logic is to first calculate the difference between the maximum and minimum values, and then multiply the difference by the calibrated range scale scaling factor to obtain the range domain sensitivity coefficient. Create a two-dimensional array of the same size as the omnidirectional gradient map, initialized with all zeros, to store the local gradient entropy modulation map; iterate through each pixel in the omnidirectional gradient map, and for each pixel, define a neighborhood window centered on the pixel with a side length equal to the size of the local gradient entropy window; count the gradient values of all pixels within the neighborhood window and construct a gradient value histogram; normalize each count in the gradient value histogram to obtain the probability of each gradient value appearing within the neighborhood window; calculate the gradient entropy of the neighborhood. The calculated gradient entropy is stored in the position of the corresponding pixel in the local gradient entropy modulation map; after the entropy values of all pixels have been calculated, the entire local gradient entropy modulation map is normalized. The omnidirectional gradient map will be subjected to four independent and unidirectional recursive filtering processes: from left to right, from right to left, from top to bottom, and from bottom to top. Each filtering process will generate a temporary result map. After filtering in four directions and obtaining four temporary result images, the final output image is created; all pixels are traversed to determine the final filter value; the calculation logic is to add the pixel values of corresponding pixels in the four temporary result images, and then divide by a constant 4 to obtain the arithmetic mean; the final output is the final enhanced image.
[0014] Furthermore, the adaptive segmentation threshold in S3 is calculated using the following formula: ,in, It is represented as the average of the K pixels with the smallest grayscale values in the enhanced image. Represented as the global pixel standard deviation of the enhanced image. This is represented as the preset sensitivity coefficient.
[0015] Furthermore, in S4, the contour filtering defines the internal area of the region of interest after removing the edge of the preset proportional width as the central safe area, and prioritizes retaining contours whose geometric center is located within the central safe area.
[0016] Compared with the prior art, the beneficial effects of the present invention are: This invention transforms image enhancement processing from a general image cleaning role into a signal preprocessor with a clear objective tailored to subsequent segmentation decisions. The primary task of enhancement processing is no longer to smooth all regions indiscriminately, but to process intelligently and differentiatedly based on the local characteristics of the image: that is, while suppressing non-target regions such as background textures, it actively and selectively enhances the gradient signal of real defect edges. By constructing intelligent signal preprocessing steps, a near-ideal working environment with extremely high signal-to-noise ratio can be created for subsequent segmentation algorithms. The decision-making basis of well-designed statistical segmentation algorithms becomes extremely solid. Decisions can be made based on the enhanced defect features, thus exhibiting high immunity to interference factors such as changes in illumination and contrast fluctuations in the original image. By creating optimal working conditions for subsequent steps through preceding steps, the design of subsequent steps fully utilizes the deep coupling design of the processing results of preceding steps, transforming two steps that originally had contradictory or dependent problems into a mutually promoting and synergistically beneficial control. Attached Figure Description
[0017] Figure 1 This is a schematic diagram illustrating the process and application scenarios of the present invention; Figure 2 This is a schematic diagram of the overall process flow of the present invention; Figure 3 This is a schematic diagram of the edge protection filtering calculation steps of the present invention. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to specific embodiments.
[0019] It should be noted that, unless otherwise defined, the technical or scientific terms used in this invention should have the ordinary meaning understood by one of ordinary skill in the art to which this invention pertains. The terms "first," "second," and similar terms used in this invention do not indicate any order, quantity, or importance, but are merely used to distinguish different components. Terms such as "comprising" or "including" mean that the element or object preceding the word encompasses the elements or objects listed following the word and their equivalents, without excluding other elements or objects. Terms such as "connected" or "linked" are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect. Terms such as "upper," "lower," "left," and "right" are used only to indicate relative positional relationships; when the absolute position of the described object changes, the relative positional relationship may also change accordingly. Example
[0020] Please see Figures 1 to 3This invention provides a technical solution: a method for accurate defect segmentation based on adaptive gradient enhancement and statistical thresholding, the specific steps of which include: S1. Obtain a small defect image containing the defect to be segmented; perform binarization on the small defect image to locate the panel region in the image, and generate the region of interest based on the panel region to remove background interference from non-panel regions. In S1, the defect mini-image is subjected to global threshold binarization using the Otsu method to distinguish the background of the panel area from that of the non-panel area; and the maximum contour of the panel area is extracted, and the defect mini-image is cropped again based on the bounding rectangle of the maximum contour to generate the region of interest. In the defect detection scenario of LCD panels, when the defect is located near the physical edge of the panel, the small defect image initially cropped by the automated optical inspection (AOI) system in this embodiment simultaneously contains a panel area with high grayscale and a pure black non-panel background with extremely low grayscale. There is an extremely high grayscale difference between the panel area and the pure black non-panel background, far greater than the grayscale difference between the panel's own texture or the defect and the background. Applying Otsu's method to the defect image, which automatically finds the optimal global threshold, divides image pixels into foreground and background categories, maximizing the inter-class variance. This method can stably separate the bright foreground from the pure black background. In the generated binary image, boundary tracking calculations are performed... The method extracts the contours of all foreground regions and filters out the contour with the largest area. The contour precisely corresponds to the physical boundary of the panel region. The minimum bounding rectangle of the largest contour is calculated, and the coordinates of this bounding rectangle are used to perform secondary cropping on the original grayscale defect small image, thereby obtaining the region of interest containing only the panel content. It achieves accurate removal of non-panel background regions. It eliminates the serious interference of high-contrast pseudo edges between pure black background and panel on subsequent gradient calculation and threshold segmentation steps from the root, ensuring that all subsequent processing steps focus on the panel content that really needs to be analyzed, improving the accuracy and reliability in processing edge defects, and demonstrating strong anti-edge interference capability. The defects to be segmented are obtained as follows: Acquire high-resolution product images, perform preliminary scanning on the acquired product images using target detection algorithms to identify potential defect targets; output the coordinates of a rectangular box containing defect location information, as well as the corresponding original high-resolution image; the area within the rectangular box coordinates is the small defect image of the defect to be segmented; In this embodiment, the target detection algorithm employs the YOLO series of deep learning models, including but not limited to YOLOv8 and YOLOv5. The algorithm performs a preliminary scan of the acquired high-resolution product image, identifying potential defect targets in the image through a built-in neural network model. It outputs the coordinates of a bounding box (bbox) containing defect location information, along with the corresponding original high-resolution image. The area within the bounding box is the small image of the defect to be segmented. This preliminary localization by the target detection algorithm achieves the goal of quickly focusing on potential defect areas from a large field-of-view image, significantly reducing the computational load of subsequent fine segmentation and improving overall processing efficiency. Simultaneously, target detection algorithms such as YOLO have high detection recall rates, effectively capturing various typical defects (such as bright spots, dark spots, scratches, murmurs, etc.), providing reliable input for subsequent accurate segmentation. The steps for cropping and preprocessing the defect thumbnail are as follows: Based on the received bounding box coordinates, crop out a small defect image containing the defect target from the original high-resolution image; convert the cropped color image into a single-channel grayscale image; and perform a conversion from an RGB three-channel image to a grayscale image. The specific grayscale conversion is calculated using the following weighted summation formula:
[0021] in, , and These are the pixel values for the red, green, and blue channels, respectively. The weighting is based on the human eye's sensitivity to different colors; the human eye is most sensitive to green, followed by red, and then blue. Therefore, when converting a color image to grayscale, green has the largest weight (0.587), followed by red (0.299), and blue has the smallest (0.114). Grayscale conversion removes color information while preserving the image's brightness information, which is crucial for subsequent binarization and contour extraction. For defect contour detection and extraction, brightness or contrast difference is the primary criterion, not color. Simultaneously, it simplifies subsequent image processing and reduces complexity. Since a grayscale image has only one channel, the computational and storage requirements are smaller, thus accelerating subsequent image processing and analysis tasks. S2. Perform image enhancement processing on the region of interest to generate an enhanced image that suppresses background texture and enhances defect edges; the image enhancement processing includes multi-directional gradient calculation and fusion of the region of interest, and applying edge protection filtering to the fused gradient information; In S2, multi-directional gradient calculation and fusion are used to calculate the Sobel gradient maps of the region of interest in the horizontal and vertical directions respectively, and to perform a weighted summation of the absolute values of the horizontal and vertical Sobel gradient maps to generate an omnidirectional gradient map. The Sobel operator is used to calculate the gradients of the region of interest in the horizontal and vertical directions, respectively, to obtain gradient maps G_x and G_y. To eliminate the influence of gradient directionality, the absolute values of gradient maps G_x and G_y are taken to obtain |G_x| and |G_y|. The gradient information in the two directions is then merged into an omnidirectional gradient map using a linear weighted fusion method. The calculation formula is as follows:
[0022] Here, 0.5 is a preset weighting coefficient, indicating that the gradients in the horizontal and vertical directions are given equal importance. By fusing gradient information from multiple directions, omnidirectional gradient information with good response to defect edges of any orientation is obtained. This ensures that the subsequent edge enhancement processing will not suffer from effect attenuation due to the specific orientation of the defect, laying the foundation for generating high-quality enhanced images. In S2, edge protection filtering is implemented through an adaptive recursive filter based on domain transformation. When processing each pixel, the adaptive recursive filter dynamically calculates the dynamic feedback coefficient based on the difference between the current pixel and the previous pixel in the gradient map, so as to keep the defect edges from being blurred while smoothing the background texture. It adopted a computational complexity of The fast edge protection filtering algorithm simulates the effect of a bilateral filter by performing filtering in the transform domain. The core innovation of the adaptive recursive filter based on domain transform lies in mapping the image signal from the spatial domain to the transform domain. In the transform domain, the transform domain distance between adjacent pixels is defined. The transform domain distance depends not only on the spatial proximity of the pixels, but also on the difference in gradient values between adjacent pixels. This makes the transform domain distance close to the physical distance in flat gray-level areas, while the transform domain distance is stretched and amplified in edge areas. Based on the transform domain distance, a dynamic feedback coefficient is dynamically calculated for each pixel; a recursive filtering iteration is performed using the dynamic feedback coefficient, wherein the dynamic feedback coefficient determines the influence weight of the filter output of the previous pixel on the current pixel; The specific logic for calculating the transform domain distance is as follows: Processing the current pixel When, calculate its relationship with the previous pixel. Transform domain distance between This distance combines spatial distance and gradient value differences, and its calculation formula is as follows:
[0023] in, and These are the gradient maps used as filter inputs. The pixel values at the current and previous positions, For spatial domain smoothing parameters, This is the range sensitivity parameter; it can be seen from this formula that in the gray-scale flat region ( The transform domain distance is approximately 1, consistent with the spatial domain distance; in the edge region of dramatic grayscale changes ( (The distance is relatively large), the transform domain distance is significantly "stretched" and becomes very large; The calculation of the transform domain distance involves the dynamic calculation of spatial domain smoothing parameters and range domain sensitivity parameters: the spatial domain smoothing parameters are adaptively determined proportionally to the size of the region of interest to adapt to small defect images of different sizes; the range domain sensitivity parameters are adaptively determined proportionally to the dynamic range of the input gradient image to adapt to image environments with different contrasts. The specific logic for calculating spatial domain smoothing parameters is as follows:
[0024] in, and This involves inputting the width and height of the region of interest. This means taking the smaller of the two values. This is a preset spatial scale ratio coefficient, which, based on experience, typically ranges from 0.1 to 0.2; for example, for a... The area of concern, if If we take 0.16, then... This design allows the filter to adapt to defect "small maps" of different sizes. The specific calculation logic for the range sensitivity parameter is as follows:
[0025] in, and It is the omnidirectional gradient map input to the edge protection filter. Maximum and minimum pixel values, This is a preset range scale ratio coefficient, which, based on experience, typically ranges from 0.03 to 0.1; this value means that the gradient difference between pixels is less than... The area will be treated as texture and smoothed; The adjustment mechanism of the dynamic feedback coefficient is as follows: when the transform domain distance between adjacent pixels is small, the generated dynamic feedback coefficient tends to retain the filtering result of the previous pixel, thereby producing a strong smoothing effect to suppress texture; when the transform domain distance between adjacent pixels increases significantly due to gradient abrupt changes, the generated dynamic feedback coefficient tends to cut off the influence of the previous pixel, thereby retaining the original value of the current pixel to protect the edge; the recursive filtering iteration performs bidirectional traversal of the rows and columns of the image to eliminate phase delay; The specific logic for calculating the dynamic feedback coefficient is as follows:
[0026] in, For the current pixel The dynamic feedback coefficient, For the transform domain distance, This is a spatial domain smoothing parameter; through this mechanism, when the filter is in the smooth region, Smaller Approaching 1, The weight is large, resulting in a strong smoothing effect, thus effectively removing texture; when the filter encounters an edge, A dramatic increase It rapidly decays to near zero, resulting in The filter is "reset" here, and the influence of the previous pixel is cut off, thus achieving precise protection of the edge; Using dynamic coefficients to perform first-order recursive filtering, the iterative formula is as follows:
[0027] in, It is the current pixel The filtered output, It is its input value. It is the filtered output of the previous pixel; the iterative process is performed independently on the rows and columns of the image to complete the two-dimensional filtering; in order to eliminate phase delay, the recursive filtering iteration performs bidirectional traversal on the rows and columns of the image, that is, it performs four independent unidirectional filterings from left to right, from right to left, from top to bottom, and from bottom to top, and then takes the arithmetic mean of the four filtering results as the final output. By introducing the domain transformation concept from signal processing into image filtering, a filter capable of dynamically adjusting its behavior is constructed. When the adaptive recursive filter moves across a flat background texture, it acts as a powerful smoother, effectively erasing interfering textures. However, when the adaptive recursive filter encounters a defect edge, the dynamic feedback coefficients instantly become ineffective, thus perfectly preserving the steepness of the edge. This edge-preserving and texture-removing capability fundamentally solves the contradiction between smoothing noise and preserving edges in traditional filtering methods, providing a high signal-to-noise ratio input for subsequent threshold segmentation, which is key to achieving accurate defect depiction. S2 also includes weighted fusion of the gradient map after edge protection filtering with the original grayscale image of the region of interest to generate an enhanced image; the weighted fusion aims to superimpose the enhanced edge features after filtering with the brightness features of the original image, so that the enhanced image has both the contour information of the defect and the brightness information of the body. After obtaining a clean and sharpened gradient map through edge protection filtering, in order to comprehensively utilize the edge information of the defect and its own brightness and darkness information, the gradient map is linearly superimposed with the original grayscale image of the region of interest from S1 that has not undergone any processing, and fused with each having a 50% weight to obtain the final enhanced image. This achieves the fusion of multimodal features. The final enhanced image not only contains the enhanced defect contour information, i.e., from the processed gradient map, but also retains the original brightness and darkness contrast information of the defect body relative to the background, i.e. from the original grayscale image. This makes the defect region in the image more prominent in both the edge and grayscale dimensions, improving the stability and accuracy of subsequent segmentation steps. The key parameters involved in the adaptive recursive filter process in this embodiment are defined as follows.
[0028] The sign of the parameter for the spatial domain smoothness coefficient is set as follows: The effective neighborhood radius for smoothing in the spatial dimension is defined by the adaptive recursive filter. A larger spatial smoothing coefficient means that the adaptive recursive filter will consider the influence of more distant pixels, thus producing a stronger blurring effect. It is adaptively determined based on the size of the region of interest to be processed; it depends on the spatial scale scaling factor, and the parameter sign is... ; The parameter sign of the range sensitivity coefficient is set to A sensitivity threshold for pixel value differences is set for the adaptive recursive filter. A smaller range-domain sensitivity coefficient makes the adaptive recursive filter extremely sensitive to even small gradient changes, thus tending to preserve more details; conversely, a larger range-domain sensitivity coefficient smooths out more gradient changes. The value is also adaptive, depending on the range scale ratio coefficient, and the parameter sign is... ; The spatial scale scaling factor and the range scale scaling factor together determine the basic smoothing characteristics of the adaptive recursive filter. This is determined through offline experimental calibration. The specific calibration process is as follows: Prepare an image dataset containing various typical defects (in this embodiment, typical defects include bright / dark spots, scratches, mura, etc.) and gold standard segmentation results manually and precisely segmented by senior engineers; establish an evaluation index, which quantifies the similarity between the filtered image and the gold standard result obtained through subsequent threshold segmentation. Using the intersection-union ratio (IUU) or Dice coefficient, perform a grid search on the spatial scale scaling factor and the range scale scaling factor within a preset two-dimensional parameter space; for each combination of spatial scale scaling factor and range scale scaling factor, perform a complete filtering and segmentation process on the entire dataset and calculate the average evaluation index score. Select the spatial scale scaling factor and range scale scaling factor that results in the highest average evaluation index score as the fixed parameters in this embodiment; in this embodiment, through the above calibration process, the value of the spatial scale scaling factor is determined to be 0.05, and the value of the range scale scaling factor is determined to be 0.1. The sign of the parameter for the local gradient entropy window size is set to... The definition specifies the neighborhood size referenced when calculating the local gradient entropy of each pixel; the window defines a square region centered on the current pixel; in this embodiment, the local gradient entropy window size is set to 5 pixels, i.e. The window; The window is large enough to capture meaningful local texture statistics, yet small enough to maintain computational locality and avoid introducing irrelevant information far removed from the current pixel, while also taking computational efficiency into account. The complete calculation steps for edge protection filtering in this embodiment are as follows: Input an omnidirectional gradient map, where pixel values reflect the edge intensity at corresponding locations in the original image; obtain the width and height of the input omnidirectional gradient map; obtain the maximum and minimum values of all pixels in the omnidirectional gradient map; To determine the spatial domain smoothing coefficient, the calculation logic is to multiply the smaller of the width and height values by the calibrated spatial scale scaling factor to obtain the spatial domain smoothing coefficient; to determine the range domain sensitivity coefficient, the calculation logic is to first calculate the difference between the maximum and minimum values, and then multiply the difference by the calibrated range scale scaling factor to obtain the range domain sensitivity coefficient. Create a two-dimensional array of the same size as the omnidirectional gradient map, initialized with all zeros, to store the local gradient entropy modulation map; iterate through each pixel in the omnidirectional gradient map, and for each pixel, define a neighborhood window centered on the pixel with a side length equal to the size of the local gradient entropy window; count the gradient values of all pixels within the neighborhood window and construct a gradient value histogram; normalize each count in the gradient value histogram to obtain the probability of each gradient value appearing within the neighborhood window; calculate the gradient entropy of the neighborhood; the calculation logic is as follows: for each non-zero probability in the gradient value histogram, calculate the non-zero probability multiplied by the logarithm of the non-zero probability to base 2, sum all the calculation results, and take the negative number. The calculated entropy value is stored in the position of the corresponding pixel in the local gradient entropy modulation map; after the entropy value of all pixels has been calculated, the entire local gradient entropy modulation map is normalized to linearly map the numerical range to the interval [0.5, 1.5]; the purpose of normalization is to convert the entropy value into a modulation factor centered at 1 that has an amplification or reduction effect. The omnidirectional gradient map will be subjected to four independent, unidirectional recursive filtering processes: from left to right, from right to left, from top to bottom, and from bottom to top; each filtering process will generate a temporary result map. The following is a detailed explanation of the filtering process from left to right: Create a temporary result map with the same size as the omnidirectional gradient map; traverse row by row, processing each row from the second pixel to the right until the end of the row; while processing the current pixel, perform the following sub-steps: Obtain the gradient value of the current pixel and the gradient value of its left neighboring pixel; obtain the modulation factor corresponding to the current pixel in the local gradient entropy modulation map; dynamically calculate the modulated range domain sensitivity coefficient; the calculation logic is to multiply the basic range domain sensitivity coefficient by the modulation factor; calculate the transform domain distance, the calculation logic is to find the absolute value of the difference between the gradient value of the current pixel and the gradient value of its left neighboring pixel; multiply the absolute value by the spatial domain smoothing coefficient, and then divide by the modulated range domain sensitivity coefficient; add the result to a constant 1; The dynamic feedback coefficients are calculated by first dividing the transform domain distance by the spatial domain smoothing coefficient, then taking the inverse, and finally calculating using the natural constant. A power with the base being the exponent and the result being the exponent; Calculate the filtered output value of the current pixel; the calculation logic is to subtract the dynamic feedback coefficient from the constant 1, and then multiply it by the gradient value of the current pixel to obtain the first part; multiply the dynamic feedback coefficient by the already calculated filtered output value of the left adjacent pixel to obtain the second part; add the first part and the second part together. After filtering in four directions and obtaining four temporary result images, the final output image is created. All pixels are traversed to determine the final filter value. The calculation logic is to add the pixel values of corresponding pixels in the four temporary result images and then divide by a constant of 4 to obtain the arithmetic mean. The final output is the final enhanced image. The background texture and random noise in the enhanced image are significantly smoothed, while the real defect edge contours are clearly preserved, providing ideal input for subsequent accurate segmentation. S3. Calculate the adaptive segmentation threshold based on the pixel statistical features of the enhanced image; the pixel statistical features include the global standard deviation of the enhanced image and the average value of a preset number of the darkest pixels that can represent the baseline of the dark areas of the image. The adaptive segmentation threshold in S3 is calculated using the following formula:
[0029] in, Represented as the image with the smallest gray value in the enhanced image. The average value of each pixel. Represented as the global pixel standard deviation of the enhanced image. This is expressed as a preset sensitivity coefficient; The adaptive segmentation threshold calculation described in S3 employs a dark area anchoring strategy, selecting the image with the smallest grayscale value from the enhanced image. The arithmetic mean of each pixel is calculated as a baseline value for the dark area that can respond to subtle defects; the global pixel standard deviation of the enhanced image is calculated as a floating term reflecting the overall contrast of the image; the dark area baseline value is added to the floating term weighted by the sensitivity coefficient to obtain the adaptive segmentation threshold; wherein, the... The value is dynamically determined based on the total number of pixels in the image to ensure the representativeness of the statistical sample; Sort all pixel values of the enhanced image in ascending order and select the smallest value. In this embodiment, each pixel... This is a preset value dynamically determined based on the total number of pixels in the image; calculation The arithmetic mean of the pixels is used to obtain the darkest value. Pixel mean; represents the grayscale baseline of the most prominent dark areas in the image; calculates the global pixel standard deviation of all pixels in the enhanced image, reflecting the overall contrast or grayscale dispersion of the image; anchors the segmentation threshold to... Above this, and dynamically fluctuating based on the global pixel standard deviation; sensitivity coefficient Used to fine-tune the sensitivity of the threshold to the standard deviation; The grayscale reference used for the adaptive segmentation threshold automatically shifts with the overall image brightness, ensuring that the adaptive segmentation threshold always maintains a relatively stable distance from the most likely defect signal; global pixel standard deviation. As an adjustment factor, when the image contrast is high... A larger contrast ratio results in a higher segmentation threshold and greater tolerance; when the contrast is low, With a smaller threshold, the adaptive segmentation threshold is closer to the grayscale baseline, making detection more sensitive and giving it strong robustness to changes in illumination and defect contrast. In this embodiment, the key parameters are designed as follows: The parameter sign for the number of representative pixels in the dark area is: This defines the number of pixels sampled from the darkest region of the image to calculate a baseline value that can stably and reliably represent defects or background down to a dark gray level; The value is dynamically determined based on the total number of pixels in the image to ensure the representativeness of the statistical sample; The calculation logic is as follows: Collect a set of image samples that represent the target application scenario and include various normal background variations; for each image in the image sample set, perform the following operations: sort the grayscale values of all pixels in the image sample set in ascending order, and gradually increase the number of pixels sampled in the loop, i.e., the temporary variable here. Starting from 1, incrementing to a larger value, such as 1% of the total number of pixels, and calculating the current value at each step. The average value of the darkest pixels, denoted as ; Put all image sample sets in different The average value obtained under the given value is plotted as a curve, and it is observed that when When the value is very small, the curve of the average value fluctuates wildly due to the influence of individual noisy pixels; as... As the value increases, the curve gradually becomes smoother and converges to a stable value; The number of representative pixels in the dark area is determined as the minimum value that causes the rate of change of the average value to first remain consistently below a preset stabilization threshold (in this embodiment, the stabilization threshold is set to 0.5 gray levels). In this embodiment, through the analysis of a large number of images, it was determined that when the number of representative pixels in the dark area is 100, the calculated mean has sufficient statistical stability; the number of representative pixels in the dark area is set as a fixed percentage of the total number of pixels, which is... ; The sign of the logarithmic damping sensitivity coefficient is set as follows: ; is a dimensionless coefficient obtained through optimization and calibration, used to finely adjust the influence weight of floating terms; it determines the sensitivity of the segmentation threshold to changes in image contrast, and is the core adjustment knob for balancing oversegmentation (i.e., generating too much noise) and undersegmentation (i.e., missing real defects). The calculation logic is as follows: A verification set of images containing various typical defects and backgrounds is prepared, and a ground truth segmentation mask, precisely annotated manually, is provided for each image. An evaluation metric that can objectively measure segmentation quality is selected; in this embodiment, the Dessiere similarity coefficient, widely recognized as the gold standard, is used. The Dessiere similarity coefficient evaluates accuracy by calculating the overlap between the predicted segmentation region and the ground truth segmentation region, and its value range is [0, ]. [1] The closer the value is to 1, the better the segmentation effect; a reasonable search range for the logarithmic damped sensitivity coefficient is set from 0.1 to 5.0, and a series of candidate sensitivity coefficients are generated within this range with a fixed step size (i.e., 0.1). For each candidate sensitivity coefficient, the following evaluation is performed: initialize the score variable used to accumulate the Dessian similarity coefficient, traverse each image in the validation set, use the current candidate sensitivity coefficient and calculate the adaptive segmentation threshold, and segment the image to obtain the predicted segmentation mask, calculate the score of the Dessian similarity coefficient between the predicted segmentation mask and the corresponding ground truth mask, and accumulate it. After traversing all validation images, divide the total score of the accumulated Dessian similarity coefficient by the number of images to obtain the average Dessian similarity coefficient performance score of the candidate sensitivity coefficient; determine the optimal logarithmic damped sensitivity coefficient, select the candidate sensitivity coefficient corresponding to the highest score among the average performance scores of all candidate sensitivity coefficients, and use it as the final logarithmic damped sensitivity coefficient; in this embodiment, the determined logarithmic damped sensitivity coefficient is 1.8; The sign of the parameter for the global pixel standard deviation of the enhanced image is: The calculation logic is as follows: calculate the average gray value of all pixels in the image, then calculate the square of the difference between each pixel value and the average value, then average all the squared differences, and finally take the square root. Online adaptive threshold segmentation is as follows: Input a preprocessed single-channel grayscale enhanced image, with parameter symbols as follows: Extract all pixel grayscale values of the single-channel grayscale enhanced image into a one-dimensional array; sort the one-dimensional array in ascending order; select the smallest number of representative pixels in the foreground dark area of the one-dimensional array; Calculate the arithmetic mean of the number of representative pixels in the dark areas to obtain the minimum gray value in the enhanced image. Average value of pixels ; Calculate the global pixel standard deviation of the enhanced image of a single-channel grayscale enhanced image. ; The image with the smallest gray value in the resulting enhanced image Average value of pixels and the global pixel standard deviation of the enhanced image and logarithmic damping sensitivity coefficient ; Enhance the global pixel standard deviation of the image Add a constant Take the natural logarithm of the result to obtain the logarithmic damping value, and then multiply this logarithmic damping value by the logarithmic damping sensitivity coefficient. This yields the final dynamic float, which will enhance the image with the smallest grayscale value. Average value of pixels Add the calculated sum to the dynamic floating amount; the output sum is the final adaptive segmentation threshold. ; Create a blank binary mask image with the same size as the input single-channel grayscale enhanced image and all initial values set to 0; iterate through each pixel of the single-channel grayscale enhanced image and obtain its grayscale value; if the grayscale value of the current pixel is less than the calculated adaptive segmentation threshold... If the target / defect is not found, the corresponding pixel value, 255, is written into the corresponding position of the binary mask image; otherwise, the position is kept as the pixel value 0 representing the background. The final generated binary mask image contains the highlighted areas, which are the detected potential defects. S4. Binarize the enhanced image based on the adaptive segmentation threshold, and perform contour filtering on the binarization result to output the pixel-level segmentation result of the defect. In S4, contour filtering uses a priority logic based on the center safe zone: Define the internal area of the region of interest after removing the edges at a preset proportional width as the central safe zone; Perform connected component analysis on all binarized candidate contours and calculate the geometric center of each contour; If a contour exists whose geometric center is located within the central safety zone, it is determined to be a valid defect and retained; If there is no outline within the central safe zone, then the outline with the largest area is retained; This filters out edge artifacts caused by image cropping or filtering boundary effects; After inverse binarizing the enhanced image using an adaptive segmentation threshold and extracting all contours, intelligent filtering based on prior knowledge defines a central safe zone within the region of interest. This is achieved by shrinking the four boundaries of the region of interest inward by a preset proportion. (In this embodiment, The process involves: forming a candidate contour; calculating the center coordinates of the smallest bounding rectangle of each extracted candidate contour; determining whether the center coordinates fall within a predefined central safe zone; prioritizing the retention of contours whose center points are within the safe zone; retaining the contour with the largest area among all candidate contours if no contour is within the safe zone; utilizing the prior knowledge that defects are usually located at the center of the AOI reporting box; effectively filtering out artifacts and noise contours located at the image edges caused by inaccurate image cropping or residual effects of edge protection filtering by establishing a central safe zone; transforming the confidence of the target detection location information into filtering weights for post-segmentation processing, improving the purity and reliability of the segmentation results, and avoiding misjudging non-defect artifacts as defects.
[0030] It should be noted that all calculation formulas in this application employ regression analysis, including but not limited to machine learning algorithms, to deeply analyze the collected parameters and identify their natural trends and interrelationships. Specialized software, such as Python's Scikit-learn library or the R language, is used to automatically generate mathematical models that match the data. Then, cross-validation and other methods are used to objectively evaluate the model performance, and continuous feedback and optimization are combined to ensure that the created formulas truly reflect the inherent laws of the data, thereby guaranteeing their effectiveness and accuracy. In all calculation formulas in this application, the parameters in each formula undergo dimensionless processing within a consistent range to ensure that different physical quantities are compared on the same scale; dimensionless processing techniques include, but are not limited to, Min-Max Normalization and Z-Score standardization. The technical solution of this invention, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory, hard disk, or optical disk, etc., including several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods of various embodiments of this invention.
[0031] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus, or device (such as a computer-based system, a processor-including system, or other system that can fetch and execute instructions from, an instruction execution system, apparatus, or device). For the purposes of this specification, "computer-readable medium" can be any means that can contain, store, communicate, propagate, or transmit programs for use by, or in conjunction with, an instruction execution system, apparatus, or device.
[0032] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.
Claims
1. A method for accurate defect segmentation based on adaptive gradient enhancement and statistical thresholding, characterized in that, The specific steps include: S1. Obtain a small defect image containing the defect to be segmented; perform binarization on the small defect image to locate the panel region in the image, and generate the region of interest based on the panel region to remove background interference from non-panel regions. S2. Perform image enhancement processing on the region of interest to generate an enhanced image that suppresses background texture and enhances defect edges; the image enhancement processing includes multi-directional gradient calculation and fusion of the region of interest, and applying adaptive recursive filtering based on domain transformation to the fused gradient information. S3. Calculate an adaptive segmentation threshold based on the pixel statistical features of the enhanced image; the pixel statistical features include the global standard deviation of the enhanced image and the average value of a preset number of the darkest pixels that can represent the baseline of the dark areas of the image. S4. Binarize the enhanced image based on the adaptive segmentation threshold, and perform contour filtering on the binarization result to output the pixel-level segmentation result of the defect.
2. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 1, characterized in that: In step S2, the specific implementation logic of the adaptive recursive filtering process based on domain transformation is as follows: The image signal is mapped from the spatial domain to the transform domain. The transform domain distance between adjacent pixels is defined in the transform domain. The transform domain distance depends not only on the spatial proximity of the pixels, but also on the difference in gradient values between adjacent pixels. This makes the transform domain distance close to the physical distance in flat gray-scale regions, while the transform domain distance is stretched and amplified in edge regions. Based on the transform domain distance, a dynamic feedback coefficient is dynamically calculated for each pixel; The recursive filtering iteration is performed using the dynamic feedback coefficient, wherein the dynamic feedback coefficient determines the influence weight of the previous pixel's filtered output on the current pixel.
3. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 2, characterized in that: The adjustment mechanism of the dynamic feedback coefficient is as follows: When the transform domain distance between adjacent pixels is small, the generated dynamic feedback coefficients tend to retain the filtering result of the previous pixel, thereby producing a strong smoothing effect to suppress texture. When the transform domain distance between adjacent pixels increases significantly due to a sudden gradient change, the generated dynamic feedback coefficients tend to cut off the influence of the previous pixel, thereby preserving the original value of the current pixel to protect the edge. The recursive filtering iteration performs bidirectional traversal of the image's rows and columns to eliminate phase delay.
4. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 2, characterized in that: Calculating the transform domain distance involves the dynamic calculation of spatial domain smoothing parameters and range domain sensitivity parameters: The spatial domain smoothing parameters are adaptively determined proportionally to the size of the region of interest to adapt to defect minimaps of different sizes. The range sensitivity parameter is adaptively determined proportionally to the dynamic range of the input gradient map to adapt to image environments with different contrasts.
5. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 1, characterized in that: In step S3, the calculation logic for the adaptive segmentation threshold is as follows: A dark area anchoring strategy is adopted, and the arithmetic mean of the K pixels with the smallest gray values in the enhanced image is calculated as the dark area reference value that can respond to weak defects. Calculate the global pixel standard deviation of the enhanced image as a floating term reflecting the overall contrast of the image; The adaptive segmentation threshold is obtained by adding the dark area reference value to the floating term weighted by the sensitivity coefficient. The K value is dynamically determined based on the total number of pixels in the image to ensure the representativeness of the statistical sample.
6. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 1, characterized in that: Step S2 also includes: The gradient map after the adaptive recursive filtering process is weighted and fused with the original grayscale image of the region of interest to generate the final enhanced image. The weighted fusion aims to superimpose the enhanced edge features after filtering with the brightness features of the original image, so that the enhanced image simultaneously possesses the contour information of defects and the brightness information of the body.
7. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 1, characterized in that: The defects to be segmented are obtained as follows: Acquire high-resolution product images, perform preliminary scanning of the acquired product images using target detection algorithms to identify potential defect targets; output the coordinates of a rectangular box containing defect location information, as well as the corresponding original high-resolution image; the area within the rectangular box coordinates is the small defect image of the defect to be segmented.
8. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 7, characterized in that: The steps for cropping and preprocessing the defect thumbnail are as follows: Based on the received bounding box coordinates, a small defect image containing the defect target is cropped from the original high-resolution image; the cropped color image is converted into a single-channel grayscale image; and the RGB three-channel image is converted into a grayscale image.
9. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 1, characterized in that: In S1, the defect mini-image is subjected to global threshold binarization using the Otsu method to distinguish the background of the panel area from that of the non-panel area; and the maximum contour of the panel area is extracted. The defect mini-image is then cropped a second time based on the bounding rectangle of the maximum contour to generate the region of interest.
10. The defect precision segmentation method based on adaptive gradient enhancement and statistical thresholding according to claim 1, characterized in that: In S4, contour filtering uses a priority logic based on the center safe zone: Define the internal area of the region of interest after removing the edges at a preset proportional width as the central safe zone; Perform connected component analysis on all binarized candidate contours and calculate the geometric center of each contour; If a contour exists whose geometric center is located within the central safety zone, it is determined to be a valid defect and retained; If there is no outline within the central safe zone, then the outline with the largest area is retained; This filters out edge artifacts caused by image cropping or filtering boundary effects.
Citation Information
Patent Citations
A defect detection method for electronic components
CN115018828B