A kind of ball cage surface defect detection method based on image anti-reflection enhancement
By calculating the local gray-level variance and area weighting of the reflective area mask, and dynamically matching the Gaussian scale for multi-scale residual map weighted fusion, the problems of reflective interference and loss of defect details in the surface inspection of ball cages are solved, and high-precision defect detection is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- WUHAN GONGKANG AUTO PARTS CO LTD
- Filing Date
- 2026-05-21
- Publication Date
- 2026-06-19
AI Technical Summary
Existing technologies struggle to effectively separate the reflective areas of metal mirrors from the edges of real micro-defects in the detection of surface defects in ball cages, resulting in a proliferation of false edges and a decreased detection rate of real defects.
By calculating the local grayscale variance map and the area of the reflective connected domain of the reflective area mask, the optimal Gaussian scale is dynamically matched, and multi-scale residual map weighted fusion is performed to remove false edges and retain real defect details.
It effectively suppresses strong specular reflections and retains subtle defect details with high fidelity, significantly improving the defect detection rate.
Smart Images

Figure CN122244040A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of computer vision and industrial defect detection technology. More specifically, this invention relates to a method for detecting surface defects in ball cages based on image de-reflection enhancement. Background Technology
[0002] In the automotive parts manufacturing industry, the CV joint, as a core component of the constant velocity universal joint, directly affects the reliability and safety of the vehicle's transmission system. At the end of the CV joint precision grinding production line, a machine vision-based online surface defect detection system is typically used to automatically screen for minute imperfections such as cracks and scratches on the CV joint surface. Because the CV joint surface exhibits extremely strong metallic mirror reflective properties after precision grinding and polishing, it produces large areas of bright reflective spots under industrial light sources. Therefore, accurately removing false edges under high-intensity environmental reflective interference and extracting minute, real defect edge features with high fidelity has become an urgent need for achieving high-precision online quality control.
[0003] To meet the aforementioned surface defect detection requirements, the industry typically employs image gray-level difference combined with traditional edge extraction operators for defect identification. These edge extraction operators include the Sobel and Canny operators. Furthermore, to suppress reflective interference in the acquired images, the traditional multi-scale Retinex enhancement algorithm is often introduced for preprocessing the original image. This technique primarily estimates the illumination components by setting a set of globally fixed Gaussian kernel scales. Then, it globally weights and fuses the logarithmic domain residual maps generated at each Gaussian scale according to pre-defined fixed proportional weights. Finally, it extracts edges from the fused image based on a fixed gradient magnitude threshold to obtain the final defect detection result.
[0004] However, the aforementioned existing technologies have significant drawbacks in practical engineering applications. On the one hand, since the local gray-scale gradient amplitude of the reflective area of the metal mirror and the gradient amplitude of the real small defect edge are often in the same high value range, traditional extraction operators based on fixed thresholds cannot effectively separate the two, resulting in a large number of false edges caused by reflection in the detection results. On the other hand, the fixed Gaussian scale and equal weight fusion mechanism in the traditional multi-scale Retinex enhancement algorithm completely ignore the physical fact that the size of the reflective spot changes dynamically with the rotation of the workpiece. This not only leads to reflection residue in large-area reflective areas due to illumination estimation mismatch, but also smooths out the high-frequency edges of small scratches in the normal background area due to the lack of a targeted detail preservation mechanism. Ultimately, this results in a serious decrease in the effective detection rate of real defects, which cannot meet the stringent quality inspection standards of the production line. Summary of the Invention
[0005] To address the technical problems of pseudo-edge interference, reflection residue, and loss of defect details caused by strong specular reflection on metal surfaces and fixed-scale illumination fusion algorithms in existing technologies, this invention proposes a surface defect detection method for spherical cages based on image de-reflection enhancement, which can effectively suppress strong specular reflection residue and retain real defect details with high fidelity.
[0006] This invention provides a method for detecting surface defects in a ball cage based on image de-reflection enhancement, comprising: performing threshold comparison on a grayscale image of the ball cage surface to obtain a reflective area mask; calculating the degree of grayscale variation in the neighborhood of each pixel in the grayscale image of the ball cage surface to obtain a local grayscale variance map; extracting each reflective connected component in the reflective area mask, performing weighted calculation based on the area of each reflective connected component to obtain the reflective spatial scale, and performing nearest neighbor matching with multiple preset Gaussian scales to obtain the optimal Gaussian scale index; performing illumination decomposition on the grayscale image of the ball cage surface using the multiple preset Gaussian scales to obtain residual maps of each preset Gaussian scale; and calculating at least two scale-differential values... The difference in the residual map corresponding to the Gaussian kernel; based on the comparison between the inter-scale difference and the preset tolerance threshold, the normally exposed core pixels in the reflective area mask are removed to obtain the corrected reflective area mask; the reflective area and the normal background area are divided according to the corrected reflective area mask; in the reflective area, the weight of each preset Gaussian scale is determined according to the optimal Gaussian scale index; in the normal background area, the weight of the minimum preset Gaussian scale is dynamically adjusted according to the local gray-scale variance map; the residual maps of each preset Gaussian scale are weighted and fused according to all the determined weights to obtain the enhanced image; edge extraction and connected component feature filtering are performed on the enhanced image, and the defect location annotation results are output.
[0007] By employing the above technical solution, feature regions are identified through variance calculation, the optimal scale is matched based on the area of connected components, misjudged pixels are eliminated using scale differences, and finally, residual weights are dynamically allocated to each region. This approach avoids scale mismatch caused by global processing, accurately removes bright backgrounds while enhancing local abrupt changes, and effectively improves the accuracy of defect annotation.
[0008] Preferably, the step of calculating the reflective spatial scale by weighting the area of each reflective connected domain includes: calculating the corresponding equivalent circle radius based on the area of each reflective connected domain; summing the product of the area of each reflective connected domain and the corresponding equivalent circle radius to obtain a weighted sum of areas; and dividing the weighted sum of areas by the sum of the areas of all reflective connected domains to obtain the reflective spatial scale.
[0009] By employing the above technical solution, and through a weighted summation of the areas of each reflective connected region and the equivalent circle radius, the larger main reflective spot dominates the scale calculation. This method effectively suppresses the interference of extremely small stray bright spots on spatial scale estimation, ensuring that the selected Gaussian scale can fully cover the main reflective area.
[0010] Preferably, the step of performing nearest neighbor matching with multiple preset Gaussian scales to obtain the optimal Gaussian scale index includes: obtaining a preset coverage coefficient; multiplying the coverage coefficient by the reflective space scale to obtain a corrected scale; calculating the absolute value of the difference between the multiple preset Gaussian scales and the corrected scale respectively; and taking the index of the preset Gaussian scale with the smallest absolute value of the difference as the optimal Gaussian scale index.
[0011] Preferably, the step of calculating the degree of grayscale variation in the neighborhood of each pixel in the grayscale image of the ball cage surface to obtain a local grayscale variance map includes: sliding a sliding window of a preset size on the grayscale image of the ball cage surface pixel by pixel, calculating the grayscale mean of all pixels within the sliding window; calculating the sum of squared differences between the grayscale value of each pixel within the sliding window and the grayscale mean, and dividing the sum of squared differences by the total number of pixels within the sliding window to obtain the local grayscale variance of the current center pixel; and traversing all pixels to obtain the local grayscale variance map.
[0012] Preferably, the step of performing illumination decomposition on the grayscale image of the ball cage surface using the multiple preset Gaussian scales to obtain residual maps at each preset Gaussian scale includes: convolving the grayscale image of the ball cage surface with the Gaussian kernel corresponding to the preset Gaussian scale to obtain an illumination estimation map at the corresponding scale; calculating the logarithm of each pixel in the grayscale image of the ball cage surface and the logarithm of the corresponding pixel in the illumination estimation map; and subtracting the logarithm of the illumination estimation map from the logarithm of the grayscale image of the ball cage surface to obtain the residual map at each preset Gaussian scale.
[0013] Preferably, the step of removing normally exposed core pixels from the reflective area mask based on the comparison result of the inter-scale difference and the preset tolerance threshold to obtain the corrected reflective area mask includes: traversing each pixel in the reflective area mask that has a value of one, and obtaining the inter-scale difference of each pixel; determining whether the inter-scale difference of the pixel is greater than the preset tolerance threshold; in response to the inter-scale difference of the pixel being greater than the preset tolerance threshold, setting the value of the corresponding pixel in the corrected reflective area mask to zero; in response to the inter-scale difference of the pixel being less than or equal to the preset tolerance threshold, setting the value of the corresponding pixel in the corrected reflective area mask to one.
[0014] By employing the aforementioned technical solution, and judging the positive or negative characteristics of the difference between scales, the normal exposure core pixels exhibiting positive values are accurately selected and reset to zero. This method successfully peels away the edges of real defects with extremely high brightness from the initial mask, preventing high-frequency details from being incorrectly erased during the illumination suppression process, and ensuring the integrity of defect features from the source.
[0015] Preferably, determining the weight of each preset Gaussian scale in the reflective area according to the optimal Gaussian scale index includes: setting the weight of the preset Gaussian scale corresponding to the optimal Gaussian scale index as the first weight, and setting the weights of the remaining preset Gaussian scales as the second weights; the sum of the first weight and all the second weights is equal to one, and the first weight is greater than any of the second weights.
[0016] Preferably, the step of dynamically adjusting the weight of the minimum preset Gaussian scale in the normal background area based on the local gray-level variance map includes: obtaining a specified percentile of the local gray-level variance of all pixels in the normal background area; dividing the local gray-level variance of the current pixel by the specified percentile to obtain a variance ratio; multiplying the variance ratio by a preset dynamic adjustment factor and adding it to the basic weight to obtain the weight of the minimum preset Gaussian scale of the current pixel, wherein the weight of the minimum preset Gaussian scale does not exceed a preset weight upper limit.
[0017] By employing the above technical solution, normalization is performed by extracting a specified percentile of the local gray-level variance, and the weights of the minimum preset Gaussian scale are adaptively calculated. This method cuts off the interference of extreme noise on the benchmark, allowing the edges of fine defects to obtain a very high weight ratio during fusion, and specifically amplifies the high-frequency response and texture contrast of the regular background area.
[0018] Preferably, before performing weighted fusion of the residual maps of each preset Gaussian scale based on all determined weights, the method further includes: subtracting the weight of the smallest preset Gaussian scale to obtain the remaining weights, and distributing the remaining weights equally among the preset Gaussian scales other than the smallest preset Gaussian scale to obtain the remaining preset Gaussian scale weights.
[0019] Preferably, the step of performing edge extraction and connected component feature filtering on the enhanced image and outputting defect location annotation results includes: using an edge extraction operator to extract edges from the enhanced image to obtain a binary edge map; performing connected component analysis on the binary edge map to calculate the area and aspect ratio of each connected edge region; removing connected edge regions whose area is less than a preset lower limit or whose aspect ratio is less than a preset lower limit, and retaining the connected edge regions that meet the conditions as defect regions; and outputting the defect location annotation results for each defect region.
[0020] By employing the above technical solution, the connected components after edge extraction are rigorously screened using both area and aspect ratio lower limits as morphological features. This method effectively filters out discrete noise and non-elongated morphological defects that may remain after image enhancement, significantly reducing the risk of false alarms caused by environmental factors and meeting the high-precision standards of industrial online inspection.
[0021] This invention achieves zone-based adaptive enhancement of spherical cage surface images by simultaneously processing reflective area masks and local grayscale variance maps, combined with dynamic scale matching and multi-scale residual fusion. This scheme effectively suppresses strong specular reflections on metal surfaces, faithfully preserves subtle, real-world defect details, and avoids false edge interference, significantly improving the defect detection rate of spherical cage surfaces.
[0022] Furthermore, this invention calculates the equivalent circle radius based on the area of each reflective connected region and performs a weighted summation, ensuring that the larger main reflective spot dominates the scale calculation and effectively suppresses the interference of stray bright spots on spatial scale estimation. This scheme utilizes the positive and negative features of the inter-scale difference to accurately select the core pixels of normal exposure, successfully stripping away the extremely bright real defect edges from the initial reflective area mask, avoiding the erroneous erasure of high-frequency details during illumination suppression, and ensuring the integrity of defect features from the source. This scheme extracts the specified percentile of the local grayscale variance for normalization, adaptively calculates the weight of the minimum preset Gaussian scale, and truncates the interference of extreme noise on the benchmark, giving fine defect edges a very high weight ratio during fusion, and specifically amplifying the high-frequency response and texture contrast of the regular background area. This scheme uses dual morphological features—area lower limit and aspect ratio lower limit—to rigorously screen connected edge regions, effectively filtering out residual discrete noise and non-elongated pseudo-defects after image enhancement, significantly reducing the risk of false alarms caused by the environment. Attached Figure Description
[0023] Figure 1 This is a flowchart of a method for detecting surface defects in ball cages based on image de-reflection enhancement; Figure 2 It is a grayscale image of the surface of the ball cage; Figure 3 It is a mask image of the reflective area; Figure 4 This is the corrected mask image of the reflective area; Figure 5 This is an enhanced effect image; Figure 6 This is a diagram showing the results of the defect location marking. Detailed Implementation
[0024] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are some embodiments of the present invention, but not all embodiments.
[0025] This invention discloses a method for detecting surface defects in ball cages based on image de-reflection enhancement, referring to... Figures 1 to 6 This includes steps S1-S6: S1. Perform threshold comparison on the grayscale image of the ball cage surface to obtain the reflective area mask; calculate the degree of grayscale change in the neighborhood of each pixel in the grayscale image of the ball cage surface to obtain the local grayscale variance map.
[0026] In an optional embodiment, to separate the specular reflection area generated by the polished metal surface, a grayscale threshold is set to binarize the grayscale image of the spherical cage surface. The value of each pixel in the reflective area mask is determined based on the grayscale value of the corresponding position, and the values of the reflective area mask satisfy the following relationship:
[0027]
[0028] In the formula, This represents the value of the reflective area mask at the current pixel position; This represents the grayscale value of the grayscale image of the ball cage surface at the current pixel location; This refers to the grayscale threshold. The reference value for the grayscale threshold is 200 gray levels. This value is determined by acquiring images of multiple calibrated workpieces and statistically analyzing the lower bound of the grayscale distribution of pixels in the reflective area. Pixels with grayscale values greater than the grayscale threshold are considered to belong to the specular reflective area. This area primarily carries strong reflection information from the light source rather than the actual texture information of the workpiece surface, and is marked as one; conversely, those with values less than the threshold are marked as zero.
[0029] While acquiring the reflective area mask, in order to characterize the degree of drastic gray-level changes in the edges of real defects or normal texture areas in the image, so as to selectively preserve defect details in subsequent steps, it is necessary to extract the local texture features of the image, calculate the degree of gray-level changes in the neighborhood of each pixel in the gray-level image of the ball cage surface, and obtain a local gray-level variance map. This includes: sliding a sliding window of a preset size on the gray-level image of the ball cage surface pixel by pixel, calculating the gray-level mean of all pixels within the sliding window; calculating the sum of squared differences between the gray-level value of each pixel within the sliding window and the gray-level mean, and dividing the sum of squared differences by the total number of pixels within the sliding window to obtain the local gray-level variance of the current center pixel; and traversing all pixels to obtain the local gray-level variance map.
[0030] Understandably, the local grayscale variance is calculated using the standard statistical variance formula. The preferred sliding window size is a rectangular window with a width and length of five pixels. Compared to larger windows, a sliding window with a width and length of five pixels contributes a higher proportion to the local grayscale variance when covering small scratches with a width of one to two pixels. This makes the response value of the local grayscale variance more accurately reflect the existence of tiny defect edges. The larger the value of the local grayscale variance, the more drastic the grayscale change in the neighborhood of that pixel, and the higher the probability that it corresponds to a real crack or scratch edge.
[0031] In this way, by synchronously processing the grayscale image of the spherical cage surface into two independent data streams—a reflective area mask and a local grayscale variance map—the specular reflection area that needs to be suppressed and the potential defect area that needs to retain details can be accurately distinguished at the pixel level. This provides a reliable spatial coordinate basis for subsequent partitioned adaptive enhancement and overcomes the engineering implementation problem that traditional global processing algorithms cannot simultaneously address reflection suppression and detail preservation.
[0032] S2. Extract each reflective connected region in the reflective area mask, perform weighted calculation based on the area of each reflective connected region to obtain the reflective spatial scale, and perform nearest neighbor matching with multiple preset Gaussian scales to obtain the optimal Gaussian scale index.
[0033] In an optional embodiment, to determine the spatial spread of the reflective area on the current spherical cage surface and adaptively select the best-matching scale from a set of preset Gaussian kernels to accurately estimate illumination, it is necessary to characterize the scale of the reflective connected domains. The reflective spatial scale is obtained by weighted calculation based on the area of each reflective connected domain, including: calculating the corresponding equivalent circle radius based on the area of each reflective connected domain; summing the products of the area of each reflective connected domain and the corresponding equivalent circle radius to obtain a weighted area sum; and dividing the weighted area sum by the sum of the areas of all reflective connected domains to obtain the reflective spatial scale.
[0034] In order to suppress the interference of stray bright spots through area weighting and make the main reflective spot dominate the scale estimation, the spatial scale of the reflectance satisfies the following relationship:
[0035] In the formula, For reflective spatial scale; The index of the reflective connected region; This represents the total number of reflective connected regions; For the first The area of each reflective connected region; For the first The equivalent circle radius corresponding to each reflective connected domain.
[0036] Understandably, the total number of reflective connected regions is extracted by performing 8-connected region analysis on the pixels marked as reflective areas in the reflective area mask. The equivalent circle radius converts the pixel area of the irregular connected regions into a one-dimensional scale reference value with the same dimensions as the Gaussian kernel. The above area weighting method makes the contribution weight of the larger main reflective spot to the reflective spatial scale higher, while the interference of the extremely small stray bright spots on the scale estimation is effectively suppressed. For example, when the spherical cage is facing the light source, there may be a very large main reflective spot and several small stray bright spots. After using area weighting, the main reflective spot dominates the final calculation result, avoiding the problem that the reflective spatial scale is seriously reduced by stray bright spots due to the use of simple arithmetic mean, and ensuring the strength of subsequent reflective suppression.
[0037] After obtaining the reflectivity spatial scale reflecting the current image reflectivity, it needs to be mapped to a set of discrete scales within a predefined algorithm framework. Nearest neighbor matching is performed with multiple predefined Gaussian scales to obtain the optimal Gaussian scale index. This includes: obtaining a predefined coverage coefficient; multiplying the coverage coefficient by the reflectivity spatial scale to obtain a corrected scale; calculating the absolute value of the difference between each predefined Gaussian scale and the corrected scale; and using the index of the predefined Gaussian scale with the smallest absolute difference as the optimal Gaussian scale index.
[0038] To accurately map continuous physical spatial scales to a finite set of algorithm kernels, the optimal Gaussian scale index satisfies the following relationship:
[0039] In the formula, For optimal Gaussian scale index; The sequence number for the preset Gaussian scale; For the first A preset Gaussian scale; This is the preset coverage coefficient; It is a reflective spatial scale.
[0040] It should be noted that the multiple preset Gaussian scales include a first preset Gaussian scale, a second preset Gaussian scale, and a third preset Gaussian scale, with their specific values preferably being 15, 80, and 250, respectively, used to extract fine edges, medium textures, and extremely wide illumination distributions. The reference value for the coverage coefficient is 2.0. This value is obtained by acquiring images of the calibrated workpiece at different rotation angles, calculating the mean square residuals of the reflective area output at each of the multiple preset Gaussian scales, and calibrating by taking the average of the ratios corresponding to the minimum residual. A larger reflective spatial scale means a wider spatial range of the reflective area on the current spherical cage surface, and the correction scale increases accordingly. The optimal Gaussian scale index is biased towards the preset Gaussian scale with a larger value. This ensures that subsequent illumination decomposition will use a wider Gaussian kernel to estimate the illumination components, so that the illumination residuals are fully eliminated within the wide reflective area.
[0041] Thus, by characterizing the measured area of the reflected connected region in each frame of the image as a scale value with the same dimensions as the Gaussian kernel, and combining it with an area weighting mechanism to filter stray bright interference, the selection of the Gaussian kernel is transformed from static fixed to frame-by-frame adaptive dynamic matching. This fundamentally overcomes the engineering defect of illumination estimation mismatch caused by fixed scale when the size of the reflected light changes due to the rotation of the workpiece, and ensures the accuracy of illumination estimation.
[0042] S3. Perform illumination decomposition on the grayscale image of the ball cage surface using multiple preset Gaussian scales to obtain residual maps at each preset Gaussian scale; calculate the difference between the residual maps corresponding to at least two Gaussian kernels with scale differences.
[0043] In an optional embodiment, the difference between the residual maps corresponding to the largest and smallest preset Gaussian scales is calculated to obtain the inter-scale difference. To separate low-frequency illumination components from high-frequency surface details in an image based on multi-scale Retinex theory, multi-scale illumination estimation and logarithmic domain residual calculation are required for the grayscale image of the sphere cage surface. Illumination decomposition is performed on the grayscale image of the sphere cage surface using multiple preset Gaussian scales to obtain residual maps for each preset Gaussian scale. This includes: convolving the grayscale image of the sphere cage surface with the Gaussian kernel corresponding to the preset Gaussian scale to obtain the illumination estimation map for the corresponding scale; calculating the logarithmic value of each pixel in the grayscale image of the sphere cage surface and the logarithmic value of the corresponding pixel in the illumination estimation map; and subtracting the logarithmic value of the illumination estimation map from the logarithmic value of the grayscale image of the sphere cage surface to obtain the residual map for each preset Gaussian scale.
[0044] The above-mentioned illumination decomposition calculation process adopts the standard single-scale Retinex enhancement algorithm formula. Low-frequency illumination estimates are stripped away through logarithmic subtraction, leaving the remaining high-frequency components as the residual map at the corresponding scale. Using multiple preset Gaussian scales allows for the extraction of illumination distributions from different ranges. Smaller scales result in illumination estimates that are closer to local details and higher contrast in the resulting residual map, but may lead to over-enhancement of reflective edges; larger scales result in smoother illumination estimates and more uniform overall brightness in the resulting residual map.
[0045] After obtaining the residual maps at each preset Gaussian scale, in order to extract features that characterize the differences in response between normally exposed core pixels and real specular reflection areas at different scales, and thus provide a basis for subsequent mask correction, it is necessary to align and compare the residual maps at different scales through difference operations. The differences between scales satisfy the following relationship:
[0046] In the formula, This represents the difference between scales; The residual plot corresponding to the maximum preset Gaussian scale; This is the residual plot corresponding to the smallest preset Gaussian scale. As described in the previous steps, the largest preset Gaussian scale corresponds to the third preset Gaussian scale, and the smallest preset Gaussian scale corresponds to the first preset Gaussian scale.
[0047] Understandably, for real mirror-reflective areas on a metal surface, due to their extremely high brightness and large area, the calculated illumination estimation map for this region, whether using the maximum or minimum preset Gaussian scale, will have values close to the original image brightness. This results in residual map values for this region approaching zero at different scales, thus the inter-scale difference is zero or a very small negative value. However, for real cracks or normal high-brightness textures with narrow widths, small-scale Gaussian kernels can sensitively follow their brightness abrupt changes, allowing the residual map corresponding to the minimum preset Gaussian scale to retain rich local high-frequency information; while large-scale Gaussian kernels are less affected by local abrupt changes, and the residual map corresponding to the maximum preset Gaussian scale tends to be globally smooth. This leads to a significantly positive inter-scale difference at the high-brightness areas of real textures or defects.
[0048] Thus, by using multi-scale illumination decomposition and differential calculation of residual maps at specific scales, the specular reflection and highlight defect features that were originally difficult to distinguish in a single grayscale image are cleverly transformed into positive and negative polarity differences on the inter-scale difference map. This constructs an extremely reliable mathematical feature flow for the subsequent accurate removal of misjudged pixels in the reflective area mask, breaking through the engineering technical bottleneck of reflection artifact interference without adding additional physical detection equipment.
[0049] S4. Based on the positive and negative characteristics of the difference between scales, remove the normal exposure core pixels in the reflective area mask to obtain the corrected reflective area mask.
[0050] In an optional embodiment, to eliminate misjudgments caused by reflections from bright textures or real defects in the initial threshold segmentation, it is necessary to combine the physical characteristics of the residual map response at different scales to screen out the true specular reflective pixels from the initially determined reflective area mask and strip away the edges of real defects that are easily mistaken. Based on the polarity characteristics of the inter-scale difference, a corrected reflective area mask is obtained, including: traversing each pixel in the reflective area mask with a value of one and obtaining the inter-scale difference of each pixel; in response to a pixel's inter-scale difference being greater than zero, setting the value of the corresponding pixel in the corrected reflective area mask to zero; and in response to a pixel's inter-scale difference being less than or equal to zero, setting the value of the corresponding pixel in the corrected reflective area mask to one.
[0051] It should be noted that in the initial mask extraction stage, the reflective area mask is divided into regions based solely on a single grayscale threshold. This can easily lead to the mislabeling of tiny cracks or scratches with similarly high brightness and extremely narrow width as reflective areas. According to the illumination decomposition principle described above, the residuals of real and large specular reflective areas approach zero under illumination estimation at different preset Gaussian scales, with the corresponding scale differences being zero or extremely small negative values. In contrast, the normally exposed core pixels mixed into the mask are mainly the bright edges of real defects. At the minimum preset Gaussian scale, they retain local high-frequency abrupt changes, while at the maximum preset Gaussian scale, they are smoothed out by large-area illumination. This results in a significantly positive difference between the residual maps corresponding to the maximum and minimum preset Gaussian scales. Therefore, checking pixel-by-pixel whether the scale difference is greater than zero becomes a precise criterion for identifying and removing normally exposed core pixels.
[0052] To achieve the pixel-level mask state correction and data aggregation described above, the corrected reflective area mask satisfies the following relationship:
[0053]
[0054] In the formula, This is the value of the corrected reflective area mask at the current pixel position; This represents the scale difference between the current pixel. A preset tolerance threshold is used. It's understandable that the above formula's logic assumes the current pixel's value is already one in the input reflective area mask. When the scale difference exceeds the preset tolerance threshold, it indicates the current pixel is essentially a normal exposure core pixel, triggering a removal mechanism that forcibly resets its two-dimensional matrix value in the corrected reflective area mask to zero. When the scale difference is less than or equal to the preset tolerance threshold, it's confirmed as a true specular reflective pixel, and its matrix value remains one. For non-reflective background pixels whose initial reflective area mask value is zero, they directly inherit and retain their zero value in the corrected reflective area mask. The preset tolerance threshold is selected based on the dark current background noise calibration of the industrial camera, effectively avoiding misjudgments caused by floating-point arithmetic precision and hardware noise; its preferred reference value is 0.02.
[0055] Thus, by introducing a positive and negative polarity discrimination mechanism for inter-scale differences into the data stream of multi-scale residual calculation, and using a mathematical method that maps physical space to frequency domain response, the edges of tiny defects hidden inside the strong light region are accurately stripped away. This completely avoids these key details being forcibly erased as stray reflections in the subsequent illumination suppression stage, ensuring the complete preservation of high-frequency defect characteristics from the engineering source and significantly improving the effective detection capability under strong interference illumination conditions.
[0056] S5. Based on the corrected reflective area mask, the reflective area and the normal background area are divided. In the reflective area, the weight of each preset Gaussian scale is determined according to the optimal Gaussian scale index. In the normal background area, the weight of the minimum preset Gaussian scale is dynamically adjusted according to the local gray-level variance map. Based on all the determined weights, the residual maps of each preset Gaussian scale are weighted and fused to obtain the enhanced image.
[0057] In an optional embodiment, to suppress residual light to the greatest extent possible within the actual specular reflection area, while avoiding excessive or insufficient reflection due to fixed weight allocation, it is necessary to assign different fusion ratios to different Gaussian scales based on the previously extracted spatial features. In line with the above objectives, this solution is designed as follows: In the reflection area, the weights of each preset Gaussian scale are determined based on the optimal Gaussian scale index, including: setting the weight of the preset Gaussian scale corresponding to the optimal Gaussian scale index as the first weight, and setting the weights of the remaining preset Gaussian scales as second weights; the sum of the first weight and all second weights is equal to one, and the first weight is greater than any of the second weights.
[0058] There are three preset Gaussian scales. To ensure that the illumination estimation of the optimal scale plays a dominant role, when the value of the corrected reflective area mask is one, indicating that the current pixel is in the reflective area, the preset Gaussian scale corresponding to the optimal Gaussian scale index is assigned the first weight, with a reference value of 0.6; the other two preset Gaussian scales are assigned the second weight, with a reference value of 0.2 for each. By assigning a very high fusion ratio to the illumination estimation that best matches the current reflective area, the low-frequency large-area light spots caused by strong reflections can be subtracted extremely effectively, restoring the true base grayscale masked by the reflective area.
[0059] Conversely, for normal metallic backgrounds or regular background areas with highlighted edges containing real defects, the main goal is to preserve or even amplify local high-frequency details as much as possible. Therefore, it is necessary to adaptively increase the proportion of small scales based on the intensity of local texture. This solution is designed to address these objectives by dynamically adjusting the weight of the minimum preset Gaussian scale in regular background areas based on the local grayscale variance map. This includes: obtaining the specified percentile of the local grayscale variance of all pixels in the regular background area; dividing the local grayscale variance of the current pixel by the specified percentile to obtain the variance ratio; multiplying the variance ratio by a preset dynamic adjustment factor and adding it to the base weight to obtain the weight of the minimum preset Gaussian scale for the current pixel. The weight of the minimum preset Gaussian scale does not exceed a preset weight upper limit.
[0060] To establish a pixel-level detail preservation mathematical mechanism and avoid scale compression caused by directly using variance extremum normalization, the weights of the minimum preset Gaussian scale satisfy the following relationship:
[0061] In the formula, The weight of the current pixel at the minimum preset Gaussian scale; Basic weights; This is a preset dynamic adjustment factor; This represents the local grayscale variance of the current pixel. To specify the percentile; This is the preset upper limit of the weights. The minimum preset Gaussian scale corresponds to the first preset Gaussian scale.
[0062] It should be noted that the base weight is preferably one-third, the dynamic adjustment factor is preferably one-third, the upper limit of the weight is preferably two-thirds, and the specified percentile is preferably the 95th percentile. This percentile is obtained by statistically analyzing the local gray-level variances of the general background area of the entire image in ascending order. Compared to directly using the maximum variance as the normalization benchmark, using the 95th percentile can truncate the compression interference of the normalization scale caused by a very small number of noisy pixels or extremely abnormally bright pixels, so that the variance ratio of the real small defect edges can be more evenly distributed within the effective mapping range of zero to one. The larger the local gray-level variance, the more severe the local abrupt change, and the weight of the minimum preset Gaussian scale is adjusted accordingly. This maintains the basic smooth weight in flat areas without increasing noise, while significantly enhancing the expression of high-frequency details at defect edges. For example, when there is an extremely narrow real scratch edge in the neighborhood of a pixel in a normal background area, its variance ratio approaches 0.9. At this time, the weight of the minimum preset Gaussian scale of the pixel is increased to close to 0.63, which is much higher than the basic one-third, so that the sharp edge of the fine scratch is extracted with high fidelity in the subsequent fusion process.
[0063] After determining the weights of the minimum preset Gaussian scale that dominates the details, it is essential to ensure energy conservation within the fusion framework to prevent local image overexposure or underexposure. To achieve this, the design of this scheme is as follows: before weighted fusion of the residual maps at each preset Gaussian scale based on all determined weights, the following steps are included: subtracting the weight of the minimum preset Gaussian scale from the weight to obtain the remaining weights; then, distributing the remaining weights evenly among the preset Gaussian scales (excluding the minimum preset Gaussian scale) to obtain the remaining preset Gaussian scale weights.
[0064] Understandably, in the normal background area, the weights of the other two larger preset Gaussian scales, namely the second and third preset Gaussian scales, are equal to half of the remaining weights. This ensures that the large-scale smoothing weights in the normal background area are evenly distributed, guaranteeing that throughout the entire normal background area, regardless of how the weight of the smallest preset Gaussian scale dynamically fluctuates with local textures, the sum of the weights of all preset Gaussian scales is always strictly constrained to one.
[0065] Finally, using all weights determined by the above partitioning and dynamically and continuously changing at the pixel level, lossless synthesis is performed on the multi-scale data generated by illumination decomposition. The enhanced image satisfies the following relation:
[0066] In the formula, To enhance the image; The sequence number for the preset Gaussian scale; For the corresponding pixel in the th Weights under a preset Gaussian scale; For the first A residual plot with a preset Gaussian scale.
[0067] It is understandable that the enhanced image is the final output physical quantity obtained by pixel-level linear combination and reconstruction of the logarithmic domain residual maps at each scale based on the aforementioned spatial adaptive weights. Based on this enhanced image, its floating-point value range can then be remapped to the standard grayscale range of zero to 255 using conventional linear stretching operations, in order to facilitate display and input into the backend visual operator.
[0068] Thus, by integrating the hard constraint of spatial partitioning defined by the modified reflective area mask with the soft mechanism of continuous dynamic weights dominated by local gray-level variance, within the same multi-scale algorithm framework, it not only maximizes the suppression of illumination estimation in the strong reflective area to completely eliminate artifacts, but also adaptively amplifies the high-frequency response of small real defects in the normal background area. This breaks through the engineering application contradiction of "unable to suppress reflections" and "easy to smooth out details" that is easily caused by the fixed fusion weights of traditional multi-scale processing algorithms, greatly improving the image signal-to-noise ratio of the defect area and achieving extremely outstanding enhancement effects in the complex surface lighting environment of metal processing parts.
[0069] S6. Perform edge extraction and connected component feature filtering on the enhanced image, and output the defect location annotation results.
[0070] In an optional embodiment, in order to accurately extract the true defect edges of the ball cage surface from the de-reflection enhanced image, edge extraction and connected component feature filtering are performed on the enhanced image, and defect location annotation results are output. This includes: using an edge extraction operator to extract edges from the enhanced image to obtain a binary edge map; performing connected component analysis on the binary edge map to calculate the area and aspect ratio of each connected edge region; removing connected edge regions with an area smaller than a preset lower limit or an aspect ratio smaller than a preset lower limit, and retaining the connected edge regions that meet the conditions as defect regions; and outputting the defect location annotation results for each defect region.
[0071] The edge extraction operator is preferably the Canny operator, which includes four steps: Gaussian smoothing, gradient magnitude calculation, non-maximum suppression, and dual-threshold hysteresis linking. In the dual-threshold hysteresis linking, a low threshold and a high threshold are set. The preferred reference value for the low threshold is 30 gray levels per pixel, and the preferred reference value for the high threshold is 70 gray levels per pixel. It can be understood that a larger high threshold means the edge extraction operator retains only edges with stronger gradient magnitudes, resulting in fewer extracted edges and higher accuracy, but weak edges such as small scratches may be missed; conversely, a smaller high threshold can easily introduce too many interfering edges.
[0072] After obtaining the binary edge map, 8-connected component analysis is performed on all edge pixels in the binary edge map to extract multiple connected edge regions. Then, the area and aspect ratio of each connected edge region are calculated. The preset lower limit for area is preferably 5 square pixels, and the preset lower limit for aspect ratio is preferably 3. It is understandable that the larger the preset lower limit for area, the more small connected regions are filtered out, and the number of false defects decreases accordingly. However, even extremely small real defects still face the risk of being missed. After using the dual screening conditions of aspect ratio and area, noise points that do not conform to the slender morphological characteristics of real scratches or cracks are eliminated, and connected edge regions with an area greater than or equal to the preset lower limit for area and an aspect ratio greater than or equal to the preset lower limit for aspect ratio are retained as defect regions.
[0073] Finally, the defect location marking results for each defect area are output, including the centroid coordinates and circumscribed rectangle of each defect area. These results are then uploaded to the quality inspection record system to determine whether the current ball cage workpiece triggers the rework process.
[0074] Thus, by performing edge extraction and connected component morphology dual screening on the enhanced image, residual stray noise is effectively removed, and the proportion of reflective false edges in the total number of detected edges in each frame image is reduced, which greatly improves the effective detection rate of real defects on the surface of the ball cage, thereby meeting the strict quality control requirements of online inspection on the production line.
[0075] In the description of this specification, "multiple" or "several" means at least two, such as two, three or more, unless otherwise expressly and specifically defined.
Claims
1. A method for detecting surface defects in a ball cage based on image de-reflection enhancement, characterized in that, include: Threshold comparison is performed on the grayscale image of the ball cage surface to obtain the reflective area mask; Calculate the degree of grayscale variation in the neighborhood of each pixel in the grayscale image of the ball cage surface to obtain a local grayscale variance map; Extract each reflective connected region in the reflective area mask, perform weighted calculation based on the area of each reflective connected region to obtain the reflective spatial scale, and perform nearest neighbor matching with multiple preset Gaussian scales to obtain the optimal Gaussian scale index; The grayscale image of the ball cage surface is subjected to illumination decomposition using the multiple preset Gaussian scales to obtain residual maps at each preset Gaussian scale; the difference between the residual maps corresponding to at least two Gaussian kernels with scale differences is calculated. Based on the comparison between the inter-scale difference and the preset tolerance threshold, the normally exposed core pixels in the reflective area mask are removed to obtain the corrected reflective area mask. The reflective area and the normal background area are divided according to the corrected reflective area mask. In the reflective area, the weight of each preset Gaussian scale is determined according to the optimal Gaussian scale index. In the normal background area, the weight of the minimum preset Gaussian scale is dynamically adjusted according to the local gray-level variance map. The residual maps of each preset Gaussian scale are weighted and fused according to all the determined weights to obtain the enhanced image. Edge extraction and connected component feature filtering are performed on the enhanced image, and the defect location annotation results are output.
2. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of calculating the reflective spatial scale by weighting the area of each reflective connected domain includes: calculating the corresponding equivalent circle radius based on the area of each reflective connected domain; summing the product of the area of each reflective connected domain and the corresponding equivalent circle radius to obtain a weighted sum of areas; and dividing the weighted sum of areas by the sum of the areas of all reflective connected domains to obtain the reflective spatial scale.
3. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of performing nearest neighbor matching with multiple preset Gaussian scales to obtain the optimal Gaussian scale index includes: obtaining a preset coverage coefficient; multiplying the coverage coefficient by the reflective space scale to obtain a corrected scale; calculating the absolute value of the difference between the multiple preset Gaussian scales and the corrected scale; and taking the index of the preset Gaussian scale with the smallest absolute value of the difference as the optimal Gaussian scale index.
4. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of calculating the degree of grayscale variation in the neighborhood of each pixel in the grayscale image of the ball cage surface to obtain a local grayscale variance map includes: sliding a sliding window of a preset size on the grayscale image of the ball cage surface pixel by pixel, calculating the grayscale mean of all pixels within the sliding window; calculating the sum of squared differences between the grayscale value of each pixel within the sliding window and the grayscale mean, and dividing the sum of squared differences by the total number of pixels within the sliding window to obtain the local grayscale variance of the current center pixel; and traversing all pixels to obtain the local grayscale variance map.
5. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of performing illumination decomposition on the grayscale image of the ball cage surface using the multiple preset Gaussian scales to obtain residual maps at each preset Gaussian scale includes: convolving the grayscale image of the ball cage surface with the Gaussian kernel corresponding to the preset Gaussian scale to obtain an illumination estimation map at the corresponding scale; calculating the logarithm of each pixel in the grayscale image of the ball cage surface and the logarithm of the corresponding pixel in the illumination estimation map; and subtracting the logarithm of the illumination estimation map from the logarithm of the grayscale image of the ball cage surface to obtain the residual map at each preset Gaussian scale.
6. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of removing normally exposed core pixels from the reflective area mask based on the comparison result of the inter-scale difference value and the preset tolerance threshold to obtain the corrected reflective area mask includes: traversing each pixel in the reflective area mask with a value of one, and obtaining the inter-scale difference value of each pixel; determining whether the inter-scale difference value of the pixel is greater than the preset tolerance threshold; in response to the inter-scale difference value of the pixel being greater than the preset tolerance threshold, setting the value of the corresponding pixel in the corrected reflective area mask to zero; in response to the inter-scale difference value of the pixel being less than or equal to the preset tolerance threshold, setting the value of the corresponding pixel in the corrected reflective area mask to one.
7. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of determining the weight of each preset Gaussian scale in the reflective area according to the optimal Gaussian scale index includes: setting the weight of the preset Gaussian scale corresponding to the optimal Gaussian scale index as the first weight, and setting the weights of the remaining preset Gaussian scales as the second weights; the sum of the first weight and all the second weights is equal to one, and the first weight is greater than any of the second weights.
8. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of dynamically adjusting the weight of the minimum preset Gaussian scale in the normal background area based on the local gray-level variance map includes: obtaining a specified percentile of the local gray-level variance of all pixels in the normal background area; dividing the local gray-level variance of the current pixel by the specified percentile to obtain a variance ratio; multiplying the variance ratio by a preset dynamic adjustment factor and adding it to the basic weight to obtain the weight of the minimum preset Gaussian scale of the current pixel, wherein the weight of the minimum preset Gaussian scale does not exceed a preset weight upper limit.
9. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 8, characterized in that, Before performing weighted fusion of the residual maps of each preset Gaussian scale based on all the determined weights, the method further includes: subtracting the weight of the smallest preset Gaussian scale to obtain the remaining weights, and distributing the remaining weights equally among the preset Gaussian scales other than the smallest preset Gaussian scale to obtain the remaining preset Gaussian scale weights.
10. The method for detecting surface defects of a ball cage based on image de-reflection enhancement according to claim 1, characterized in that, The step of performing edge extraction and connected component feature filtering on the enhanced image and outputting defect location annotation results includes: using an edge extraction operator to extract edges from the enhanced image to obtain a binary edge map; performing connected component analysis on the binary edge map to calculate the area and aspect ratio of each connected edge region; removing connected edge regions whose area is less than a preset lower limit or whose aspect ratio is less than a preset lower limit, and retaining the connected edge regions that meet the conditions as defect regions; and outputting the defect location annotation results for each defect region.