Magnet surface defect detection method based on computer vision
By using multi-scale, multi-directional texture analysis and phase consistency measurement, the problems of low efficiency and misjudgment in magnet surface defect detection are solved, achieving highly robust automatic detection and outputting detailed defect information.
Patent Information
- Application Number
- CN202511127570.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-13
- Publication Date
- 2025-11-21
AI Technical Summary
Existing technologies for detecting defects on magnet surfaces suffer from problems such as low detection efficiency, high subjectivity, susceptibility to fatigue and misjudgment, and inability to adapt to complex or minute defects. In particular, it is difficult to achieve highly robust automatic detection under various complex surface conditions, lighting changes, and different defect types.
By employing multi-scale and multi-directional texture analysis technology, complex-form filtering operators are constructed to extract texture phase information at different scales and directions on the surface. Adaptive segmentation and connected component analysis are then performed, and combined with phase consistency measurement, to achieve accurate location and report output of defects.
It improves the accuracy and efficiency of detection, reduces the false detection rate, enhances adaptability to different lighting conditions and complex surfaces, can identify multiple types of defects, and outputs structured inspection reports.
Smart Images

Figure CN120997185A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer vision and image processing technology, specifically to a method for detecting defects on the surface of magnets based on computer vision. Background Technology
[0002] In modern industrial production and manufacturing processes, the surface quality of magnets and related precision components such as metals and ceramics directly affects product performance, reliability, and market competitiveness. Magnets, in particular, are widely used in motors, sensors, and medical equipment. Even minute defects such as cracks, scratches, dents, foreign objects, and pores on their surfaces can easily lead to product failure, functional degradation, and even safety hazards. Therefore, improving the accuracy and efficiency of magnet surface defect detection is a significant technical challenge in high-end manufacturing.
[0003] Traditional defect detection mainly relies on manual visual inspection or simple single-feature threshold segmentation methods. These methods suffer from low detection efficiency, high subjectivity, susceptibility to fatigue and misjudgment, and inability to adapt to complex or subtle defects. With the increasing demands for automation and intelligent manufacturing, achieving efficient, accurate, and fully automated surface defect detection based on computer vision and digital image processing technologies has become an important development direction in the industrial inspection field. Although existing automatic detection methods have achieved a certain degree of automation, they still face several challenges under various complex surface conditions, lighting variations, and different defect types and sizes, such as noise interference, easy confusion between normal textures and defect features, strong parameter dependence, and large detection errors. Especially for industrial parts such as magnets with reflective surfaces, complex patterns, and significant differences in defect size, how to achieve automated detection that is independent of manual labor, requires minimal manual parameter setting, and is highly robust to various types of defects remains a core bottleneck in the industry's technology.
[0004] Therefore, this case aims to propose a computer vision-based method for detecting defects on magnet surfaces. By utilizing multi-scale and multi-directional texture analysis technology, a complex-form filtering operator is constructed to extract texture phase information at different scales and directions on the surface. Using phase consistency as a metric, the method adaptively segments and analyzes connected components of abnormal regions in the image, ultimately achieving accurate location and report output of defects. Summary of the Invention
[0005] This invention provides a computer vision-based method for detecting defects on the surface of magnets, which helps to solve the problems mentioned in the background art.
[0006] This invention provides the following technical solution: a method for detecting defects on the surface of magnets based on computer vision, comprising:
[0007] Acquire a grayscale image of the magnet surface, and perform noise suppression and grayscale linear normalization on the image;
[0008] Set multiple scales and corresponding filter wavelengths and Gaussian function standard deviations, determine several directional angles, and generate corresponding complex Gabor filter kernels in each direction downwards at each scale;
[0009] Apply the Gabor filter kernel to the preprocessed image to obtain the filter response at each scale and in each direction;
[0010] Phase angle information is extracted from the filter response at each scale and in each direction, and the average phase angle at the pixel is calculated by summing the cosine and sine components.
[0011] The phase consistency value of a pixel is calculated based on the deviation between the phase components in each direction and the average phase angle.
[0012] An adaptive segmentation threshold is determined based on the mean and standard deviation of the phase consistency values of the entire image. The phase consistency image is then binarized to obtain a defect candidate mask.
[0013] Perform connected component analysis on the binarization results, calculate the area of each connected component, remove connected components with an area smaller than the preset minimum area threshold, and retain the effective defect areas.
[0014] Calculate the bounding box and centroid coordinates for each valid connected region, and output the bounding box, centroid, and area information of each defect to form an inspection report.
[0015] Optionally, the step of acquiring a grayscale image of the magnet surface and performing noise suppression and grayscale linear normalization processing on the image specifically includes:
[0016] Take the image pixel domain Ω = {0, ..., W-1} × {0, ..., H-1}; where W is the image width and H is the image height; the x and y coordinates of the pixel satisfy x∈[0,W-1] and y∈[0,H-1];
[0017] Obtain the gray value I0(x,y) of pixel (x,y) in the original grayscale image: Ω→[0,255];
[0018] Let the neighborhood radius of the median filter be r. m =1;
[0019] Construct the filtered neighborhood N of pixel (x,y) m (x,y), including the center and 8 neighboring pixels, specifically: N m (x,y)={(i,j)∈Ω||ix|≤r m ,|jy|≤r m};
[0020] Median filtering is applied to I0(x,y) to obtain the denoised grayscale value of pixel (x,y): I1(x,y)=median{I0(i,j)|(i,j)∈N m (x,y)}; where median{·} is the median function, which returns the median of the elements in the set;
[0021] Obtain the global minimum gray value of the denoised image
[0022] Obtain the global maximum grayscale value of the denoised image
[0023] If I 1,max =I 1,min If no defect is detected, the output will be "No defect detected" and the process will end.
[0024] If I 1,max ≠I 1,min Then the pixel values are normalized to [0,1], specifically:
[0025] Among them, I n (x,y) represents the normalized grayscale value of pixel (x,y).
[0026] Optionally, the step of setting multiple sets of scales and corresponding filter wavelengths and Gaussian function standard deviations, determining several directional angles, and generating corresponding complex Gabor filter kernels in each direction at each scale specifically includes:
[0027] Let the number of scales be N. s =3, number of directions is N o =4;
[0028] Set the Gabor filter wavelength λ to scale s. s =2 s The standard deviation σ of the Gaussian function corresponding to the scale s =λ s Where s = {1, 2, ..., N} s} is the scale index;
[0029] Get the direction angle in the o-th direction Where o = {1, 2, ..., N} o} represents the direction index;
[0030] Set the maximum radius of the Gabor kernel at any scale as...
[0031] For kernel coordinates (u,v)∈[-K max ,K max ] 2 Perform kernel coordinate rotation, specifically:
[0032] Where (u,v) represents the kernel coordinate offset; (u' s,o ,v' s,o ) represents the direction θ o And the coordinates after rotation by scale s;
[0033] Constructing complex Gabor kernels:
[0034] Among them, G s,o (u,v) represents the complex Gabor kernel function value at scale s and direction o; o is the imaginary unit.
[0035] Optionally, applying the Gabor filter kernel to the preprocessed image to obtain the filter response at each scale and in each direction specifically includes:
[0036] Obtain the Gabor response value R at scale s and direction o. s,o (x,y), specifically:
[0037]
[0038] like Let I n (x+u,y+v)=0.
[0039] Optionally, the step of extracting phase angle information from the filtered responses at each scale and in each direction, and calculating the average phase angle at the pixel by summing the cosine and sine components, specifically includes:
[0040] Extract the response phase φ of pixel (x,y) at scale s and orientation o. s,o (x,y):
[0041] in, It is an imaginary part function; is the real part of the function; arctan2 is the signed arctangent function in two variables;
[0042] Obtain the cosine phase component and C at (x,y) across all scale directions. ∑ (x,y) and sinusoidal components and S ∑ (x,y), specifically:
[0043]
[0044] Calculate the average phase angle at pixel (x,y)
[0045]
[0046] Optionally, the step of calculating the phase consistency value of a pixel based on the deviation between the phase components in each direction and the average phase angle specifically includes:
[0047] Let the total number of responses N = N s ×N o Then the phase coherence value of pixel (x,y) is:
[0048]
[0049] Optionally, the step of determining an adaptive segmentation threshold based on the mean and standard deviation of the phase consistency values across the entire image, and binarizing the phase consistency image to obtain a defect candidate mask, specifically includes:
[0050] Calculate the mean phase consistency of the entire image. Where, |Ω|=W×H;
[0051] Calculate the phase consistency variance of the entire image The standard deviation is
[0052] Set the adaptive segmentation threshold to T = μ PC -σ PC ;
[0053] Binarization: Where B(x,y) is the binary defect candidate mask for pixel (x,y), B(x,y)=1 is a defect candidate, and B(x,y)=0 is a non-defect;
[0054] If ∑ (x,y)∈Ω If B(x,y)=0, the output is "No defect detected" and the process ends; otherwise, continue.
[0055] Optionally, the step of performing connected component analysis on the binarization result, calculating the area of each connected component, removing connected components with an area smaller than a preset minimum threshold, and retaining valid defect regions specifically includes:
[0056] Construct a candidate pixel set P = {(x,y)∈Ω|B(x,y)=1};
[0057] Based on the 8-neighborhood equivalence relation, P is divided into M connected components. Where M is the total number of connected components; each Let m be the m-th connected component; m = {1,...,M} is the index of the connected component.
[0058] Calculate the number of pixels A in the m-th connected component. m =|C m |;
[0059] Let the minimum effective area threshold be A. min , retain {C m |Am ≥A min}, relabeled Where K is the number of remaining connected components after filtering; C k This represents the k-th valid connected component; k is the reserved index.
[0060] Calculate the number of pixels A in the k-th effective connected component. k =|C k |;
[0061] If K=0, the output "No defect detected" is displayed, and the process ends; otherwise, it continues.
[0062] Optionally, the step of calculating the bounding box and centroid coordinates for each valid connected component and outputting the bounding box, centroid, and area information of each defect to form an inspection report specifically includes:
[0063] For each preserved connected component C k Perform steps S801 to S802 respectively:
[0064] S801, Calculate the bounding box:
[0065] in, and These are the minimum and maximum x-coordinates of the k-th defective connected component, respectively.
[0066] in, and These are the minimum and maximum ordinates of the k-th defective connected component, respectively;
[0067] S802, Calculate the centroid:
[0068] in, and These are the centroids of the horizontal and vertical coordinates of the k-th defective connected domain, respectively.
[0069] Output the bounding box for each defect k. Center of mass and area A k .
[0070] The present invention has the following beneficial effects:
[0071] 1. Building upon conventional blurring and noise reduction, a strategy for dynamically judging the overall image contrast is introduced: when the image brightness change is minimal and the surface is likely uniform and defect-free, subsequent calculations are immediately terminated; otherwise, linear normalization is performed on the image. This effectively avoids wasting computational resources on invalid images, reducing system power consumption. Furthermore, by dynamically judging the global minimum and maximum gray levels, the adaptability to images acquired under different lighting conditions is enhanced, avoiding detection errors caused by differences in ambient lighting. In existing technologies, most methods only perform blurring or median filtering with fixed parameters, lacking a mechanism for recognizing and excluding uniform, defect-free images, often leading to meaningless subsequent calculations or even false alarms. This scheme improves algorithm efficiency and reduces the probability of false detections by pre-judging the dynamic range. The entire preprocessing process suppresses salt-and-pepper noise while preserving defect edge details as much as possible, laying a solid foundation for subsequent multi-scale phase analysis.
[0072] 2. An adaptive filter kernel construction strategy targeting the scale range and directional distribution of defects is proposed. Based on preset minimum and maximum defect sizes, the number of filter scale groups and directions is reasonably controlled to balance detection accuracy and computational complexity while ensuring coverage of micro to medium-scale defects. Unlike traditional fixed-scale unidirectional analysis, this scheme incorporates prior knowledge of defect characteristics in scale setting and directional angle allocation, enabling uniform response to scratches, cracks, and unevenness in various directions. In existing technologies, common practices either select only a single scale or a few directions, resulting in insufficient response to tilted or slender defects. This scheme, through multi-scale and multi-directional matrix construction, ensures comprehensive coverage and allows for flexible adjustment of scale size according to production needs. While ensuring accurate detection, it can be appropriately tailored according to equipment computing power. Industrial magnet surface defects have diverse morphologies, and a single scale or direction is insufficient to detect all defects. Traditional methods are prone to omissions or weak responses to oblique defects. This method improves the recognition rate of defects of different shapes by rationally allocating scale and direction, especially the detection capability of defects with random orientation and asymmetric shape. In practical applications, it reduces the false negative rate and adapts to workpieces of different specifications through an adjustable parameter mechanism.
[0073] 3. The concept of complex filtering response is introduced into the classic convolution framework, which not only acquires texture intensity information but also preserves phase information, thus providing rich data for subsequent phase consistency analysis. Furthermore, low-intensity responses are suppressed, distinguishing between noise and weak texture responses and reducing interference in subsequent stages. Traditional methods often use real-valued filtering, ignoring phase information; even when phase is used, it is often extracted at a single scale or direction. This scheme, through unified calculation of complex responses across multiple scales and directions, can obtain the texture polarity changes of the defect region at different resolutions and, by masking low-amplitude responses, suppress spurious responses caused by surface fine lines or environmental noise, improving the robustness of phase calculation.
[0074] 4. Instead of directly judging the phase in a single direction, this method first extracts the angles of the phase in each direction, and then integrates the phase components from all directions to obtain a more representative overall texture phase. This multi-directional integration method can eliminate phase deviations caused by noise, illumination, or surface material in a single direction, improving the stability of the overall phase estimation. Unlike existing technologies that directly take the phase in a certain direction or simply average it, this scheme uses a vector-based phase accumulation and normalization method, assigning equal weights to the phase in each direction. The resulting comprehensive phase can more comprehensively reflect the dominant directional characteristics of the local texture, thus providing a more reliable benchmark for subsequent consistency measurement. Traditional phase extraction is common in unidirectional or bidirectional areas, and is easily affected by local noise when used to calculate statistics. This scheme, through multi-directional phase integration, takes into account the needs of anisotropic texture analysis, and enhances the sensitivity to surface defects by balancing the components, while suppressing erroneous offsets caused by unidirectional noise, making the detection results more stable and accurate.
[0075] 5. Phase consistency is used as the core metric for pixel-level defect identification, measuring the degree of local texture anomaly based on the difference between the overall phase and the phase in each direction. When the phase deviation at a certain point increases significantly at different scales and in different directions, it can be identified as a potential defect. Compared with traditional measures based on gradient or gray-level difference, phase consistency has the advantage of being insensitive to illumination and overall gray-level, allowing detection to focus more on changes in texture structure. In existing technologies, most methods rely on intensity gradients or texture statistical features, which are limited by changes in illumination and differences in material reflection. The phase consistency metric in this scheme effectively eliminates these interferences, maintaining stable defect identification capabilities under various ambient lighting conditions and on different magnetic material surfaces.
[0076] 6. Based on the global phase consistency distribution, the segmentation threshold is dynamically calculated, and candidate defect regions are delineated according to the overall consistency level and fluctuation range of the image. This adaptive mechanism can automatically adjust the threshold according to the differences in overall surface quality between different workpiece batches, avoiding the risk of maladaptation caused by manually fixing the threshold. Compared with existing fixed or semi-fixed threshold methods, the adaptive segmentation strategy of this scheme can more strictly identify subtle anomalies in a high consistency background, and can also appropriately relax the criteria when the overall consistency is low, thus achieving a dual consideration for small defects and minor texture perturbations.
[0077] 7. This method introduces connected component analysis based on eight neighborhoods and combines it with a minimum effective area threshold to eliminate sporadic noise spots, retaining only defect areas with actual impact. This area screening mechanism can both eliminate isolated small noises and ensure the detection capability of truly small defects. Unlike simple connected component or morphological operations in existing technologies, this method adds a screening strategy based on the minimum defect size prior after connected component analysis, improving the false alarm filtering efficiency. In traditional methods, morphological methods such as dilation or erosion are often used, which can easily change the defect shape and are sensitive to parameters. The prior minimum area screening in this scheme does not change the true shape of the connected component, more accurately reflects the defect size, and adapts to different detection accuracy requirements through an adjustable threshold.
[0078] 8. After detecting a valid connected component, this solution not only directly calculates the coordinates of the region boundary and center, but also outputs structured information such as defect area and shape, forming a visualized inspection report. This report can be directly used in production monitoring systems, supporting subsequent defect classification, statistical analysis, and traceability management. Unlike existing solutions that only output defect coordinates or simple markings, this solution's report contains richer geometric information, providing quality engineers with more intuitive and actionable data support. Attached Figure Description
[0079] Figure 1 This is a schematic diagram of the process of the present invention. Detailed Implementation
[0080] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0081] Example, refer to Figure 1 A computer vision-based method for detecting defects on magnet surfaces, comprising:
[0082] Acquire a grayscale image of the magnet surface, and perform noise suppression and grayscale linear normalization on the image;
[0083] Set multiple scales and corresponding filter wavelengths and Gaussian function standard deviations, determine several directional angles, and generate corresponding complex Gabor filter kernels in each direction downwards at each scale;
[0084] Apply the Gabor filter kernel to the preprocessed image to obtain the filter response at each scale and in each direction;
[0085] Phase angle information is extracted from the filter response at each scale and in each direction, and the average phase angle at the pixel is calculated by summing the cosine and sine components.
[0086] The phase consistency value of a pixel is calculated based on the deviation between the phase components in each direction and the average phase angle.
[0087] An adaptive segmentation threshold is determined based on the mean and standard deviation of the phase consistency values of the entire image. The phase consistency image is then binarized to obtain a defect candidate mask.
[0088] Perform connected component analysis on the binarization results, calculate the area of each connected component, remove connected components with an area smaller than the preset minimum area threshold, and retain the effective defect areas.
[0089] Calculate the bounding box and centroid coordinates for each valid connected region, and output the bounding box, centroid, and area information of each defect to form an inspection report.
[0090] This paper proposes an end-to-end magnet surface defect detection workflow. By sequentially executing key steps such as image acquisition and preprocessing, construction and response of multi-scale, multi-directional gamma-wave filtering operators, phase information extraction and consistency measurement, adaptive threshold segmentation, connected component filtering, and location report output, it solves the problems of fragmented detection workflows, parameter mismatches, and easy missed and false detections in existing technologies. First, noise reduction and grayscale linear mapping are used to eliminate ambient light and noise interference, providing a stable foundation for subsequent texture analysis. Then, multiple sets of filtering operators are constructed for defects of different sizes and orientations to achieve a comprehensive response to various texture changes such as scratches, pits, and protrusions. Subsequently, while preserving phase information, the difference between the phase in each direction and the overall phase is measured, transforming subtle texture abrupt changes into detection criteria. Finally, based on the overall... Figure 1 Consistency statistics adaptively set segmentation thresholds to accurately extract true anomaly regions. Finally, connected component analysis removes isolated noise, retains true defects, and outputs a structured report containing boundary range, center location, and area information to meet subsequent quality traceability and statistical analysis needs. By organically linking each step, the drawbacks of traditional methods where single feature extraction is susceptible to variations in lighting and material properties are avoided. This significantly improves detection accuracy and robustness while also meeting online real-time requirements, providing a reliable solution for automated quality inspection in industrial production lines.
[0091] The process of acquiring a grayscale image of the magnet surface and performing noise suppression and grayscale linear normalization on the image specifically includes:
[0092] The image pixel domain Ω = {0, ..., W-1} × {0, ..., H-1} is defined, where W is the image width (number of pixels) and H is the image height (number of pixels); the x and y coordinates of the pixels satisfy x∈[0, W-1] and y∈[0, H-1]; the image space range in which this method is applied is clearly defined, providing a basic index and processing boundary for all subsequent pixel-related processing steps, and preventing out-of-bounds access;
[0093] Obtain the grayscale value I0(x,y):Ω→[0,255] of the pixel (x,y) in the original grayscale image; obtain the original grayscale image of the magnet surface to provide the most basic input data for the entire defect detection process;
[0094] Let the neighborhood radius of the median filter be r. m =1; Set a 3×3 pixel window so that the filter can suppress noise while preserving as much detail as possible; r m The median filter defines the size of the local window used to calculate the median value of pixel (x, y) during denoising. A larger window provides better noise smoothing but also blurs fine texture edges; a smaller window preserves details better but reduces the removal of large-grain noise. If r... m =1 (3×3 window) is often used to remove single-pixel salt-and-pepper noise without significantly blurring edges; if r m =2 (5×5 window) can remove noise over a larger area, but may smooth out small defects; if r m Values ≥3 (7×7 and above) tend to over-smooth fine textures and minor defects. The value is determined based on the noise particle size and the minimum defect size in the original image: when the noise particle size is approximately equal to the minimum defect size, a value slightly smaller than the defect size can be selected. m If the image noise is uniformly distributed and the particles are small, take r. m =1; if the noise particles are significantly larger than the defect size, then increase the value appropriately. Recommended value: generally take r. m =1, i.e., a 3×3 window; if the average noise particles on the magnet surface exceed 2 pixels and the defect size is not less than 5 pixels, then r can be selected. m =2.
[0095] Construct the filtered neighborhood N of pixel (x,y) m (x,y), including the center and 8 neighboring pixels, specifically: N m (x,y)={(i,j)∈Ω||ix|≤r m ,|jy|≤r m}; provides a local reference region for pixel (x,y), and only this neighborhood is considered when calculating the median, which helps to remove isolated noise.
[0096] Median filtering is applied to I0(x,y) to obtain the denoised grayscale value of pixel (x,y): I1(x,y)=median{I0(i,j)|(i,j)∈N m (x, y)}; where median{·} is the median function, which returns the median of the elements in the set; replacing the original value with the median of the neighboring pixels effectively suppresses local abrupt noise without blurring the edges;
[0097] Obtain the global minimum gray value of the denoised image
[0098] Obtain the global maximum grayscale value of the denoised image
[0099] Obtain the dynamic range of the denoised image to provide a boundary for normalization and ensure that subsequent processing is not affected by changes in brightness and contrast.
[0100] If I 1,max =I 1,min If no defect is detected, the output will be "No defect detected" and the process will end. This checks whether the image is completely uniform, without texture or defects, avoiding meaningless calculations and saving resources.
[0101] If I 1,max ≠I 1,min Then the pixel values are normalized to [0,1], specifically:
[0102] Among them, I n (x,y) represents the normalized grayscale value of pixel (x,y); all pixel values are linearly mapped to the [0,1] interval to unify the subsequent analysis standard and eliminate the influence of brightness differences between different images.
[0103] The image acquisition and preprocessing stages have been refined by defining the image spatial range, dynamically selecting the median filtering window, and determining the dynamic range to improve efficiency and accuracy. First, the width and height ranges of the image are clearly defined to ensure safe access to each pixel and prevent out-of-bounds errors. Then, the denoising window is dynamically determined based on the size of surface noise particles and the minimum defect size, effectively suppressing salt-and-pepper noise while preserving defect edge details to the greatest extent. Finally, the overall gray-level extreme difference of the denoised image is calculated to determine whether the surface is uniform and textureless, thus terminating subsequent calculations early when no defects are found, saving resources. Spatial range constraints prevent anomalies caused by out-of-bounds access, improving system stability; dynamic filtering windows suppress various noise types and maintain the integrity of defect edges, improving the effectiveness of subsequent texture analysis; and gray-level difference judgments preemptively exclude defect-free images, reducing false detection rates and shortening processing time. This meets the dual requirements of real-time performance and accuracy for industrial online inspection, outperforming existing technologies that only use fixed-parameter preprocessing.
[0104] The process of setting multiple scales and corresponding filter wavelengths and Gaussian function standard deviations, determining several directional angles, and generating corresponding complex Gabor filter kernels in each direction at each scale specifically includes:
[0105] Let the number of scales be N. s =3, number of directions is N o =4; Preset different scales and directions to ensure coverage of multi-resolution and multi-directional texture information of defects, and enhance the adaptability of the algorithm;
[0106] N s The number of spatial frequency resolution layers used in the Gabor filter determines the granularity of the multi-scale analysis. More scales capture different frequency characteristics from tiny to large defects. Smaller N... s (As in 2), the scale coverage is narrow, and it is not sensitive to certain intermediate-scale defects; larger N s When the value is 5 or higher, it can cover a wider frequency band, but the computational load increases exponentially, and response maps are difficult to aggregate. The value is determined by the defect size range and the minimum / maximum defect width (in pixels) to be detected. If the minimum defect width is approximately 2 pixels and the maximum is approximately 16 pixels, then the scale can be selected as {2, 4, 8} (i.e., N). s =3), or increase {2,4,8,16} (i.e., N) s =4). Recommended value: N is usually... s =3, i.e., λ s ={2,4,8} can balance computational efficiency and detection accuracy; for a larger range, it can be increased to N. s =4.
[0107] N oDefine the number of discretizations for the Gabor kernel rotation angle to detect texture changes in various directions. The more directions, the finer the response to defects at arbitrary angles. Choose a smaller N. o (As in 2), only horizontal and vertical textures are detected, resulting in poor response to tilted defects; a larger N is taken. o (As shown in step 8) it can detect multi-directional patterns with a step of 22.5°, but the computational load increases and the phase aggregation calculation becomes more complex. The value is determined based on the defect morphology and orientation distribution requirements and real-time performance. If the defects are mostly in random orientations, then at least N... o =4(0°, 45°, 90°, 135°); for higher precision, N can be used. o =8. Suggested value: Generally, N o =4 is sufficient for detecting the direction of most common surface defects on industrial parts; for more stringent requirements such as tilting or spot-like defects, N can be selected. o =6 or 8.
[0108] Set the Gabor filter wavelength λ to scale s. s =2 s The standard deviation σ of the Gaussian function corresponding to the scale s =λ s Where s = {1, 2, ..., N} s} serves as the scale index; it determines the spatial periodicity and Gaussian envelope expansion degree of each scale, and adaptively detects defects of different sizes;
[0109] Get the direction angle in the o-th direction Where o = {1, 2, ..., N} o} is the direction index; the various direction angles are distributed at equal intervals to achieve anisotropic texture analysis and avoid missing surface defects in any direction.
[0110] Set the maximum radius of the Gabor kernel at any scale as... Ensure complete spatial coverage of Gabor kernels at all scales, avoid information loss, and improve the accuracy of boundary pixel processing;
[0111] For kernel coordinates (u,v)∈[-K max ,K max ] 2 Perform kernel coordinate rotation, specifically:
[0112] Where (u,v) represents the kernel coordinate offset; (u' s,o ,v' s,o ) represents the direction θ o The coordinates after rotation at scale s; the convolution kernel space is aligned to each direction to facilitate the extraction of texture structures in each direction;
[0113] Constructing complex Gabor kernels:
[0114] Among them, G s,o (u, v) represents the complex Gabor kernel function value at scale s and direction o; i is the imaginary unit; it is used for subsequent operations with the normalized image, and also contains texture frequency and principal direction information. The complex form facilitates phase analysis.
[0115] An improvement was made to the generation of multi-scale, multi-directional Gaussian filter operators by combining prior defect size with computational resource constraints to achieve a balance between the number of scales and directions. First, the scale hierarchy of the filter operator is determined based on the minimum and maximum defect size range of the magnet under test, ensuring coverage of texture features ranging from tiny scratches to large pits. Then, the directional distribution density is determined based on the defect directional distribution requirements, effectively capturing texture variations in any tilt direction. Finally, complex-form filter operators are designed and constructed at each scale and direction using rotation, period, and Gaussian envelope parameters, providing a complete tool for subsequent phase extraction. These steps address the problems of insufficient response and high false negative rate in multi-morphological defects caused by single-scale, single-directional analysis in existing technologies. Furthermore, the adjustable hierarchical structure balances detection accuracy and computational efficiency, allowing for adaptation to different production line computing power, thus improving the algorithm's engineering usability and deployment flexibility.
[0116] The step of applying the Gabor filter kernel to the preprocessed image to obtain the filter response at each scale and in each direction specifically includes:
[0117] Obtain the Gabor response value R at scale s and direction o. s,o (x, y), specifically:
[0118] The Gabor kernels at different scales and orientations are slid-correlated with the normalized image to output a complex response; this complex response reflects the texture matching intensity and dominant phase of each point on the magnet surface at multiple resolutions and orientations.
[0119] like Let I n (x+u,y+v)=0; This ensures that edge pixels can also participate in filtering, avoiding size reduction.
[0120] The filtering response acquisition stage is optimized by using complex convolution calculations to preserve both amplitude and phase information, while suppressing weak responses and reducing noise interference. First, the constructed multi-scale, multi-directional gamma wave operator is convolved with the preprocessed image to obtain the complex response of each pixel at different scales and directions. Then, responses with amplitudes below a set threshold are set to zero, filtering out spurious responses caused by background texture or sensor noise. Simultaneously acquiring intensity and phase information through complex responses lays the foundation for accurate extraction of local texture phase. By masking low-amplitude responses, the negative impact of noise or slight background texture on phase estimation is avoided, reducing false alarms. Overall, this approach enhances key texture signals and suppresses invalid information, exhibiting higher robustness and reliability compared to existing methods that rely solely on real responses.
[0121] The step of extracting phase angle information from the filtered response at each scale and in each direction, and calculating the average phase angle at the pixel by summing the cosine and sine components, specifically includes:
[0122] Extract the response phase φ of pixel (x, y) at scale s and orientation o. s,o (x, y):
[0123] in, It is an imaginary part function; is the real part function; arctan2 is the signed binary arctangent function; obtain the dominant phase angle of the response to facilitate subsequent unified analysis of the consistency of texture direction;
[0124] Obtain the cosine phase component and C at (x,y) across all scale directions. ∑ (x,y) and sinusoidal components and S ∑ (x,y), specifically:
[0125]
[0126] The phase of each response direction is accumulated in a vector manner to facilitate the overall measurement of the main texture direction; the average phase angle at pixel (x,y) is calculated.
[0127] Returns the dominant texture phase angle across all scale directions, used as a benchmark for consistent alignment.
[0128] In terms of phase information extraction and integration, a comprehensive phase representative value located in the main direction of the local texture is obtained by converting the phase values of the responses in each direction into vector components and summing and normalizing them. Specifically, the phase angle is first extracted from the complex response of each scale and direction and mapped to the corresponding vector component; then, the vector components of all directions are summed and normalized to obtain the overall texture phase at each pixel. This overcomes the problem that the phase of a single direction or single scale is easily affected by local noise. This design solves the detection bias caused by unstable phase estimation and large influence of fluctuations in a single direction in the prior art. By integrating multiple directions, the robustness and reliability of phase information are enhanced, providing a more stable benchmark for subsequent consistency measurement and significantly improving the accuracy and repeatability of detection.
[0129] The calculation of the pixel's phase consistency value based on the deviation between the phase components in each direction and the average phase angle specifically includes:
[0130] Let the total number of responses N = N s ×N o Then the phase coherence value of pixel (x,y) is:
[0131]
[0132] Using the average principal phase as a reference, the deviation of the response in each direction from the principal phase is calculated and normalized; a high value indicates that the local texture is highly consistent, while a low value reflects abrupt changes or anomalies in the texture, which often correspond to defects.
[0133] A pixel-level consistency measurement strategy based on phase deviation is proposed. This strategy normalizes the difference between the phase in each direction and the overall phase, assigning a score to each pixel to reflect texture continuity. Specifically, the phase in each direction is compared with the overall representative phase value of that pixel. A consistency index is obtained by statistically analyzing the deviation magnitude. High consistency corresponds to defect-free areas, while low consistency corresponds to possible defect edges or abnormal textures. This measurement method is inherently insensitive to illumination changes and grayscale drift, focusing on the changes in texture structure itself. It can accurately identify phase abrupt changes caused by scratches and unevenness, reducing false alarms caused by intensity gradients. Compared to traditional methods based on grayscale difference or texture statistics, it provides a more stable and focused criterion for texture anomalies, further improving the robustness of defect detection.
[0134] The step of determining an adaptive segmentation threshold based on the mean and standard deviation of the phase consistency values of the entire image, binarizing the phase consistency image, and obtaining a defect candidate mask specifically includes:
[0135] Calculate the mean phase consistency of the entire image. Where, |Ω|=W×H;
[0136] Calculate the phase consistency variance of the entire image The standard deviation is
[0137] Statistics Figure 1 Consistent mean and fluctuation, adapting to the overall quality of different magnet surfaces, achieving global self-adaptation;
[0138] Set the adaptive segmentation threshold to T = μ PC -σ PC Setting dynamic thresholds facilitates the differentiation between uniform areas and suspected defect areas.
[0139] Binarization: Wherein, B(x,y) is the binary defect candidate mask for pixel (x,y), B(x,y)=1 is a defect candidate, and B(x,y)=0 is a non-defect; pixel areas with consistency significantly lower than the overall level are marked as defect candidates, and a binary mask is generated;
[0140] If ∑ (x,y)∈Ω If B(x,y)=0, the output indicates that no defect was detected, the process ends, resources are saved, and it indicates that the surface is uniform and there are no defects; otherwise, continue.
[0141] A global adaptive threshold strategy is introduced in the segmentation stage, dynamically setting the binarization threshold based on the average level and fluctuation range of the pixel consistency index across the entire image. This method first statistically analyzes the overall distribution characteristics of all pixel consistency indices, then automatically generates a segmentation threshold based on the overall trend, marking pixels with significantly lower consistency than the overall level as defect candidates. This avoids missed or false detections caused by fixed thresholds when there are large differences in surface quality between different batches of workpieces; it can automatically adjust the sensitivity according to the surface texture uniformity in the production environment, maintaining consistent detection performance; compared to manual experience thresholds or semi-automatic threshold adjustment methods, this strategy reduces the workload of parameter debugging and improves the algorithm's adaptability and reliability in the field.
[0142] The process of performing connected component analysis on the binarization results, calculating the area of each connected component, removing connected components with an area smaller than a preset minimum threshold, and retaining valid defect regions specifically includes:
[0143] Construct a candidate pixel set P = {(x,y)∈Ω|B(x,y)=1}; focus on the suspected defect areas to be analyzed to reduce the amount of subsequent processing;
[0144] Based on the 8-neighborhood equivalence relation, P is divided into M connected components. Where M is the total number of connected components; each Let m be the m-th connected component; m = {1, ..., M} is the index of the connected component; group the spatially continuous defect pixels into groups, each group representing an independent suspected defect;
[0145] Calculate the number of pixels A in the m-th connected component.m =|C m |;Measure the actual size of each connected component to prepare for noise removal and defect severity assessment;
[0146] Let the minimum effective area threshold be A. min , retain {C m |A m ≥A min}, relabeled Where K is the number of remaining connected components after filtering; C k The k-th valid connected component; k is the retention index; isolated noise smaller than 3×3 pixels is removed, and only defects that have a real impact on quality are considered;
[0147] A min Used to remove excessively small connected components (noise) and retain larger regions that are likely to be real defects. If A min If the size is too small (e.g., 1 or 2 pixels), it is easy to retain noise specks, leading to an increase in false alarms; if A min An excessively large value (e.g., 100 pixels) may miss tiny, real defects. The value is determined by converting the minimum permissible defect size into pixel area. If the minimum allowable defect width for production inspection is 3 pixels, then the lower limit of the area is 3 × 3 = 9; if the minimum defect is 5 pixels, then A can be set... min =5 × 5 = 25. Suggested value: Usually choose A. min =9 or A min =16, which can both remove isolated noise and ensure the detection of the smallest defects.
[0148] Calculate the number of pixels A in the k-th effective connected component. k =|C k |;
[0149] If K=0, the output "No defect detected" indicates the process ends, indicating no actual defect and no false alarms are generated; otherwise, continue.
[0150] An area-prior screening mechanism was designed for the connected component analysis and noise removal stages. Candidate regions are divided using eight-neighbor connectivity analysis, and discrete noise spots are removed based on a preset minimum region size, while retaining true defect regions. Specifically, the binarized results are grouped into several independent regions based on spatial continuity. Then, regions that are too small are removed by comparing their area with the prior minimum defect size. Through spatial connectivity constraints, potential defects and noise are accurately grouped; sporadic noise points are removed using area thresholds, reducing false alarms; and the sensitivity to small true defects is not affected, improving overall detection accuracy. Compared with existing techniques that rely solely on morphological operations, this method maintains the true shape of defects while also considering noise filtering, providing more reliable defect candidates.
[0151] The process involves calculating the bounding box and centroid coordinates for each valid connected component, and outputting the bounding box, centroid, and area information for each defect to form an inspection report. Specifically, this includes:
[0152] For each preserved connected component C k Perform steps S801 to S802 respectively:
[0153] S801, Calculate the bounding box:
[0154] in, and These are the minimum and maximum x-coordinates of the k-th defective connected component, respectively.
[0155] in, and These are the minimum and maximum ordinates of the k-th defective connected component, respectively;
[0156] Precisely locate the enclosing area for each defect region to facilitate subsequent defect analysis and annotation;
[0157] S802, Calculate the centroid:
[0158] in, and These are the centroids of the horizontal and vertical coordinates of the k-th defective connected domain, respectively.
[0159] Providing the center point of the defect area helps in statistical defect distribution, feature extraction, and subsequent tracking.
[0160] Output the bounding box for each defect k. Center of mass and area A k The results of defects are reported in a structured and readable manner, providing direct evidence for quality inspection, grading, or repair, thus achieving a closed-loop testing process.
[0161] The output provides a structured and visualized inspection report, including defect boundary range, center location, and area information. It is designed as a plug-in module for easy integration with host computer systems or production monitoring platforms. By automatically calculating the minimum and maximum x and y coordinates of each effective connected region to construct bounding boxes, determine the center coordinates of the regions, and calculate the area, a complete defect list is generated. This provides quality engineers with intuitive and quantifiable information on defect location and size; supports subsequent defect classification, statistics, traceability, and report generation; the pluggable design of the report module simplifies system integration and facilitates rapid deployment in different production environments; compared to traditional systems that only output defect pixels or bounding box markers, the rich geometric information and structured output generated by this solution enhance the practical value of the inspection results.
[0162] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.
[0163] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A method for detecting surface defects in magnets based on computer vision, characterized in that, include: Acquire a grayscale image of the magnet surface, and perform noise suppression and grayscale linear normalization on the image; Set multiple scales and corresponding filter wavelengths and Gaussian function standard deviations, determine several directional angles, and generate corresponding complex Gabor filter kernels in each direction at each scale. The Gabor filter kernel is applied to the preprocessed image to obtain the filter response at each scale and in each direction; Phase angle information is extracted from the filter response at each scale and in each direction, and the average phase angle at the pixel is calculated by summing the cosine and sine components. The phase consistency value of a pixel is calculated based on the deviation between the phase components in each direction and the average phase angle. An adaptive segmentation threshold is determined based on the mean and standard deviation of the phase consistency values of the entire image. The phase consistency image is then binarized to obtain a defect candidate mask. Perform connected component analysis on the binarization results, calculate the area of each connected component, remove connected components with an area smaller than the preset minimum area threshold, and retain the effective defect areas. Calculate the bounding box and centroid coordinates for each valid connected region, and output the bounding box, centroid, and area information of each defect to form an inspection report.
2. The method for detecting surface defects of magnets based on computer vision according to claim 1, characterized in that, The process of acquiring a grayscale image of the magnet surface and performing noise suppression and grayscale linear normalization on the image specifically includes: Take the image pixel domain Ω = {0, ..., W-1} × {0, ..., H-1}; where W is the image width and H is the image height; the x and y coordinates of the pixel satisfy x∈[0,W-1] and y∈[0,H-1]; Obtain the gray value I0(x,y) of pixel (x,y) in the original grayscale image: Ω→[0, 255]; Let the neighborhood radius of the median filter be r. m =1; Construct the filtered neighborhood N of pixel (x,y) m (x,y), including the center and 8 neighboring pixels, specifically: N m (x,y)={(i,j)∈Ω||ix|≤r m ,|jy|≤rm}; Median filtering is applied to I0(x,y) to obtain the denoised grayscale value of pixel (x,y): I1(x,y)=median{I0(i,j)|(i,j)∈N m (x,y)}; where median{·} is the median function, which returns the median of the elements in the set; Obtain the global minimum gray value of the denoised image Obtain the global maximum gray value of the denoised image If I 1,max =I 1,min If no defect is detected, the output will be "No defect detected" and the process will end. If I 1,max ≠I 1,min Then the pixel values are normalized to [0,1], specifically: Among them, I n (x,y) represents the normalized grayscale value of pixel (x,y).
3. The method for detecting surface defects of magnets based on computer vision according to claim 2, characterized in that, The process of setting multiple scales and corresponding filter wavelengths and Gaussian function standard deviations, determining several directional angles, and generating corresponding complex Gabor filter kernels in each direction at each scale specifically includes: Let the number of scales be N. s =3, number of directions is N o =4; Set the Gabor filter wavelength λ to scale s. s =2 s The standard deviation σ of the Gaussian function corresponding to the scale s =λ s Where s = {1, 2, ..., N} s } is the scale index; Get the direction angle in the o-th direction Where o = {1, 2, ..., N} o } represents the direction index; Set the maximum radius of the Gabor kernel at any scale as... For kernel coordinates (u,v)∈[-K max ,K max ]2 2 Perform kernel coordinate rotation, specifically: Where (u,v) represents the kernel coordinate offset; (u' s,o ,v' s,o ) represents the direction θ o And the coordinates after rotation by scale s; Constructing complex Gabor kernels: Among them, G s,o (u,v) represents the complex Gabor kernel function value at scale s and direction o; It is the imaginary unit.
4. The method for detecting surface defects of magnets based on computer vision according to claim 3, characterized in that, The step of applying the Gabor filter kernel to the preprocessed image to obtain the filter response at each scale and in each direction specifically includes: Obtain the Gabor response value R at scale s and direction o. s,o (x,y), specifically: like Let I n (x+u,y+v)=0.
5. The method for detecting surface defects of magnets based on computer vision according to claim 4, characterized in that, The step of extracting phase angle information from the filtered response at each scale and in each direction, and calculating the average phase angle at the pixel by summing the cosine and sine components, specifically includes: Extract the response phase φ of pixel (x,y) at scale s and orientation o. s,o (x,y): in, It is an imaginary part function; is the real part of the function; arctan2 is the signed arctangent function in two variables; Obtain the cosine phase component and C at (x,y) across all scale directions. ∑ (x,y) and sinusoidal components and S ∑ (x,y), specifically: Calculate the average phase angle at pixel (x,y) 6. The method for detecting surface defects of magnets based on computer vision according to claim 5, characterized in that, The calculation of the pixel's phase consistency value based on the deviation between the phase components in each direction and the average phase angle specifically includes: Let the total number of responses N = N s ×N o Then the phase coherence value of pixel (x,y) is:
7. The method for detecting surface defects of magnets based on computer vision according to claim 6, characterized in that, The step of determining an adaptive segmentation threshold based on the mean and standard deviation of the phase consistency values of the entire image, binarizing the phase consistency image, and obtaining a defect candidate mask specifically includes: Calculate the mean phase consistency of the entire image. Where, |Ω|=W×H; Calculate the phase consistency variance of the entire image The standard deviation is Set the adaptive segmentation threshold to T = μ PC -σ PC ; Binarization: Where B(x,y) is the binary defect candidate mask for pixel (x,y), B(x,y)=1 is a defect candidate, and B(x,y)=0 is a non-defect; If ∑ (x,y)∈Ω If B(x,y) = 0, the output "No defect detected" is displayed, and the process ends; otherwise, it continues.
8. The method for detecting surface defects of magnets based on computer vision according to claim 7, characterized in that, The process of performing connected component analysis on the binarization results, calculating the area of each connected component, removing connected components with an area smaller than a preset minimum threshold, and retaining valid defect regions specifically includes: Construct a candidate pixel set P = {(x,y)∈Ω|B(x,y)=1}; Based on the 8-neighborhood equivalence relation, P is divided into M connected components. Where M is the total number of connected components; each Let m be the m-th connected component; m = {1, ..., M} is the index of the connected component. Calculate the number of pixels A in the m-th connected component. m =|C m |; Let the minimum effective area threshold be A. min , retain {C m |A m ≥A min }, relabeled Where K is the number of remaining connected components after filtering; C k This represents the k-th valid connected component; k is the reserved index. Calculate the number of pixels A in the k-th effective connected component. k =|C k |; If K=0, the output "No defect detected" is displayed, and the process ends; otherwise, it continues.
9. The method for detecting surface defects of magnets based on computer vision according to claim 8, characterized in that, The process involves calculating the bounding box and centroid coordinates for each valid connected component, and outputting the bounding box, centroid, and area information for each defect to form an inspection report. Specifically, this includes: For each preserved connected component C k Perform steps S801 to S802 respectively: S801, Calculate the bounding box: in, and These are the minimum and maximum x-coordinates of the k-th defective connected component, respectively. in, and These are the minimum and maximum ordinates of the k-th defective connected component, respectively; S802, Calculate the centroid: in, and These are the centroids of the horizontal and vertical coordinates of the k-th defective connected domain, respectively. Output the bounding box for each defect k. Center of mass and area A k .
Citation Information
Patent Citations
Method for detecting object contour based on phase characteristic
CN102222325A