Image defect positioning method and system based on visual detection
By employing multi-scale decomposition and cross-resolution spatial mapping methods, combined with pixel gradient and local neighborhood analysis, the problem of inaccurate defect detection under complex textures and lighting variations in existing technologies is solved, achieving higher accuracy and more stable defect localization.
Patent Information
- Application Number
- CN202611290819.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-25
- Publication Date
- 2026-11-24
AI Technical Summary
Existing machine vision-based image defect detection methods are prone to misjudgment under complex textures and local lighting variations, and lack cross-scale spatial correlation and boundary fusion mechanisms, resulting in inaccurate defect localization.
Image components are obtained through multi-scale decomposition. Texture abrupt changes and gray-level anomaly regions are identified by combining pixel gradient direction consistency and gradient magnitude distribution. Defect locations are screened by the degree of deviation between the local neighborhood and the surrounding reference region. Cross-resolution spatial mapping and boundary fusion are then performed to obtain the defect contour.
It improves the accuracy and stability of defect detection in complex image scenes, reduces the false detection rate, and enhances the integrity of defect contours and positioning accuracy.
Smart Images

Figure CN123160467A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing and defect detection technology, specifically to a method and system for locating image defects based on visual detection. Background Technology
[0002] In existing technologies, image defect detection based on machine vision typically identifies defect regions through image segmentation, edge detection, texture analysis, or feature extraction. However, in actual detection processes, defect size, shape, and grayscale differences vary considerably. Single-scale image analysis can easily lead to the loss of features of minor defects or incomplete representation of larger defect regions. Furthermore, complex textures, localized lighting variations, and grayscale fluctuations can easily be confused with genuine defect features, causing normal textures or grayscale anomalies to be misidentified as defects, thus affecting the accuracy of defect detection.
[0003] Existing methods typically filter candidate defect regions based on a single threshold or local features, making it difficult to fully utilize the statistical differences between the defect region and the surrounding background, which can easily lead to false positives or false negatives. For the same defect obtained at different resolution levels, due to differences in defect location, boundary range, and feature representation, existing methods usually lack effective cross-scale spatial correlation and boundary fusion mechanisms, which can easily cause defect localization offsets, incomplete contours, or repeated detection of the same defect, thus limiting the accuracy of defect localization under complex image conditions. Summary of the Invention
[0004] The purpose of this invention is to provide a method and system for locating image defects based on visual detection, aiming to solve at least one of the technical problems existing in the prior art.
[0005] The technical solution of this invention is: an image defect localization method based on visual detection, comprising the following steps: The image to be detected is acquired, and the image to be detected is decomposed into multiple scales to obtain image components at multiple resolution levels. The pixel gradient direction consistency and pixel gradient magnitude distribution within the image components of each resolution level are calculated respectively. Based on the pixel gradient direction consistency and pixel gradient magnitude distribution, texture change regions and gray-level abnormal regions are identified to obtain the candidate defect locations for each resolution level. For candidate defect locations at each resolution level, local neighborhoods and peripheral reference regions are extracted. The degree of deviation between the pixel statistical features of the local neighborhoods and the pixel statistical features of the peripheral reference regions is calculated. Based on the degree of deviation, the selected defect locations are obtained. The filtered defect locations are spatially mapped across multiple resolution levels. The boundary information of candidate defect locations at different resolution levels belonging to the same defect is aligned and fused to obtain the defect contour. The defect contour is marked on the image to be inspected, and the marked image is output.
[0006] The pixel gradient direction consistency and pixel gradient magnitude distribution within the image components of each resolution level are calculated respectively. Based on the pixel gradient direction consistency and pixel gradient magnitude distribution, texture abrupt change regions and grayscale anomaly regions are identified, resulting in candidate defect locations for each resolution level, including: The concentration of gradient directions in the neighborhood of each pixel in the image component of each resolution level is calculated to obtain the pixel gradient direction consistency of each resolution level. The gradient magnitude of all pixels in each resolution level is statistically analyzed to obtain the pixel gradient magnitude distribution of each resolution level. The thresholds for determining texture mutations and grayscale anomalies are determined based on the pixel gradient magnitude distribution. Pixels whose gradient direction consistency is lower than the texture mutation determination threshold are marked as texture mutation candidate points, and pixels whose gradient magnitude exceeds the gray-scale anomaly determination threshold in the pixel gradient magnitude distribution are marked as gray-scale anomaly candidate points. Spatial location comparison is performed on candidate points for texture mutation and candidate points for grayscale anomalies, and pixels with overlapping spatial locations are selected as core candidate points; Connectivity analysis is performed on the core candidate points. Texture mutation candidate points and gray-level anomaly candidate points that are spatially connected to the core candidate points are merged to form connected regions. The regions where texture mutation candidate points are located in the connected regions are identified as texture mutation regions, and the regions where gray-level anomaly candidate points are located are identified as gray-level anomaly regions. Texture mutation regions and gray-level anomaly regions are used as candidate defect locations for each resolution level.
[0007] Determining the texture mutation and grayscale anomaly detection thresholds based on pixel gradient magnitude distribution includes: The pixel gradient magnitude distribution at each resolution level is sorted by quantile, and the pixel gradient magnitude at the preset quantile position is used to obtain the initial texture mutation judgment threshold. Calculate the mean of the initial texture mutation detection threshold for each resolution level, and normalize the initial texture mutation detection threshold with the mean to obtain the texture mutation detection threshold. The numerical difference is calculated based on the pixel gradient magnitude at different quantile positions after quantile sorting. The ratio of the numerical difference to the texture mutation judgment threshold is then used to obtain the grayscale anomaly judgment threshold.
[0008] For candidate defect locations at each resolution level, local neighborhoods and surrounding reference regions are extracted. The deviation between the pixel statistical features of the local neighborhoods and the pixel statistical features of the surrounding reference regions is calculated. Based on the deviation, the selected defect locations are obtained, including: For candidate defect locations at each resolution level, a local neighborhood is determined with the candidate defect location as the center, and an outer reference region is determined based on the boundary of the local neighborhood. The pixel statistical features of the local neighborhood are obtained by calculating the mean gray value and the variance of the gray value of the pixels in the local neighborhood, and the pixel statistical features of the outer reference area are obtained by calculating the mean gray value and the variance of the gray value of the pixels in the outer reference area. The deviation magnitude of the pixel statistical features of the local neighborhood is quantified based on the pixel statistical features of the outer reference region to obtain the deviation metric value of each resolution level. The deviation metric values at each resolution level are weighted and fused across different levels to obtain the degree of deviation. The screening threshold is determined based on the distribution characteristics of the degree of deviation, and the candidate defect positions with a deviation greater than the screening threshold are retained as the screening defect positions.
[0009] The filtered defect locations are spatially mapped across multiple resolution levels. Boundary information of candidate defect locations at different resolution levels belonging to the same defect is aligned and fused to obtain the defect contour, which includes: Based on the scale ratio between each resolution level and the original image, the spatial coordinates of the filtered defect locations at each resolution level are transformed into the spatial coordinate system of the original image to obtain the mapped defect locations. Calculate the spatial distance between the mapped defect locations, determine the aggregation threshold based on the spatial distance distribution of the mapped defect locations, and classify the mapped defect locations with spatial distances less than the aggregation threshold into defect clusters belonging to the same defect. For each defect cluster, the boundary information of the corresponding mapped defect position in each resolution level is extracted, and the boundary information is transformed into the original image space coordinate system to obtain the aligned boundary information. The fusion weights are determined based on the resolution level corresponding to the aligned boundary information, and the aligned boundary information of each defect cluster is weighted and fused to obtain the defect contour.
[0010] Determining the aggregation threshold based on the spatial distance distribution of the mapped defect locations includes: The spatial distance distribution of the mapped defect locations is segmented and analyzed. The rate of change between adjacent spatial distances in the spatial distance distribution of the mapped defect locations is calculated, and the location corresponding to the maximum rate of change is identified as the segmentation point. Extract the spatial distance value corresponding to the segment point as the aggregation threshold.
[0011] The fusion weights are determined based on the resolution level corresponding to the aligned boundary information, including: The frequency of occurrence of each resolution level in the resolution level corresponding to the aligned boundary information is statistically analyzed. Calculate the image sharpness of the image corresponding to each resolution level as the level quality coefficient; The fusion weight is obtained by combining the frequency of occurrence with the hierarchical quality coefficient.
[0012] This invention provides a visual inspection-based image defect localization system, the system comprising: The image decomposition module is used to acquire the image to be detected, decompose the image to be detected into multiple scales, and obtain image components at multiple resolution levels. The candidate defect identification module is used to calculate the consistency of pixel gradient direction and the distribution of pixel gradient magnitude in the image components of each resolution level, and to identify texture change regions and grayscale abnormal regions based on the consistency of pixel gradient direction and the distribution of pixel gradient magnitude, so as to obtain the location of candidate defects at each resolution level. The defect screening module is used to extract the local neighborhood and the surrounding reference area for candidate defect locations at each resolution level, calculate the degree of deviation between the pixel statistical features of the local neighborhood and the pixel statistical features of the surrounding reference area, and obtain the screened defect locations based on the degree of deviation. The contour fusion module is used to spatially map the filtered defect locations across multiple resolution levels, align and fuse the boundary information of candidate defect locations at different resolution levels belonging to the same defect, and obtain the defect contour. The annotation output module is used to annotate the image to be inspected based on the defect contour and output the annotated image.
[0013] One technical solution provided in this embodiment of the invention is an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps in any of the aforementioned methods.
[0014] One technical solution provided in this embodiment of the invention is a computer-readable storage medium storing computer program instructions, which, when executed by a processor, implement the steps in any of the aforementioned methods.
[0015] This invention improves the ability to characterize defects of different sizes by performing multi-scale decomposition on the image to be detected, extracting defect features from different resolution levels; by combining pixel gradient direction consistency and gradient magnitude distribution to identify texture abrupt changes and gray-level anomaly regions, it can expand the detection range of defect candidate regions; by comparing the deviation of pixel statistical features between local neighborhoods and surrounding reference regions, it can reduce false detections caused by complex textures and local gray-level fluctuations; and by cross-resolution spatial mapping and alignment fusion of the same defect boundary, it can improve the integrity of defect contours and positioning accuracy, thereby enhancing the accuracy and stability of defect detection in complex image scenes. Attached Figure Description
[0016] Figure 1 A flowchart of an image defect localization method based on visual detection provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of the structure of the image defect localization system based on visual detection according to an embodiment of the present invention. Detailed Implementation
[0017] like Figure 1 As shown, Figure 1 A flowchart of a vision-based image defect localization method provided in an embodiment of the present invention, the method comprising the following steps: Step 101: Obtain the image to be detected, and perform multi-scale decomposition on the image to be detected to obtain image components at multiple resolution levels.
[0018] Step 102: Calculate the pixel gradient direction consistency and pixel gradient magnitude distribution within the image components of each resolution level. Identify texture abrupt change regions and grayscale abnormal regions based on the pixel gradient direction consistency and pixel gradient magnitude distribution to obtain the candidate defect locations for each resolution level.
[0019] In some embodiments of the present invention, step 102 may specifically include the following sub-steps: Sub-step 1021: Calculate the concentration of gradient directions in the neighborhood of each pixel in the image component of each resolution level to obtain the pixel gradient direction consistency of each resolution level, and count the gradient magnitude of all pixels in each resolution level to obtain the pixel gradient magnitude distribution of each resolution level. Sub-step 1022: Determine the texture mutation judgment threshold and grayscale anomaly judgment threshold based on the pixel gradient magnitude distribution; Sub-step 1023: Mark pixels whose gradient direction consistency is lower than the texture mutation determination threshold as texture mutation candidate points, and mark pixels whose gradient magnitude exceeds the gray-level anomaly determination threshold in the pixel gradient magnitude distribution as gray-level anomaly candidate points. Sub-step 1024: Spatial position comparison of texture mutation candidate points and grayscale anomaly candidate points, and use pixels with overlapping spatial positions as core candidate points. Sub-step 1025: Perform connected component analysis on the core candidate points, merge the texture mutation candidate points and gray-level anomaly candidate points that are spatially connected to the core candidate points to form a connected region, identify the region where the texture mutation candidate points are located in the connected region as the texture mutation region, identify the region where the gray-level anomaly candidate points are located as the gray-level anomaly region, and use the texture mutation region and the gray-level anomaly region as the candidate defect locations for each resolution level.
[0020] After acquiring the image to be detected, it is decomposed into multiple resolution levels to obtain image components at various scales. This multi-scale decomposition employs a Gaussian pyramid or Laplacian pyramid method, progressively downsampling the original image to form an image sequence from high to low resolution. The number of resolution levels is determined based on the image size and the scale of the defect features, ranging from 3 to 6 levels, preferably 4. Different resolution levels correspond to defect features at different scales. High-resolution levels preserve detailed texture information, while low-resolution levels highlight overall grayscale variation features. Multi-scale decomposition provides the data foundation for subsequent multi-level feature extraction.
[0021] The pixel gradient direction consistency and pixel gradient magnitude distribution are calculated for each resolution level of the image component. For each resolution level, the concentration of gradient directions within the neighborhood of each pixel is calculated to obtain the pixel gradient direction consistency, and the gradient magnitude of all pixels within each resolution level is statistically analyzed to obtain the pixel gradient magnitude distribution. The pixel neighborhood is represented by a rectangular window centered on the current pixel. The window size is adjusted according to the resolution level; a smaller window is used for high-resolution levels to capture local changes, and a larger window is used for low-resolution levels to reflect regional characteristics. After calculating the gradient direction of each pixel within the neighborhood, the concentration of gradient directions is evaluated by the standard deviation of the gradient direction vector or the entropy value of the direction histogram. High concentration indicates consistent texture direction in the region, while low concentration indicates chaotic texture direction or abrupt changes. The pixel gradient magnitude distribution is obtained by statistically analyzing the gradient magnitudes of all pixels within each resolution level to form an magnitude histogram, reflecting the overall edge intensity distribution characteristics of that level.
[0022] The texture mutation and grayscale anomaly detection thresholds are determined based on the pixel gradient magnitude distribution. The texture mutation threshold is determined based on the statistical distribution of pixel gradient direction consistency. The mean and standard deviation of the gradient direction consistency of all pixels within each resolution level are calculated, and the value obtained by subtracting one to two times the standard deviation from the mean is used as the texture mutation threshold, preferably the mean minus 1.5 times the standard deviation. The grayscale anomaly detection threshold is determined based on the percentiles of the pixel gradient magnitude distribution. The values from the 90th to the 98th percentile of the gradient magnitude distribution are selected as the grayscale anomaly detection threshold, preferably the 95th percentile. By adaptively determining the thresholds, the inadequacy of fixed thresholds in different image scenarios is avoided, ensuring that the threshold settings match the actual feature distribution of the current image.
[0023] Pixels with gradient direction consistency below the texture abruptness threshold are marked as texture abruptness candidate points, while pixels with gradient magnitudes exceeding the grayscale anomaly threshold are marked as grayscale anomaly candidate points. Texture abruptness candidate points correspond to regions where the texture direction changes drastically, typically appearing at defect edges or surface discontinuities, but they may also be misjudged due to noise. Grayscale anomaly candidate points correspond to regions with significant grayscale intensity changes, reflecting abnormal optical reflection characteristics at the defect, but a single gradient magnitude constraint cannot distinguish defect edges from normal structure edges. These two types of candidate points characterize defect features from the dimensions of texture continuity and grayscale abruptness, respectively, providing complementary information for subsequent spatial correlation analysis.
[0024] Spatial location comparison is performed on candidate points for texture abrupt changes and candidate points for grayscale anomalies, and pixels with overlapping spatial locations are selected as core candidate points. The determination of spatial overlap uses direct pixel coordinate matching or matching within a neighborhood range, with the neighborhood range set to 3 to 5 pixels, preferably 3 pixels. Core candidate points simultaneously satisfy both the conditions of texture direction abrupt change and gradient magnitude anomaly, indicating that the location exhibits both texture discontinuity and significant grayscale change, belonging to a high-confidence region of defect features. This dual constraint mechanism eliminates noise interference caused by single feature anomalies, reducing the false detection rate.
[0025] Connectivity analysis is performed on the core candidate points, merging texture mutation candidate points and grayscale anomaly candidate points spatially connected to the core candidate points to form connected regions. This connectivity analysis employs eight-neighbor connectivity determination, traversing each core candidate point and searching for texture mutation and grayscale anomaly candidate points within its neighborhood, progressively merging spatially adjacent candidate points into the same connected region. Based on the high-confidence features of the core candidate points, the connectivity analysis expands the identification range outwards, integrating scattered candidate points into a complete defect region. During the merging process, texture mutation candidate points and grayscale anomaly candidate points retain their respective type identifiers, providing a basis for subsequent region type identification.
[0026] The regions containing candidate points for texture abrupt changes in the connected region are identified as texture abrupt change regions, and the regions containing candidate points for grayscale anomalies are identified as grayscale anomaly regions. These regions are used as candidate defect locations for each resolution level. The texture abrupt change regions mainly include candidate points for texture abrupt changes obtained by expanding the core candidate points, reflecting the surface texture discontinuities caused by defects. The grayscale anomaly regions mainly include candidate points for grayscale anomalies obtained by expanding the core candidate points, reflecting the optical response anomalies caused by defects. Each resolution level independently identifies candidate defect locations, forming a multi-scale defect candidate set. Candidate defect locations at different scales are then matched and verified across levels in subsequent fusion steps. By integrating multi-scale information, the final defect location is determined, improving positioning accuracy.
[0027] This invention obtains feature representations at different scales through multi-scale decomposition, utilizes the dual constraint mechanism of gradient direction consistency and gradient magnitude distribution to identify defect features from the perspectives of texture continuity and grayscale variation, and achieves accurate segmentation and localization of defect regions through core candidate point screening and connected component analysis.
[0028] Sub-step 1022, determining the texture mutation judgment threshold and grayscale anomaly judgment threshold based on pixel gradient magnitude distribution, further includes: The pixel gradient magnitude distribution at each resolution level is sorted by quantile, and the pixel gradient magnitude at the preset quantile position is used to obtain the initial texture mutation judgment threshold. Calculate the mean of the initial texture mutation detection threshold for each resolution level, and normalize the initial texture mutation detection threshold with the mean to obtain the texture mutation detection threshold. The numerical difference is calculated based on the pixel gradient magnitude at different quantile positions after quantile sorting. The ratio of the numerical difference to the texture mutation judgment threshold is then used to obtain the grayscale anomaly judgment threshold.
[0029] The pixel gradient magnitude distribution at each resolution level is sorted by quantiles. This quantile sorting arranges the gradient magnitudes of all pixels within each resolution level in ascending order of numerical value, establishing a cumulative distribution sequence of gradient magnitudes. Quantile sorting eliminates the influence of absolute gradient magnitude values, transforming the gradient magnitude distributions across different levels into a unified probability distribution, providing a comparable statistical basis for determining cross-level thresholds.
[0030] An initial texture abrupt change threshold is obtained by statistically analyzing the pixel gradient magnitude at a preset quantile position. The preset quantile position is determined based on the statistical characteristics of texture abrupt changes in the gradient magnitude distribution. The gradient magnitude corresponding to a texture abrupt change region is typically in a medium intensity range, distinct from both the low gradient magnitude of flat areas and the high gradient magnitude of strong edges. The preset quantile position is set from the 30th to the 50th percentile, preferably the 40th percentile. The gradient magnitude of each resolution level at the preset quantile position is extracted as the initial texture abrupt change threshold for that level. This initial threshold reflects the typical intensity level of texture changes at each level.
[0031] The mean of the initial texture mutation detection thresholds for each resolution level is calculated, and the initial texture mutation detection thresholds are normalized to obtain the texture mutation detection thresholds. The normalization process is implemented as follows: the arithmetic mean of the initial texture mutation detection thresholds for all resolution levels is calculated as the normalization benchmark, and the initial texture mutation detection threshold for each level is divided by this mean to obtain the normalized texture mutation detection threshold. Normalization eliminates the gradient magnitude scale deviation caused by differences in sampling density between different resolution levels, ensuring that the thresholds for each level have uniform dimensions and comparability. This avoids the problem of high thresholds at high resolution levels due to high pixel density, or low thresholds at low resolution levels due to low pixel density. The normalized texture mutation detection thresholds maintain relatively consistent detection sensitivity across all levels, adapting to the needs of collaborative analysis of multi-scale features.
[0032] The numerical difference is calculated based on the pixel gradient magnitude at different quantile positions after quantile sorting. This numerical difference is then ratioed to a texture mutation threshold to obtain a grayscale anomaly determination threshold. The numerical difference is calculated by selecting gradient magnitudes at two specific quantile positions, corresponding to the medium-intensity and high-intensity regions of the gradient magnitude distribution, respectively. The quantile position in the medium-intensity region is the same as or close to the preset quantile position corresponding to the texture mutation determination threshold. The quantile position in the high-intensity region is set between the 85th and 98th percentiles, preferably the 90th percentile. The numerical difference equals the gradient magnitude at the high-intensity region quantile position minus the gradient magnitude at the medium-intensity region quantile position. This difference reflects the gradient magnitude interval between strong edges and medium texture variations in the current resolution level. The grayscale anomaly determination threshold is obtained by ratioing the numerical difference to the texture mutation determination threshold. The calculation method is to add the texture mutation determination threshold to the product of the numerical difference and an adjustment coefficient, which is set between 0.5 and 1.5, preferably 1.0. The ratio operation establishes a numerical correlation between the texture mutation judgment threshold and the gray-level anomaly judgment threshold, so that the gray-level anomaly judgment threshold is adaptively adjusted based on the dynamic range of the gradient amplitude distribution on the basis of the texture mutation judgment threshold, ensuring that there is a reasonable numerical interval between the two thresholds and avoiding excessive overlap or complete separation between gray-level anomaly candidate points and texture mutation candidate points.
[0033] The initial threshold is determined by quantile statistics, avoiding interference from extreme points in the threshold calculation and improving the stability of the threshold. Cross-level normalization processing provides a unified detection benchmark for texture mutation detection thresholds at each resolution level, eliminating scale bias introduced by multi-scale decomposition. The gray-level anomaly detection threshold is calculated based on the dynamic range of gradient magnitude distribution, maintaining its correlation with the texture mutation detection threshold. The two thresholds work synergistically in the subsequent candidate point identification process: the texture mutation detection threshold filters regions with discontinuous texture directions, while the gray-level anomaly detection threshold filters regions with significant changes in gray-level intensity. This dual constraint mechanism improves the accuracy of defect feature identification.
[0034] In practical applications, for objects with complex surface textures, the gradient amplitude distribution exhibits a wide-range dispersion with large numerical differences, leading to a correspondingly higher threshold for grayscale anomaly detection and preventing the misclassification of normal texture edges as defects. For objects with uniform surface textures, the gradient amplitude distribution exhibits a narrow-range concentration with smaller numerical differences, resulting in a correspondingly lower threshold for grayscale anomaly detection and improved sensitivity for detecting minute defects. Normalization and ratio calculations enable the threshold determination process to adapt to the gradient amplitude distribution characteristics of the current image, eliminating the need for manual adjustments for different objects and improving the method's versatility and practicality.
[0035] This invention achieves adaptive determination of texture mutation judgment threshold and grayscale anomaly judgment threshold through quantile statistics, cross-level normalization and correlation calculation. The two thresholds are generated collaboratively based on a unified gradient amplitude distribution feature, which not only maintains their respective detection capabilities for different defect features, but also avoids the arbitrariness and inconsistency of threshold setting, providing a reliable judgment basis for subsequent multi-scale defect feature recognition.
[0036] Step 103: For candidate defect locations at each resolution level, extract the local neighborhood and the surrounding reference region, calculate the degree of deviation between the pixel statistical features of the local neighborhood and the pixel statistical features of the surrounding reference region, and filter the defect locations based on the degree of deviation.
[0037] In some embodiments of the present invention, step 103 may specifically include the following sub-steps: Sub-step 1031: For the candidate defect locations at each resolution level, determine the local neighborhood with the candidate defect location as the center, and determine the outer reference area based on the boundary of the local neighborhood. Sub-step 1032: Calculate the gray mean and gray variance of pixels in the local neighborhood to obtain the pixel statistical features of the local neighborhood, and calculate the gray mean and gray variance of pixels in the outer reference area to obtain the pixel statistical features of the outer reference area. Sub-step 1033: Based on the pixel statistical features of the outer reference region, quantify the deviation of the pixel statistical features of the local neighborhood to obtain the deviation metric value of each resolution level. Sub-step 1034: Perform cross-level weighted fusion of the deviation metric values for each resolution level to obtain the degree of deviation; Sub-step 1035: Determine the screening threshold based on the distribution characteristics of the deviation degree, and retain the candidate defect positions with a deviation degree greater than the screening threshold as the screening defect positions.
[0038] For candidate defect locations at each resolution level, a local neighborhood is determined centered on the candidate defect location. This local neighborhood covers the candidate defect location and its surrounding pixels, capturing the local representation of the defect features. The local neighborhood can be a rectangular or circular region. The side length of the rectangular region ranges from 5 to 15 pixels, and the radius of the circular region ranges from 3 to 8 pixels. The specific size is determined based on the sampling density of the current resolution level; higher resolution levels use larger sizes to cover the complete defect structure, while lower resolution levels use smaller sizes to avoid including too much background information. An outer reference region is determined based on the boundary of the local neighborhood. This outer reference region surrounds the local neighborhood, maintaining a spatial distance to prevent defect edges from contaminating the reference region. The distance between the inner boundary of the outer reference region and the outer boundary of the local neighborhood ranges from 2 to 6 pixels, preferably 3 pixels. The width of the outer reference region ranges from 8 to 20 pixels, preferably 12 pixels. The outer reference region provides statistical features of the normal region surrounding the defect as a comparison benchmark. Its size should be large enough to obtain stable statistics while avoiding excessive size that might include other defects or texture variation areas.
[0039] The pixel statistical characteristics of a local neighborhood are obtained by statistically analyzing the mean gray value and gray variance of pixels within that neighborhood. The mean gray value reflects the average brightness level of the local neighborhood and is obtained by summing the gray values of all pixels within the neighborhood and dividing by the total number of pixels. The gray variance reflects the dispersion of the gray value distribution in the local neighborhood and is obtained by averaging the sum of the squared differences between each pixel's gray value and the mean gray value. The pixel statistical characteristics of the outer reference region are obtained by statistically analyzing the mean gray value and gray variance of pixels within that region, using the same calculation method as for the local neighborhood. The pixel statistical characteristics of the outer reference region represent the normal background characteristics around the candidate defect location, providing a reference for determining whether anomalies exist in the local neighborhood.
[0040] The deviation magnitude of the pixel statistical features of the local neighborhood is quantified based on the pixel statistical features of the outer reference region to obtain the deviation metric value for each resolution level. The deviation magnitude includes the gray-level mean deviation magnitude and the gray-level variance deviation magnitude. The gray-level mean deviation magnitude is calculated by the absolute value of the difference between the gray-level mean of the local neighborhood and the gray-level mean of the outer reference region. The gray-level variance deviation magnitude is calculated by the absolute value of the difference between the gray-level variance of the local neighborhood and the gray-level variance of the outer reference region. To eliminate gray-level benchmark differences between different resolution levels and different image regions, the deviation magnitude is normalized. The gray-level mean deviation magnitude is divided by the gray-level mean of the outer reference region to obtain the normalized gray-level mean deviation. The gray-level variance deviation magnitude is divided by the sum of the gray-level variance of the outer reference region and a preset stability term to obtain the normalized gray-level variance deviation. The preset stability term is set in the range of 1 to 10, preferably 5, to avoid numerical instability when the gray-level variance of the outer reference region is too small. The deviation metric is obtained by weighted summing of the deviation from the normalized grayscale mean and the deviation from the normalized grayscale variance. The calculation method is as follows: Among them, D l μ is the deviation metric for the l-th resolution level. i μ is the average gray level of the local neighborhood. o The average gray value of the outer reference area. The gray-level variance of the local neighborhood. ϵ represents the grayscale variance of the outer reference area, α is a preset stability term, and β are the fusion weights for grayscale mean deviation and grayscale variance deviation, respectively. α is set from 0.4 to 0.7, preferably 0.6, and β is set from 0.3 to 0.6, preferably 0.4, with α + β = 1. Grayscale mean deviation is sensitive to overall brightness changes caused by defects, while grayscale variance deviation is sensitive to texture inhomogeneity caused by defects. Weighted fusion integrates the deviations of these two features.
[0041] The deviation metrics at each resolution level are weighted and fused across different levels to obtain the degree of deviation. This cross-level weighted fusion considers the differences in defect feature detection capabilities across different resolution levels. High-resolution levels retain more detailed information and are sensitive to small-sized defects and edge details, while low-resolution levels suppress noise interference and are sensitive to the overall structural features of defects. The fusion weight for each resolution level is determined based on its reliability. Reliability is assessed through the stability of pixel statistical features in the surrounding reference region. A level with a smaller gray-level variance and a gray-level mean similar to adjacent levels has higher reliability and is assigned a larger weight. The fusion weights are normalized across levels so that the sum of the weights is 1. The degree of deviation is obtained by summing the products of the deviation metrics at each level and their corresponding fusion weights, thus integrating multi-scale information to improve the accuracy and robustness of the determination.
[0042] The screening threshold is determined based on the distribution characteristics of the deviation degree. These distribution characteristics are obtained by statistically analyzing the deviation degrees of all candidate defect locations. The deviation degree of the actual defect location is typically significantly higher than that of the false alarm point, forming a high-value cluster in the deviation degree distribution. The deviation degree sequence is sorted in descending order, and the gradient change of the sequence is statistically analyzed. The location where the gradient change shows a significant decrease corresponds to the boundary between the actual defect and the false alarm point. The screening threshold is set as the deviation degree value at this boundary point, or as a multiple of the sum of the mean and standard deviation of the deviation degree, with the multiple coefficient ranging from 1.5 to 3.0, preferably 2.0. Candidate defect locations with a deviation degree greater than the screening threshold are retained as filtered defect locations, while candidate defect locations with a deviation degree less than or equal to the screening threshold are identified as false alarm points and eliminated.
[0043] By comparing local neighborhoods with peripheral reference regions, defect determination is transformed from absolute feature thresholds to relative deviation assessments, adapting to defect detection needs under different background conditions. The peripheral reference region provides a local background benchmark for candidate defect locations, eliminating the influence of global illumination variations and texture differences. Cross-level weighted fusion integrates multi-scale information; high-resolution layers capture defect details, while low-resolution layers suppress noise interference, resulting in a fusion outcome that balances detection sensitivity and anti-interference capabilities. An adaptive selection threshold based on the deviation distribution characteristics avoids the applicability issues of fixed thresholds in different detection scenarios, improving selection accuracy.
[0044] This invention effectively distinguishes between real defect locations and false alarm points by measuring the deviation between local neighborhood statistical features and peripheral reference area statistical features, combined with multi-resolution layer weighted fusion and adaptive threshold filtering, thereby reducing the computational burden of subsequent localization processing and improving the accuracy and reliability of defect localization.
[0045] Step 104: Spatial mapping of the filtered defect locations across multiple resolution levels; alignment and fusion of the boundary information of candidate defect locations at different resolution levels belonging to the same defect to obtain the defect contour.
[0046] In some embodiments of the present invention, step 104 may specifically include the following sub-steps: Sub-step 1041: Based on the scale ratio between each resolution level and the original image, transform the spatial coordinates of the filtered defect locations at each resolution level to the spatial coordinate system of the original image to obtain the mapped defect locations. Sub-step 1042: Calculate the spatial distance between the mapped defect locations, determine the aggregation threshold based on the spatial distance distribution of the mapped defect locations, and divide the mapped defect locations with spatial distances less than the aggregation threshold into defect clusters belonging to the same defect. Sub-step 1043: Extract the boundary information of the corresponding mapped defect position in each resolution level for each defect cluster, and transform the boundary information to the original image space coordinate system to obtain the aligned boundary information; Sub-step 1044: Determine the fusion weight based on the resolution level corresponding to the aligned boundary information, and perform weighted fusion on the aligned boundary information of each defect cluster to obtain the defect contour.
[0047] Based on the scale ratio between each resolution level and the original image, the spatial coordinates of the filtered defect locations at each resolution level are transformed into the spatial coordinate system of the original image. Each resolution level is obtained by downsampling the original image, and the scale ratio of each level relative to the original image is determined during the downsampling process. For multi-resolution representation using a pyramid structure, the scale ratio between adjacent levels is set to a range of 1.5 to 2.5, preferably 2.0, meaning that the image size of each level is half that of the level above. The filtered defect locations are represented by pixel coordinates in each resolution level. Coordinate transformation is achieved by multiplying the pixel coordinates of that level by the scale ratio of that level relative to the original image. The transformed coordinates are in pixels, consistent with the spatial coordinate system of the original image. Due to pixel merging and rounding during the downsampling process, the transformed coordinates may contain sub-pixel offsets; the decimal part of the coordinates is retained for accurate alignment in subsequent boundary fusion. The obtained mapped defect locations establish a correspondence between defect information of different resolution levels in a unified spatial coordinate system, providing a spatial reference for cross-level defect association.
[0048] The spatial distance between mapped defect locations is calculated using Euclidean distance, obtained by calculating the square root of the sum of the squares of the differences in the x-coordinates and y-coordinates of two mapped defect locations in the original image spatial coordinate system. When the same defect is detected at different resolution levels, mapped defect locations are spatially close to each other with a small spatial distance, while mapped defect locations of different defects are spatially far apart with a large spatial distance. An aggregation threshold is determined based on the spatial distance distribution of mapped defect locations, obtained by statistically analyzing the spatial distances between all pairs of mapped defect locations. The spatial distance sequence is sorted in ascending order, and the density change of the sequence is statistically analyzed. The location where the density change shows a significant decrease corresponds to the boundary point between the distance within the same defect and the distance between different defects. The aggregation threshold is set as the spatial distance value at this boundary point, or as a multiple of the defect feature size. The defect feature size is estimated by the average size of the local neighborhood corresponding to the filtered defect location, and the multiple factor is set in the range of 2.0 to 5.0, preferably 3.0. Mapped defect locations with spatial distances less than an aggregation threshold are grouped into defect clusters belonging to the same defect. The clustering process employs connectivity analysis to establish relationships between mapped defect locations with spatial distances less than the aggregation threshold. Mapped defect locations with direct or indirect relationships are grouped into the same defect cluster. Each defect cluster corresponds to an independent defect region in the original image, and the cluster contains the detection results of that defect at different resolution levels.
[0049] Boundary information of the mapped defect locations at each resolution level is extracted for each defect cluster. This boundary information describes the spatial extent and shape characteristics of the defect at that resolution level, represented by a set of boundary points or a contour curve. Boundary point extraction is achieved by edge detection of the local neighborhood corresponding to the filtered defect locations. Edge detection uses a gradient operator to calculate the gray-level gradient magnitude of pixels within the local neighborhood. Pixels with gradient magnitudes greater than an edge threshold are identified as boundary points. The edge threshold is adaptively determined based on the gray-level gradient distribution of the local neighborhood and is set as the sum of the mean and standard deviation of the gradient magnitudes. The boundary point sets are connected using a link tracing algorithm to form closed contour curves. The contour curves store the boundary point coordinates in clockwise or counterclockwise order. The boundary information is transformed into the original image spatial coordinate system, using the same transformation method as the mapped defect location coordinates. The boundary point coordinates are multiplied by the scale ratio of the resolution level relative to the original image to obtain the aligned boundary information. The aligned boundary information is uniformly expressed in the original image spatial coordinate system, eliminating the influence of spatial scale differences between different resolution levels.
[0050] The fusion weights are determined based on the resolution level corresponding to the aligned boundary information. Boundary information at different resolution levels has varying reliability in describing defect contours. High-resolution levels retain more boundary details and have higher accuracy in locating defect edges, but are susceptible to noise interference, leading to boundary point jitter. Low-resolution levels suppress noise and have better overall boundary stability, but lose detail information, resulting in smooth boundary transitions. The fusion weights reflect the contribution ratio of boundary information at each resolution level to the fusion process. High-resolution levels are assigned larger weights to retain boundary details, medium-resolution levels are assigned moderate weights to balance detail and stability, and low-resolution levels are assigned smaller weights to provide overall constraint. The fusion weights are determined based on the resolution level number; higher level numbers correspond to higher resolutions. The fusion weights are calculated using a decreasing function, which can be set to exponential or linear decreasing. The attenuation coefficient for exponential decreasing is set to a range of 0.6 to 0.9, preferably 0.75, while the slope for linear decreasing is set to a range of 0.1 to 0.3, preferably 0.2. The fusion weights are normalized across resolution levels so that the sum of the fusion weights across all levels within the same defect cluster is 1.
[0051] The aligned boundary information of each defect cluster is weighted and fused to obtain the defect contour. This weighted fusion integrates the aligned boundary information of each resolution level within the same defect cluster into a unified defect boundary representation. The fusion process considers the spatial distribution and weight allocation of boundary points. Spatial clustering is performed on the boundary points of each level within the same defect cluster, grouping boundary points with similar spatial positions into the same boundary segment. The clustering distance threshold is set between 3 and 10 pixels, preferably 5 pixels. For each boundary segment, the weighted average position of the boundary points at each level within that segment is calculated. The weights are the fusion weights corresponding to that level. The x-coordinate of the weighted average position is the sum of the products of the x-coordinates of the boundary points at each level and the fusion weights, and the y-coordinate is the sum of the products of the y-coordinates of the boundary points at each level and the fusion weights. The weighted average position serves as the representative point of that boundary segment in the fused contour. The representative points of each boundary segment are connected according to the principle of boundary continuity to form a complete defect contour. The fused contour is smoothed to eliminate local discontinuities generated during boundary point clustering and weighted averaging. The smoothing process uses polygon approximation or spline interpolation methods. The tolerance parameter of the polygon approximation is set from 0.5 pixels to 2.0 pixels, preferably 1.0 pixels.
[0052] Through multi-resolution hierarchical spatial mapping, the locations of defects scattered across different scales are unified into the original image coordinate system, establishing a spatial correlation basis for cross-level defect information. Defect clustering based on spatial distance associates detection results from different levels belonging to the same defect, avoiding misclassification of multiple detections of the same defect as multiple independent defects. Boundary information alignment and fusion integrate multi-scale boundary features; high-resolution levels provide accurate boundary details, while low-resolution levels provide stable overall shape. Weighted fusion balances positioning accuracy and noise resistance, and the extracted defect contours accurately describe the spatial extent and shape characteristics of the defect.
[0053] This invention achieves accurate extraction of defect contours by combining multi-scale spatial mapping and defect cluster division with weighted fusion of boundary information that adapts to resolution levels, providing reliable spatial feature input for subsequent defect classification and quality assessment.
[0054] In sub-step 1042, determining the aggregation threshold based on the spatial distance distribution of the mapped defect locations further includes: The spatial distance distribution of the mapped defect locations is segmented and analyzed. The rate of change between adjacent spatial distances in the spatial distance distribution of the mapped defect locations is calculated, and the location corresponding to the maximum rate of change is identified as the segmentation point. Extract the spatial distance value corresponding to the segment point as the aggregation threshold.
[0055] The spatial distance distribution of mapped defect locations is analyzed in segments. The mapped defect locations include the positions of all defects in the original image at each resolution level after coordinate mapping, within a unified spatial coordinate system. The Euclidean distance between any two mapped defect locations is calculated, and all distance values constitute the spatial distance distribution. When the same defect is repeatedly detected at different resolution levels, the corresponding mapped defect locations cluster spatially with small spatial distances between them. Conversely, the mapped defect locations of different defects are spatially separated with larger spatial distances between them. The spatial distance distribution exhibits bimodal or multimodal characteristics. Small distance peaks correspond to inter-level distances within the same defect, while large distance peaks correspond to spatial intervals between different defects. There is a significant boundary between these two types of distances. The spatial distance distribution is sorted in ascending order to obtain a distance sequence from smallest to largest. The length of this distance sequence is on the order of the square of the total number of mapped defect locations, containing the distance values between all location pairs. A numerical jump occurs in the transition region between the distance within the same defect and the distance between different defects; this jump position is a reasonable setting point for the aggregation threshold.
[0056] The rate of change between adjacent spatial distances in the spatial distance distribution of the mapped defect locations is calculated. Adjacent spatial distances refer to two distance values that are adjacent in the distance sequence. The rate of change is obtained by dividing the difference between the previous and subsequent distance values by the previous distance value, reflecting the local growth rate of the distance sequence. In densely distributed sections of distances within the same defect, the differences between adjacent distance values are small, and the rate of change remains stable and low. In sections where the distances transition from within the same defect to between different defects, adjacent distance values show a significant jump, and the rate of change increases sharply, forming a peak. The calculation of the rate of change traverses all adjacent position pairs in the distance sequence. For the i-th distance value and the (i+1)-th distance value in the distance sequence, the rate of change is the ratio of their difference to the i-th distance value, where i ranges from 1 to the length of the distance sequence minus 1. Since the distance sequence is already arranged in ascending order, the difference between adjacent distances is always non-negative, and the rate of change is also non-negative. The rate of change sequence changes synchronously with the distance sequence, and its length is the length of the distance sequence minus 1.
[0057] The location corresponding to the maximum rate of change in the rate of change is identified as the segmentation point. The maximum rate of change corresponds to the location with the largest jump in adjacent distances in the distance sequence, marking the boundary between the distance distribution within the same defect and the distance distribution between different defects. The rate of change sequence is traversed to find the largest rate of change and its index in the sequence; this index corresponds to adjacent distance pairs in the distance sequence. The segmentation point divides the distance sequence into two segments: the first segment contains smaller distance values, mainly the distances between the mapped locations of the same defect at different detection levels; the second segment contains larger distance values, mainly the distances between the mapped locations of different defects. The segmentation point is selected based on the peak value of the rate of change, avoiding the insufficient adaptability caused by manually setting fixed thresholds or percentiles, and can automatically adjust according to the defect distribution characteristics in the specific image. When there are multiple local peaks in the rate of change sequence, the location corresponding to the global maximum peak is selected as the main segmentation point; other local peaks can be used as auxiliary judgment criteria but do not participate in the determination of the aggregated threshold.
[0058] The spatial distance value corresponding to the segment point is extracted as the aggregation threshold. After the position index of the segment point in the distance sequence is determined, the distance value corresponding to that position or the distance value of the next position is used as the initial value of the aggregation threshold. Considering the boundary between the distance within the same defect and the distance between different defects, to ensure complete aggregation of all levels of detection results for the same defect, the aggregation threshold is taken as the average of the distance value of the segment point and the distance value of the next position, or directly as the distance value of the segment point. After the aggregation threshold is determined, the mapped defect positions are clustered. Mapped defect positions with a spatial distance less than the aggregation threshold are grouped into the same defect cluster, while mapped defect positions with a spatial distance greater than or equal to the aggregation threshold are determined to belong to different defects. The adaptive determination of the aggregation threshold avoids the applicability differences of a fixed threshold in different images or different defect density scenarios, improving the accuracy of defect cluster division.
[0059] In practical implementation, when the image contains sparsely distributed large-sized defects, the differences between the distances within the same defect and between different defects are significant, the peak of the rate of change is obvious, and the accuracy of segment point identification is high. When the image contains densely distributed small-sized defects, the mapped defect positions of different defects may be locally close, leading to a weakening of the bimodal characteristics of the spatial distance distribution. In this case, the rate of change sequence can still identify segment points through abrupt changes in relative growth rates, but the aggregation threshold needs to be corrected in conjunction with defect size information. The correction method is to compare the aggregation threshold with the defect feature size. The defect feature size is estimated by the average diameter or area equivalent circle diameter of the detection area corresponding to the defect position after screening. If the aggregation threshold is less than the defect feature size, the aggregation threshold is adjusted to a multiple of the defect feature size. The multiple coefficient is set in the range of 1.5 to 3.0, preferably 2.0, to avoid the same defect being misclassified into multiple defect clusters due to its large size. If the aggregation threshold is greater than 5 times the defect feature size, the aggregation threshold is kept unchanged to ensure the ability to distinguish between different defects.
[0060] In extreme cases, when all mapped defect locations correspond to a single defect or a small number of defects, the spatial distance distribution may exhibit a unimodal or weakly bimodal characteristic, and the peak value of the rate of change sequence is not obvious. In such cases, a rate of change threshold is set to assist in the judgment. The rate of change threshold is set as the sum of the mean and standard deviation of the rate of change sequence. Locations with a rate of change greater than this threshold are considered candidate segmentation points, and the location with the largest rate of change among the candidate segmentation points is selected as the final segmentation point. If none of the rate of change exceeds the rate of change threshold, the aggregation threshold is directly set using the defect feature size, which is 2 to 3 times the defect feature size.
[0061] By segmenting and analyzing the spatial distance distribution of mapped defect locations, the boundary points between the internal distances of the same defect and the distances between different defects are identified using the rate of change of adjacent spatial distances. The extracted aggregation threshold adaptively matches the actual distribution features of defects in the image. Segment point identification based on the peak value of the rate of change avoids the empirical dependence of fixed threshold settings, improves the applicability and accuracy of defect cluster division in different scenarios, and provides reliable defect association relationships for subsequent multi-level boundary information fusion.
[0062] This invention identifies abrupt changes in the rate of change of spatial distance to adaptively determine the aggregation threshold, accurately aggregating multi-resolution hierarchical detection results belonging to the same defect, avoiding duplicate counting or segmentation errors of defects, and ensuring the integrity and accuracy of defect contour extraction.
[0063] In sub-step 1044, determining the fusion weights based on the resolution level corresponding to the aligned boundary information also includes: The frequency of occurrence of each resolution level in the resolution level corresponding to the aligned boundary information is statistically analyzed. Calculate the image sharpness of the image corresponding to each resolution level as the level quality coefficient; The fusion weight is obtained by combining the frequency of occurrence with the hierarchical quality coefficient.
[0064] The aligned boundary information originates from detection results at different resolution levels within the same defect cluster, with each set of boundary points labeled with its source resolution level. All boundary information within the same defect cluster is traversed, and the frequency of occurrence for each resolution level is recorded. A higher frequency for a particular resolution level indicates a more stable response to the current defect region during defect detection, better consistency in boundary localization, and the frequency reflects the sensitivity of the resolution level to the detection of a specific defect. The statistical range of the frequency is limited to the current defect cluster, and the frequency of occurrence for different defect clusters is calculated independently.
[0065] Image sharpness is calculated for each resolution level as a level quality coefficient. Image sharpness is used to evaluate the detail retention and boundary localization accuracy of images at different resolution levels. It is calculated using a gradient magnitude statistical method. Gradient operations are performed on the defect region image corresponding to each resolution level to obtain the horizontal and vertical gradient components. The gradient magnitude of each pixel is calculated, and the average gradient magnitude of all pixels within the defect region is used as the image sharpness for that resolution level. The defect region image is selected as the calculation area, extending outward from the detected defect center by 1.5 to 2.0 times the defect feature size. After the image sharpness calculation for each resolution level is completed, the sharpness values are normalized by dividing the sharpness values of all levels by the maximum sharpness value to obtain a level quality coefficient ranging from 0 to 1. This provides an evaluation basis for the quality dimension in subsequent fusion weight calculations.
[0066] The frequency of occurrence is normalized by dividing the frequency of occurrence at each level by the sum of the frequencies of occurrence at all levels. The fusion weight is calculated by weighting the normalized frequency value with the level quality coefficient. The combination formula is as follows: , where w k f is the fusion weight for the k-th resolution level. k Let be the frequency of occurrence of the k-th resolution level. q is the sum of the frequencies of occurrence of all levels. kLet γ be the quality coefficient for the k-th resolution level, and γ be the balance coefficient. The balance coefficient controls the relative importance of occurrence frequency and image quality in the fusion weights, and its value ranges from 0.3 to 0.7, preferably 0.5. After the fusion weights are calculated, the fusion weights for each resolution level are normalized so that the sum of all level fusion weights equals 1, resulting in the final weight coefficients used for boundary information fusion. The normalized fusion weights are directly applied to the weighted fusion process of boundary points at each resolution level. The coordinates of boundary points detected at different levels within the same defect cluster are weighted and averaged according to the fusion weight of their respective levels to obtain the fused defect boundary coordinates.
[0067] When the image corresponding to a certain resolution level has noise or is unclear, the level quality coefficient of that level is low, and the fusion weight after the combined operation is automatically reduced. When a certain resolution level only appears in a few defect clusters, with a low frequency of occurrence but high image clarity, the compensation effect of the level quality coefficient on the fusion weight ensures that high-precision boundary information is not ignored. If the level quality coefficient of a certain resolution level is lower than a preset quality threshold, which is set to 0.3 to 0.5, the fusion weight of that level is forcibly set to 0, excluding its boundary information from participating in the fusion.
[0068] This invention uses a combination of occurrence frequency and hierarchical quality coefficients to determine a fusion weight that adaptively matches the reliability and accuracy differences of boundary information at each level, thereby achieving accurate reconstruction of defect contours and improving the robustness and accuracy of boundary positioning.
[0069] Step 105: Mark the image to be inspected according to the defect contour and output the marked image.
[0070] In this embodiment, the defect contour information determined in the aforementioned steps is obtained. This defect contour information includes a sequence of defect boundary point coordinates and a defect category identifier. The defect boundary point coordinate sequence is mapped to the pixel coordinate system of the image to be inspected. A closed contour curve is drawn according to the connection order of the boundary points. The contour curve line width is set to 2 to 4 pixels, and the contour curve color is distinguished according to the defect category identifier; different categories of defects are marked with different colors for identification. A text label is superimposed at the upper left corner of the outer rectangle of the defect contour. The text label contains the defect category name and defect number, and the text font size is adaptively adjusted according to the resolution of the image to be inspected. After annotation, the image with the superimposed contour curve and text label is output. The output format supports common image formats. The annotated image is used for the visualization of quality inspection results and for manual review and confirmation.
[0071] like Figure 2 As shown, Figure 2 This is a schematic diagram of the structure of a vision-based image defect localization system provided in an embodiment of the present invention. The system includes: The image decomposition module 201 is used to acquire the image to be detected, perform multi-scale decomposition on the image to be detected, and obtain image components at multiple resolution levels. The candidate defect identification module 202 is used to calculate the consistency of pixel gradient direction and the distribution of pixel gradient magnitude in the image components of each resolution level, and to identify texture change areas and grayscale abnormal areas based on the consistency of pixel gradient direction and the distribution of pixel gradient magnitude, so as to obtain the location of candidate defects at each resolution level. The defect screening module 203 is used to extract the local neighborhood and the surrounding reference area for the candidate defect locations at each resolution level, calculate the degree of deviation between the pixel statistical features of the local neighborhood and the pixel statistical features of the surrounding reference area, and obtain the screened defect locations based on the degree of deviation. The contour fusion module 204 is used to spatially map the filtered defect locations across multiple resolution levels, and to align and fuse the boundary information of candidate defect locations at different resolution levels belonging to the same defect to obtain the defect contour. The annotation output module 205 is used to annotate the image to be inspected according to the defect contour and output the annotated image.
[0072] One technical solution provided in this embodiment of the invention is an electronic device, including: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps in any of the aforementioned methods.
[0073] One technical solution provided in this embodiment of the invention is a computer-readable storage medium storing a computer program, wherein the processor executes the computer program to implement the steps in any of the aforementioned methods.
[0074] The specific embodiments described above are preferred embodiments of the present invention and are not intended to limit the specific scope of the present invention. The scope of the present invention includes, but is not limited to, these specific embodiments. All equivalent changes made in accordance with the shape and structure of the present invention are within the protection scope of the present invention.
Claims
1. A method for locating image defects based on visual inspection, characterized in that, Includes the following steps: The image to be detected is acquired, and the image to be detected is decomposed into multiple scales to obtain image components at multiple resolution levels. The pixel gradient direction consistency and pixel gradient magnitude distribution within the image components of each resolution level are calculated respectively. Based on the pixel gradient direction consistency and pixel gradient magnitude distribution, texture change regions and gray-level abnormal regions are identified to obtain the candidate defect locations for each resolution level. For candidate defect locations at each resolution level, local neighborhoods and peripheral reference regions are extracted. The degree of deviation between the pixel statistical features of the local neighborhoods and the pixel statistical features of the peripheral reference regions is calculated. Based on the degree of deviation, the selected defect locations are obtained. The filtered defect locations are spatially mapped across multiple resolution levels. The boundary information of candidate defect locations at different resolution levels belonging to the same defect is aligned and fused to obtain the defect contour. The defect contour is marked on the image to be inspected, and the marked image is output.
2. The method according to claim 1, characterized in that, The pixel gradient direction consistency and pixel gradient magnitude distribution within the image components of each resolution level are calculated respectively. Based on the pixel gradient direction consistency and pixel gradient magnitude distribution, texture abrupt change regions and grayscale anomaly regions are identified, resulting in candidate defect locations for each resolution level, including: The concentration of gradient directions in the neighborhood of each pixel in the image component of each resolution level is calculated to obtain the pixel gradient direction consistency of each resolution level. The gradient magnitude of all pixels in each resolution level is statistically analyzed to obtain the pixel gradient magnitude distribution of each resolution level. The thresholds for determining texture mutations and grayscale anomalies are determined based on the pixel gradient magnitude distribution. Pixels whose gradient direction consistency is lower than the texture mutation determination threshold are marked as texture mutation candidate points, and pixels whose gradient magnitude exceeds the gray-scale anomaly determination threshold in the pixel gradient magnitude distribution are marked as gray-scale anomaly candidate points. Spatial location comparison is performed on candidate points for texture mutation and candidate points for grayscale anomalies, and pixels with overlapping spatial locations are selected as core candidate points; Connectivity analysis is performed on the core candidate points. Texture mutation candidate points and gray-level anomaly candidate points that are spatially connected to the core candidate points are merged to form connected regions. The regions where texture mutation candidate points are located in the connected regions are identified as texture mutation regions, and the regions where gray-level anomaly candidate points are located are identified as gray-level anomaly regions. Texture mutation regions and gray-level anomaly regions are used as candidate defect locations for each resolution level.
3. The method according to claim 2, characterized in that, Determining the texture mutation and grayscale anomaly detection thresholds based on pixel gradient magnitude distribution includes: The pixel gradient magnitude distribution at each resolution level is sorted by quantile, and the pixel gradient magnitude at the preset quantile position is used to obtain the initial texture mutation judgment threshold. Calculate the mean of the initial texture mutation detection threshold for each resolution level, and normalize the initial texture mutation detection threshold with the mean to obtain the texture mutation detection threshold. The numerical difference is calculated based on the pixel gradient magnitude at different quantile positions after quantile sorting. The ratio of the numerical difference to the texture mutation judgment threshold is then used to obtain the grayscale anomaly judgment threshold.
4. The method according to claim 1, characterized in that, For candidate defect locations at each resolution level, local neighborhoods and surrounding reference regions are extracted. The deviation between the pixel statistical features of the local neighborhoods and the pixel statistical features of the surrounding reference regions is calculated. Based on the deviation, the selected defect locations are obtained, including: For candidate defect locations at each resolution level, a local neighborhood is determined with the candidate defect location as the center, and an outer reference region is determined based on the boundary of the local neighborhood. The pixel statistical features of the local neighborhood are obtained by calculating the mean gray value and the variance of the gray value of the pixels in the local neighborhood, and the pixel statistical features of the outer reference area are obtained by calculating the mean gray value and the variance of the gray value of the pixels in the outer reference area. The deviation magnitude of the pixel statistical features of the local neighborhood is quantified based on the pixel statistical features of the outer reference region to obtain the deviation metric value of each resolution level. The deviation metric values at each resolution level are weighted and fused across different levels to obtain the degree of deviation. The screening threshold is determined based on the distribution characteristics of the degree of deviation, and the candidate defect positions with a deviation greater than the screening threshold are retained as the screening defect positions.
5. The method according to claim 1, characterized in that, The filtered defect locations are spatially mapped across multiple resolution levels. Boundary information of candidate defect locations at different resolution levels belonging to the same defect is aligned and fused to obtain the defect contour, which includes: Based on the scale ratio between each resolution level and the original image, the spatial coordinates of the filtered defect locations at each resolution level are transformed into the spatial coordinate system of the original image to obtain the mapped defect locations. Calculate the spatial distance between the mapped defect locations, determine the aggregation threshold based on the spatial distance distribution of the mapped defect locations, and classify the mapped defect locations with spatial distances less than the aggregation threshold into defect clusters belonging to the same defect. For each defect cluster, the boundary information of the corresponding mapped defect position in each resolution level is extracted, and the boundary information is transformed into the original image space coordinate system to obtain the aligned boundary information. The fusion weights are determined based on the resolution level corresponding to the aligned boundary information, and the aligned boundary information of each defect cluster is weighted and fused to obtain the defect contour.
6. The method according to claim 5, characterized in that, Determining the aggregation threshold based on the spatial distance distribution of the mapped defect locations includes: The spatial distance distribution of the mapped defect locations is segmented and analyzed. The rate of change between adjacent spatial distances in the spatial distance distribution of the mapped defect locations is calculated, and the location corresponding to the maximum rate of change is identified as the segmentation point. Extract the spatial distance value corresponding to the segment point as the aggregation threshold.
7. The method according to claim 5, characterized in that, The fusion weights are determined based on the resolution level corresponding to the aligned boundary information, including: The frequency of occurrence of each resolution level in the resolution level corresponding to the aligned boundary information is statistically analyzed. Calculate the image sharpness of the image corresponding to each resolution level as the level quality coefficient; The fusion weight is obtained by combining the frequency of occurrence with the hierarchical quality coefficient.
8. A visual inspection-based image defect localization system, used to implement the method described in any one of claims 1-7, characterized in that, The system includes: The image decomposition module is used to acquire the image to be detected, decompose the image to be detected into multiple scales, and obtain image components at multiple resolution levels. The candidate defect identification module is used to calculate the consistency of pixel gradient direction and the distribution of pixel gradient magnitude in the image components of each resolution level, and to identify texture change regions and grayscale abnormal regions based on the consistency of pixel gradient direction and the distribution of pixel gradient magnitude, so as to obtain the location of candidate defects at each resolution level. The defect screening module is used to extract the local neighborhood and the surrounding reference area for candidate defect locations at each resolution level, calculate the degree of deviation between the pixel statistical features of the local neighborhood and the pixel statistical features of the surrounding reference area, and obtain the screened defect locations based on the degree of deviation. The contour fusion module is used to spatially map the filtered defect locations across multiple resolution levels, align and fuse the boundary information of candidate defect locations at different resolution levels belonging to the same defect, and obtain the defect contour. The annotation output module is used to annotate the image to be inspected based on the defect contour and output the annotated image.
9. An electronic device, characterized in that, include: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the steps of the method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program instructions that, when executed by a processor, implement the steps of the method as described in any one of claims 1 to 7.
