Image labeling grayscale compensation method, system, electronic device and medium

CN122597244APending Publication Date: 2026-08-18ZHUHAI BOJAY ELECTRONICS
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611077318.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-20
Publication Date
2026-08-18

AI Technical Summary

Technical Problem

这些原始标记(如二维码、序列号、定位标识等)与背景之间的灰度阶跃特征在补偿过程中往往被显著削弱或整体重塑,导致标记在灰度补偿过后可辨识度下降甚至丢失,因此现有技术无法兼顾灰度均匀化与标记形态完整性的双重需求

Benefits of technology

[0005] The embodiments of this invention have at least the following beneficial effects: By introducing a constraint relationship between the grid side length and the minimum size of the original mark on the product surface, the invention divides the region containing the mark in the image to be compensated into multiple fine grids. During the process of averaging the pixels within each grid, the gray-level step details of the mark are physically stripped away, retaining only the low-resolution image reflecting the macroscopic illumination trend. Based on this, the deviation of each grid region relative to the target gray-level value is calculated, and this deviation is upsampled and applied back to the original image in a superimposed manner. The method provided by this invention effectively eliminates local gray-level drift in the image and achieves uniform image gray-level standards, while completely preserving the gray-level step characteristics between the original mark on the product surface and the background, ensuring that the mark remains recognizable after compensation. This solves the problem of weakened, distorted, or lost mark information in existing gray-level compensation methods when processing product images with marks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122597244A_ABST
    Figure CN122597244A_ABST
Patent Text Reader

Abstract

The application discloses a kind of image mark gray compensation method, system, electronic equipment and medium, the method relates to computer vision detection field, the method specifically includes: obtaining the image mark containing image to be compensated image;Based on the grid side length of pre-set, the preset region containing image mark in to-be-compensated image is carried out grid down sampling processing, and down sampling image is obtained;Wherein, grid side length is less than the smaller value in width, height of image mark in image plane;The gray difference value of the gray value of each down sampling pixel and pre-set target gray value is obtained, and all gray difference values are mapped and arranged according to the spatial coordinates of corresponding down sampling pixel, and difference value image is generated;Difference value image is carried out spatial interpolation up sampling processing, and compensation amount image is obtained;Based on compensation amount image, pre-set region is compensated in gray scale.The method provided by the application image mark still has discernibility after being compensated in gray scale.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer vision inspection, and in particular to a grayscale compensation method, system, electronic device, and medium for image marking. Background Technology

[0002] In the field of computer vision inspection, image enhancement is a fundamental method for improving image quality and enhancing the accuracy of subsequent analysis. Gray-scale correction and illumination homogenization aim to eliminate uneven gray-scale distribution caused by factors such as lighting conditions, imaging equipment, or object surface characteristics. In industrial visual defect detection, due to factors such as differences in material composition, changes in light source angle, and uneven reflection, acquired images often exhibit local gray-scale drift, affecting the robustness of detection algorithms. Existing methods, such as industrial camera vignetting compensation and histogram equalization, improve gray-scale uniformity through edge gain compensation or global gray-scale stretching, respectively, but neither considers the need to preserve the original markings on the product surface during gray-scale compensation. The gray-scale step features between these original markings (such as QR codes, serial numbers, and positioning marks) and the background are often significantly weakened or completely reshaped during compensation, leading to decreased or even lost recognizability of the markings after gray-scale compensation. Therefore, existing technologies cannot simultaneously meet the dual requirements of gray-scale uniformity and marking morphological integrity. Summary of the Invention

[0003] This invention aims to at least solve one of the technical problems existing in the prior art. To this end, this invention proposes a grayscale compensation method, system, electronic device, and medium for image markers, which can protect the recognizability of image markers during the grayscale compensation process and prevent the loss of information contained in the image markers.

[0004] A grayscale compensation method for image markers according to a first aspect embodiment of the present invention includes: Obtain the image to be compensated, which includes image markers; Based on a preset grid side length, a gridded downsampling process is performed on a preset region containing the image marker in the image to be compensated to obtain a downsampled image; wherein, the grid side length is less than the smaller of the width and height of the image marker in the image plane, so that the image marker is segmented into multiple grids; the downsampled image includes multiple downsampled pixels corresponding to the grids, and the gray value of each downsampled pixel is the average gray value of all pixels in the corresponding grid; Obtain the grayscale difference between the grayscale value of each downsampled pixel and the preset target grayscale value, and arrange all the grayscale differences according to the spatial coordinates of the corresponding downsampled pixels to generate a difference image; Spatial interpolation upsampling is performed on the difference image to obtain a compensation image; wherein the resolution of the compensation image is the same as that of the image to be compensated. The compensation image is superimposed pixel by pixel onto the pixels corresponding to the preset region to perform grayscale compensation on the preset region.

[0005] The embodiments of this invention have at least the following beneficial effects: By introducing a constraint relationship between the grid side length and the minimum size of the original mark on the product surface, the invention divides the region containing the mark in the image to be compensated into multiple fine grids. During the process of averaging the pixels within each grid, the gray-level step details of the mark are physically stripped away, retaining only the low-resolution image reflecting the macroscopic illumination trend. Based on this, the deviation of each grid region relative to the target gray-level value is calculated, and this deviation is upsampled and applied back to the original image in a superimposed manner. The method provided by this invention effectively eliminates local gray-level drift in the image and achieves uniform image gray-level standards, while completely preserving the gray-level step characteristics between the original mark on the product surface and the background, ensuring that the mark remains recognizable after compensation. This solves the problem of weakened, distorted, or lost mark information in existing gray-level compensation methods when processing product images with marks.

[0006] According to some embodiments of the present invention, after the step of performing gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image, the method further includes: The downsampled image is subjected to local neighborhood smoothing to obtain a smoothed downsampled image; The absolute value of the difference between the downsampled image and the smoothed downsampled image is calculated pixel by pixel to obtain the difference map; Pixels with pixel values ​​greater than or equal to a preset defect determination threshold in the difference image are marked as defect areas, and pixels with pixel values ​​lower than the preset defect determination threshold are marked as normal lighting areas. A binary mask image is generated based on the defect areas and the normal lighting areas.

[0007] According to some embodiments of the present invention, after the step of generating the difference image, the method further includes: Based on the binary mask image, the difference image is corrected. The correction includes setting the grayscale difference of the pixels corresponding to the defect area in the difference image to zero, while keeping the grayscale difference of the normally illuminated area unchanged. Performing a morphological closing operation on the difference image includes: first performing a morphological dilation operation on the difference image, and then performing a morphological erosion operation on the dilated difference image.

[0008] According to some embodiments of the present invention, after the step of generating the difference image, the method further includes: A linear mapping relationship is determined based on the minimum value of the grayscale difference, the maximum value of the grayscale difference, and the lower and upper limits of the preset compensation range. Based on the linear mapping relationship, each gray-level difference in the difference image is mapped, and each gray-level difference is normalized to the compensation range.

[0009] According to some embodiments of the present invention, after the step of superimposing the compensation image pixel by pixel onto the pixels corresponding to the preset region to perform grayscale compensation on the preset region, the method further includes: If the grayscale value of the superimposed pixel exceeds the upper limit of the preset grayscale range, the grayscale value of the superimposed pixel is corrected to the upper limit; if the grayscale value of the superimposed pixel is lower than the lower limit of the preset grayscale range, the grayscale value of the superimposed pixel is corrected to the lower limit.

[0010] According to some embodiments of the present invention, before the step of performing gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image, the method further includes: Obtain the deviation between the average grayscale value of all pixels within the preset area and the preset target grayscale value; If the deviation is less than a preset threshold, the image to be compensated is directly output; otherwise, the steps of performing gridded downsampling processing on the preset region, generating a difference image, performing spatial interpolation upsampling processing, and performing grayscale compensation are continued.

[0011] According to some embodiments of the present invention, before the step of performing gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image, the method further includes: Determine whether the grid side length is zero, negative, or greater than the shortest side of the preset area. If so, set the grid side length to 1 pixel.

[0012] A grayscale compensation system for image marking according to a second aspect embodiment of the present invention includes: The image acquisition module is used to acquire the image to be compensated, which includes image markers; The downsampling module is used to perform gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length, to obtain a downsampled image, such that the image marker is segmented into multiple grids; wherein, the grid side length is less than the smaller of the width and height of the image marker in the image plane; the downsampled image includes multiple downsampled pixels corresponding to the grids, and the grayscale value of each downsampled pixel is the average grayscale value of all pixels in the corresponding grid; The difference image generation module is used to obtain the gray value difference between the gray value of each downsampled pixel and the preset target gray value, and to map and arrange all the gray value differences one by one according to the spatial coordinates of the corresponding downsampled pixels to generate a difference image. An upsampling module is used to perform spatial interpolation upsampling processing on the difference image to obtain a compensation image; wherein the resolution of the compensation image is the same as that of the image to be compensated; The grayscale compensation module is used to superimpose the compensation amount image pixel by pixel onto the pixels corresponding to the preset area to perform grayscale compensation on the preset area.

[0013] An electronic device according to a third aspect embodiment of the present invention includes a memory and a processor, the memory being used to store at least one program, and the processor being used to load the at least one program to perform the grayscale compensation method for image markers described in the above-described aspect embodiments.

[0014] According to a fourth aspect of the present invention, a computer-readable storage medium stores computer-executable instructions for causing a computer to perform a grayscale compensation method for image markers as described in the above-described aspects.

[0015] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0016] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the description of the embodiments taken in conjunction with the following drawings, in which: Figure 1 This is a flowchart of the grayscale compensation method for image marking according to an embodiment of the present invention; Figure 2 This is an overall schematic diagram of the grayscale compensation method for image marking according to an embodiment of the present invention; Figure 3 This is a structural diagram of an electronic device provided in another embodiment of the present invention. Detailed Implementation

[0017] Embodiments of the present invention are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present invention, and should not be construed as limiting the present invention.

[0018] In the description of this invention, "several" means one or more, "multiple" means two or more, "greater than," "less than," "exceeding," etc. are understood to exclude the stated number, and "above," "below," "within," etc. are understood to include the stated number. If "first," "second," etc. are used in the description, they are only for the purpose of distinguishing technical features and should not be construed as indicating or implying relative importance or implicitly indicating the number of indicated technical features or the order of the indicated technical features.

[0019] In the field of computer vision inspection, image enhancement is a fundamental method for improving image quality and enhancing the accuracy of subsequent analysis. Gray-scale correction and illumination homogenization aim to eliminate uneven gray-scale distribution caused by factors such as lighting conditions, imaging equipment, or object surface characteristics. In industrial visual defect detection, due to factors such as differences in material composition, changes in light source angle, and uneven reflection, acquired images often exhibit local gray-scale drift, affecting the robustness of detection algorithms. Existing methods, such as industrial camera vignetting compensation and histogram equalization, improve gray-scale uniformity through edge gain compensation or global gray-scale stretching, respectively, but neither considers the need to preserve the original markings on the product surface during gray-scale compensation. The gray-scale step features between these original markings (such as QR codes, serial numbers, and positioning marks) and the background are often significantly weakened or completely reshaped during compensation, leading to decreased or even lost recognizability of the markings after gray-scale compensation. Therefore, existing technologies cannot simultaneously meet the dual requirements of gray-scale uniformity and marking morphological integrity.

[0020] The concepts involved in this invention are explained below: ROI: Region of Interest.

[0021] Please refer to Figures 1-3 , Figure 1 This is a flowchart of an image marker grayscale compensation method according to an embodiment of the present invention. This embodiment discloses an image marker grayscale compensation method, which includes, but is not limited to, the following steps: Step S100: Obtain the image to be compensated, which includes image markers; It should be noted that the image to be compensated obtained in this embodiment of the invention contains image markers that need to be retained. These image markers are the objects of subsequent mesh size constraints, marker stripping, and compensation processing. The types of image markers include QR codes, Data Matrix codes, serial number characters, positioning crosshairs, or scale lines, etc., which are manually identified. In practical applications, any method that can provide an image to be compensated containing image markers is applicable to this step. Those skilled in the art can choose an appropriate acquisition method based on the specific detection scenario and equipment conditions.

[0022] Step S200: Based on the preset grid side length, perform gridded downsampling processing on the preset region containing image markers in the image to be compensated to obtain a downsampled image; wherein, the grid side length is less than the smaller of the width and height of the image marker in the image plane, so that the image marker is divided into multiple grids; the downsampled image includes multiple downsampled pixels corresponding to the grid, and the gray value of each downsampled pixel is the average gray value of all pixels in the corresponding grid; It should be noted that step S200 aims to separate the illumination trend of the image marker from the image to be compensated through gridded downsampling processing. Specifically, the gridded downsampling process is as follows: Based on a preset grid side length, the preset region is divided into multiple non-overlapping grids in both the horizontal and vertical directions. For each grid, the arithmetic mean of the grayscale values ​​of all pixels within its coverage area is calculated, and this mean is used as the grayscale value of the corresponding pixel in the downsampled image. After the mean values ​​of all grids are calculated, they are arranged according to their row and column positions within the preset region to form the downsampled image. The resolution of this downsampled image is lower than that of the preset region, and each pixel represents the macroscopic average brightness of a grid region in the original image. It is understandable that, since the grid side length is set to be smaller than the minimum size of the image marker, a complete image marker must be spatially divided into multiple different grids and cannot completely fall within any single grid. Based on this, the grayscale mean is calculated for all pixels within each grid. This averaging operation actively discards the grayscale variation relationships between pixels within each grid (i.e., the microscopic details of the markers), retaining only the macroscopic average brightness level of that grid region. The resulting downsampled image is essentially a low-resolution image stripped of the marker microscopic details, reflecting only the macroscopic illumination trends of each local area. This provides the data structure foundation for subsequent calculations of compensation in a "marker-free" space. Simultaneously, the downsampling process significantly reduces the number of pixels in the image, substantially reducing the computational load of subsequent filtering, interpolation, and other operations, thus improving overall processing efficiency.

[0023] In some specific embodiments of the present invention, in order to reduce the computational load of data processing, the preset area in the above-mentioned embodiments is a pre-set ROI region, that is, a designated region of interest, and all subsequent compensation operations are limited to this region. Specifically, the input image to be compensated is... ,in , and These are the width and height of the image, respectively (in pixels). The user specifies the compensation area, and the system converts that area into a region of interest for image processing. : ·····①; in and are the top-left and bottom-right corner coordinates of the ROI region, respectively, in pixels. The ROI region is a rectangular area containing from arrive , arrive All pixels. Further, extract the ROI region image from the original image. ,in , ,in and Local coordinates within the ROI region and These represent the width and height of the ROI region, respectively. All subsequent compensation operations will only apply to this region. Within the region, the images of non-target regions to be compensated retain their original data. It can be understood that gridded downsampling processing for a preset region can be interpreted as gridded downsampling processing for the ROI region, or, without considering computational complexity, as gridded downsampling processing for the entire or part of the image to be compensated.

[0024] In some specific embodiments of the present invention, the number of grids needs to be determined before performing gridded downsampling processing: ·····②; In equation ②, M represents the number of grid cells in the horizontal direction, and its value ranges from [value missing]. ; The number of grid cells in the vertical direction, with a value range of [value missing]. ;symbol To round up, when the results of M and N are not integers, the result is rounded up to the nearest integer, ensuring that even pixels at the very edge of the ROI region are included in the outermost grid. Furthermore, the grayscale value of each pixel in the downsampled image is obtained by the following formula: ······③; In Equation ③, g is the grid side length, so a single standard grid contains g rows and g columns of original pixels; The total number of pixels within the complete grid is used as the fixed denominator for mean normalization calculation; i is the horizontal grid index of the downsampled image, with a value range of [0, M]. 1], j are the vertical grid indices of the downsampled image, with values ​​ranging from [0, N]. 1]; U is the total number of pixels horizontally in the region of interest image, and V is the total number of pixels vertically in the region of interest image. 1. V 1 represents the maximum index of the corresponding region of interest (ROI) image pixel; u and v are the pixel coordinates of the original ROI image. Let (u, v) be the pixel grayscale value of the original region of interest image at coordinates (u, v). When calculating the grayscale value of the downsampled pixel corresponding to a single grid cell, the horizontal and vertical starting coordinates of the grid cell in the original image are determined by the product of the grid indices i and j and the grid side length g, respectively. The theoretical horizontal and vertical ending coordinates are determined by (i+1)·g-1 and (j+1)·g-1, respectively. Boundary constraints are applied by taking the smaller value between the theoretical ending coordinates and the actual boundary pixel index of the image to ensure that invalid pixels outside the image are not read. Then, the grayscale values ​​of all valid original pixels within the grid cell are summed, and the sum is divided by the square of g to obtain the grid grayscale mean. This mean value is assigned as the grayscale value of the pixel at coordinates (i, j) in the downsampled image. This calculation method uniformly completes grid segmentation, adaptive boundary truncation, and mean downsampling. Even if the image size cannot be divided by the grid side length, pixels with insufficient edges to form a complete grid will not be missed. Furthermore, all grids use a unified normalized denominator to ensure consistent grayscale scale in the downsampled image. While weakening the random grayscale noise at a single point in the original image, it fully preserves the overall grayscale change trend of the region of interest.

[0025] In some specific embodiments of the present invention, before performing gridded downsampling processing on the preset area, it is first necessary to determine the minimum size of the image marker. The minimum size is defined as the smaller value of the marker in the width and height directions within the image plane, in pixels. ······④; In formula ④, and These represent the width and height of the image markers, respectively. Further, the grid size g is set to satisfy... The core constraint, physically speaking, is that when the grid side length is less than the minimum size of the marker, the marked area will span at least 2×2 grid cells (4 grid units). It can be understood that the minimum size can be obtained through visual recognition or direct input by the inspector. To obtain a more accurate marker size, rotation correction can be performed on the image to be compensated or the preset area. If the image marker is an irregular shape, the smaller of the width and height of the minimum bounding rectangle aligned to the axis of the image marker is used as the minimum size. In some embodiments, the grid side length can be set to be less than half the minimum size of the image marker, ensuring the marker spans at least 4×4 grid cells, thereby guaranteeing sufficient stripping of marker details with greater redundancy.

[0026] Step S300: Obtain the gray value difference between the gray value of each downsampled pixel and the preset target gray value, and map all gray value differences one by one according to the spatial coordinates of the corresponding downsampled pixels to generate a difference image. It should be noted that in the downsampled image of the above-described embodiments, the grayscale value of each pixel represents the macroscopic average brightness of the corresponding grid area. This brightness value has been stripped of the microscopic details of the markings and only reflects the illumination trend. A preset target grayscale value is used as a unified grayscale standard, and the difference between this value and the grayscale value of each downsampled pixel is calculated one by one. The resulting grayscale difference reflects the degree of deviation of the grid area from the target standard—a positive value indicates that the area is too dark and needs to be brightened, while a negative value indicates that the area is too bright and needs to be darkened. After obtaining the grayscale difference values, each grayscale difference value can be directly assigned to a two-dimensional matrix that corresponds one-to-one with the spatial coordinates of the downsampled image. This two-dimensional matrix is ​​the difference image. The number of rows and columns of the difference image is equal to the number of rows and columns of the downsampled image, respectively. The grayscale value of the pixel in the i-th row and j-th column of the difference image is obtained by subtracting the grayscale value of the pixel in the i-th row and j-th column of the downsampled image from the preset target grayscale value.

[0027] In some specific embodiments, the preset target grayscale value is a configurable, uniform numerical value, typically pre-calibrated based on the sample grayscale values ​​of standard qualified products, or manually specified by the user according to testing requirements. Its value ranges from 0 to 255, and is an integer. Furthermore, the preset target grayscale value may not be a fixed value, but rather a target grayscale image with the same resolution as the downsampled image. Pixels at different locations in the target grayscale image can have different target grayscale values ​​to suit application scenarios where different regions of the image have differentiated grayscale requirements. During difference calculation, the grayscale value of each pixel in the target grayscale image is subtracted from the grayscale value of the corresponding pixel in the downsampled image, pixel by pixel, to obtain the grayscale difference at each pixel location.

[0028] Step S400: Perform spatial interpolation upsampling processing on the difference image to obtain a compensation image; wherein the resolution of the compensation image is the same as that of the image to be compensated. It should be noted that step S400 is used to map the compensation information calculated in the low-resolution space back to the original high-resolution space, establishing a pixel correspondence between each original pixel and its corresponding compensation amount. The resolution of the difference image generated in step S300 is the same as that of the downsampled image, but much lower than the resolution of the original image to be compensated. Each pixel in the difference image only represents the overall compensation requirement of a single grid region and cannot be directly superimposed with each pixel of the original image. Through spatial interpolation upsampling, based on the grayscale values ​​and spatial relationships of each pixel in the difference image, the grayscale values ​​of the newly added pixels are reasonably estimated in the magnified image, ensuring that the final compensation image is completely consistent with the image to be compensated in resolution. This provides accurate pixel-level compensation data for subsequent pixel-by-pixel superposition compensation operations.

[0029] In some specific embodiments of the present invention, a bilinear spatial interpolation algorithm can be used to upsample and enlarge the corrected difference image, restoring its resolution to be completely consistent with the original ROI image. This interpolation method selects the four adjacent grid pixels of the target pixel within the low-resolution difference image for weighted averaging, outputting an enlarged image with smooth grayscale transition, which can completely avoid the blocky and mosaic artifacts caused by direct enlargement. After the interpolation and enlargement are completed, the high-resolution compensation image is superimposed pixel by pixel with the original ROI image, and the grayscale of the superimposed pixels is constrained within an interval, finally outputting the ROI image with grayscale compensation completed. The method specifically includes: First, calculate the scale factors in the horizontal and vertical directions: ·····⑤; In formula ⑤, The horizontal scaling factor. Where is the scaling factor in the vertical direction, and M is the number of grid cells in the horizontal direction. U represents the number of grid cells in the vertical direction, U represents the total number of pixels in the horizontal direction of the region of interest image, and V represents the total number of pixels in the vertical direction of the region of interest image.

[0030] Furthermore, for any target pixel coordinate (u, v) within the magnified compensated image, the mapping position of that pixel within the low-resolution corrected difference image is solved in reverse, yielding: ······⑥; Equation ⑥ is illustrated below, where the target x-coordinate u is divided by the horizontal scaling factor. Round the calculation result down to obtain the corresponding integer x-coordinate i in the low-resolution image; divide the target y-coordinate v by the vertical scaling factor. Round the calculation result down to obtain the corresponding integer ordinate j in the low-resolution image; solve for the horizontal decimal offset α: the result of dividing the target's x-coordinate by the scaling factor minus the integer x-coordinate i, with a value range of [0,1], representing the relative offset distance of the target point between the two horizontal grids i and i+1; solve for the vertical decimal offset β: the result of dividing the target's ordinate by the scaling factor minus the integer ordinate j, with a value range of [0,1], representing the relative offset distance of the target point between the two vertical grids j and j+1. The floor function is used to identify the coordinates (i, j) of the top-left neighboring pixel of the target pixel in the low-resolution difference image. Based on these coordinates, the four neighboring pixels participating in the weighted calculation can be determined: top-left (i, j), top-right (i+1, j), bottom-left (i, j+1), and bottom-right (i+1, j+1).

[0031] Furthermore, the gray value of the target pixel after enlargement is calculated by weighting the gray values ​​of the four neighboring pixels, the horizontal offset α, and the vertical offset β. The complete interpolation calculation formula is as follows: ...Form ⑦; In formula ⑦, The value of the pixel closest to the top left corner of the source image. The value of the pixel closest to the top right corner of the source image. The value of the pixel closest to the bottom left corner of the source image. This represents the value of the pixel closest to the bottom right corner of the source image. The weighting coefficients for the four terms are as follows: , , , The sum of the four sets of coefficients is always equal to 1, ensuring that the grayscale output of the interpolation will not have numerical deviation.

[0032] In some specific embodiments of the present invention, a nearest neighbor interpolation algorithm can also be used for upsampling processing to reduce computational complexity, making it suitable for real-time detection scenarios with high processing speed requirements. Specifically, the scaling factor based on the above-described embodiments... For any target pixel coordinate within the magnified compensated image ( , Inverse mapping to the corresponding position in the low-resolution corrected difference image , ( , () represents the coordinates of the nearest low-resolution grid pixel to the target pixel. Further, the nearest neighbor interpolation amplification calculation formula is: ...Form ⑧; In formula ⑧, Correcting coordinates in low-resolution difference images The algorithm directly selects the gray value of the single grid closest to the mapping point as the output value of the magnified pixel, eliminating the need for weighted summation of multiple neighboring pixels and greatly simplifying the computation steps.

[0033] Step S500: The compensation image is superimposed pixel by pixel onto the pixels corresponding to the preset region to perform grayscale compensation on the preset region.

[0034] It should be noted that step S500 is used to precisely apply the pixel-level compensation information contained in the compensation image generated in step S400 to the target area of ​​the original image to be compensated, completing the final correction of local gray-level drift. Each pixel value in the compensation image represents the gray-level offset that needs to be adjusted for the corresponding original pixel—a positive value indicates that the position is too dark and needs to be brightened by increasing gray-level, while a negative value indicates that the position is too bright and needs to be darkened by decreasing gray-level. By superimposing the compensation image with the corresponding pixels in the preset area of ​​the original image to be compensated one by one, the gray-level value of each pixel is corrected independently, thereby eliminating local gray-level drift of arbitrary shape and position caused by factors such as uneven lighting and material differences. Since the compensation value in the compensation image comes entirely from the downsampled image in step S200 where the micro-details of the marker have been stripped, the compensation operation itself only corrects the macro-level lighting trend and does not touch the gray-level step relationship between the marker and the background. Therefore, it will not have too much impact on the integrity of the background marker, so that the information contained in the background marker can still be recognized after compensation. Meanwhile, as a linear operation, the overlay operation does not change the relative grayscale differences between pixels in the original image. The grayscale step characteristics of the marked area remain completely consistent before and after compensation, and the morphological integrity and recognizability of the marker are fully preserved. Finally, the grayscale values ​​of each pixel within the preset area are uniformly corrected to near the preset target grayscale value, achieving grayscale standard unification among images acquired from multiple production lines and workstations, while the image pixels outside the preset area remain unchanged, fully preserving the background information of the original image.

[0035] It should be noted that this invention first identifies the processing area containing image markers in the image to be compensated. Then, it divides the image into a dense grid using a grid smaller than the minimum size of the markers. By aggregating the mean values ​​of pixels within the grid, the microscopic morphological details of the markers are stripped away to reveal the macroscopic illumination distribution trend, transforming it into a low-resolution downsampled image. Subsequently, in this low-resolution space, the deviation from the preset target grayscale value is calculated based on the local mean of the downsampled image, resulting in a difference image reflecting the required compensation magnitude for each local area. Then, spatial interpolation maps the low-resolution compensation amount back to the original image size. Since the compensation amount comes entirely from the downsampled data with the marker details stripped away, the superposition compensation operation only corrects the macroscopic illumination trend and does not reshape or smooth out the original grayscale step between the markers and the background. This achieves the technical effect of completely preserving the morphology of the image markers while eliminating local grayscale drift.

[0036] Reference Figure 2 In addition, after step S200 in the above-described embodiments, the following steps are included, but are not limited to: Step S210: Perform local neighborhood smoothing on the downsampled image to obtain a smoothed downsampled image; Step S220: Calculate the absolute value of the difference between the downsampled image and the smoothed downsampled image pixel by pixel to obtain the difference map; Step S230: Mark the pixel positions in the difference map that are greater than or equal to the preset defect judgment threshold as defect areas, and mark the pixel positions that are lower than the preset defect judgment threshold as normal lighting areas, and generate a binary mask map based on the defect areas and normal lighting areas.

[0037] It should be noted that while the microscopic details of the markings have been removed from the downsampled image obtained in step S200 through grid mean aggregation, some isolated gray-level anomalies may still remain in the downsampled image due to potential minor defects on the product surface or camera sensor noise. These anomalies have gray values ​​that significantly deviate from the normal levels of their surrounding pixels. If not filtered out, they will directly participate in subsequent difference calculations, leading to localized incorrect compensation amounts in the difference image, ultimately introducing granular or patchy artifacts into the compensated image. Step S210 removes these anomalies using median filtering: a filtering window is defined centered on each pixel, and the median of the gray values ​​of all pixels within the window is used to replace the original gray value of the center pixel. Median filtering, as a nonlinear spatial filtering method, addresses isolated outliers by addressing the following mechanism: the gray values ​​of outliers (such as extremely bright or dark pixels caused by defects or noise) are necessarily located at the two ends of the sequence after being sorted within the window. When taking the median, these extreme values ​​are naturally excluded and will not be selected as output values. Meanwhile, the gray-scale changes caused by illumination trends are gradual and regional, and the gray-scale values ​​of most pixels within the window are relatively concentrated. The median can accurately reflect the true macroscopic brightness level of the region, thereby obtaining a purer and smoother local gray-scale reference image, providing a more accurate data basis for subsequent difference calculations.

[0038] Furthermore, in industrial visual appearance defect detection, in addition to image markers, the preset area often contains physical defects such as scratches, dents, and stains on the product surface. These defects themselves have inherent gray-level abrupt change characteristics. If gray-level compensation is performed directly, the mean aggregation operation in the gridded downsampling process will incorporate the gray-level abrupt change of the defect into the macroscopic illumination trend, causing the compensation amount to neutralize the original gray-level characteristics of the defect. This results in a decrease in the contrast of the defect after compensation, which the subsequent AOI threshold segmentation or defect recognition algorithm cannot capture, leading to missed detection. Therefore, steps S220 and S230 distinguish between illumination drift and physical defects through differential operation and threshold determination. Since median filtering has almost no impact on the macroscopic illumination trend while filtering out noise and defect signals, the original downsampled image can be regarded as the superposition of illumination trend, defect signal, and noise, while the smoothed downsampled image is approximately a pure illumination trend. After calculating the absolute value of the difference between the two pixel by pixel, the difference in the normal illumination area is close to zero, while the defect area, because the gray-level value is smoothed out in the median filtering, shows a prominent non-zero peak on the difference map. Step S230: Based on this, the difference map is binarized by a preset defect judgment threshold. The defect area is marked so that it can be exempted in the subsequent compensation process, so as to avoid the compensation operation weakening or masking the grayscale features of the real defect.

[0039] In some specific embodiments of the present invention, the side length of the filtering window can be preset to an odd number of pixels, typically 3 by default. Taking a 3×3 window as an example, for the center pixel at coordinates (i, j) in the downsampled image, the gray values ​​of the surrounding 9 pixels are extracted. These 9 gray values ​​are sorted from smallest to largest, and the gray value at the 5th position in the sorted sequence (i.e., the median) is taken as the filtered gray value of the center pixel. For pixels located at the boundary of the downsampled image, the portion of the window extending beyond the image boundary is not included in the sorting and median calculation; only the effective pixels within the overlapping area of ​​the window and the image are used to calculate the median. The window side length can be adjusted according to the actual noise level. The larger the side length, the stronger the denoising capability, but the more real gray-level gradient details in the image are lost. It is generally recommended to use an odd value between 3 and 7.

[0040] Reference Figure 2 In addition, after step S300 in the above-described embodiments, the following steps are included, but are not limited to: Step S310: Based on the binary mask image, the difference image is corrected. The correction includes: setting the grayscale difference of the pixels corresponding to the defect area in the difference image to zero, while keeping the grayscale difference of the normally illuminated area unchanged. Step S320: Perform a morphological closing operation on the difference image, including: first perform a morphological dilation operation on the difference image, and then perform a morphological erosion operation on the dilated difference image.

[0041] It should be noted that steps S310 and S320 are used for differential processing and spatial continuity optimization of the compensation amount to detect defects. Step S310, based on the binary mask image generated in the above-mentioned embodiments, performs partition correction on the defect area and the normal illumination area in the difference image: the gray-level difference corresponding to the defect area is forcibly set to zero, so that it is not subject to any gray-level adjustment in the subsequent compensation process, thereby avoiding the neutralization or weakening of the original gray-level abrupt change characteristics of the physical defects on the product surface during the compensation operation, ensuring that the defects can still be accurately captured by the subsequent AOI threshold segmentation or defect recognition algorithm after compensation; at the same time, the original gray-level difference of the normal illumination area is retained unchanged, so that it continues to participate in the subsequent amplitude normalization, morphological correction and upsampling superposition, to complete the correction of uneven illumination, thereby exempting the defect area from subsequent compensation.

[0042] Furthermore, step S320 performs a morphological closing operation on the masked difference image. Through a cascaded process of dilation followed by erosion, it fills in any tiny missing holes that may occur in the compensation area due to the difference being set to zero, removes isolated noise points caused by threshold determination, and simultaneously maintains the main shape and boundary position of the compensation area basically unchanged. This makes the spatial distribution of the compensation amount in the difference image more continuous and smooth, avoiding granular or jagged compensation traces after upsampling and stacking, and improving the naturalness and visual quality of the final compensated image.

[0043] Specifically, let the structuring element be S, then the closing operation is defined as: ...Form 9; in As a structural element, it is usually adopted The matrix can be set to all 1s or adjusted to 5×5 as needed to control the degree of filling missing areas and noise removal. The closing operator indicates that expansion occurs before erosion. This is the expansion operator; This is the erosion operator.

[0044] The expansion operation is defined as: ...Form 10; The specific steps of the expansion operation are as follows: The first step is to set the structuring element. The second step is to create a blank image of the same size as the original difference image to store the dilated result. The third step is to iterate through each pixel in the original difference image. For the currently iterated pixel, align the center of the structuring element with that pixel's position. At this point, the structuring element covers a local neighborhood region centered on that pixel. The fourth step is to read the grayscale value of each pixel within the neighborhood region covered by the structuring element and find the maximum value among these grayscale values. The fifth step is to use the found maximum value as the grayscale value of the corresponding pixel in the dilated image, replacing the original grayscale value of the current center pixel. The sixth step is to repeat steps three through five for all pixels in the difference image until the entire image has been traversed, resulting in the dilated difference image.

[0045] The erosion operation is defined as follows: ······Mode ; Formula 10, Formula In the diagram, m and n are the row and column coordinate offsets of the structuring element, respectively. By traversing all m and n, the maximum and minimum values ​​of the image window can be obtained to complete the dilation / erosion.

[0046] The specific steps of the erosion operation are as follows: First, use the dilated difference image as the processing object. Use the same structuring element as the dilation operation. Second, create another blank image with the same size as the dilated difference image to store the erosion result. Third, traverse each pixel in the dilated difference image. For the currently traversed pixel, align the center of the structuring element with the pixel position. The structuring element covers a local neighborhood region centered on that pixel. Fourth, within the neighborhood region covered by the structuring element, read the grayscale value of each pixel one by one and find the minimum value among these grayscale values. Fifth, use the found minimum value as the grayscale value of the corresponding pixel in the eroded image and replace the grayscale value of the current center pixel. Sixth, repeat steps three to five for all pixels in the dilated difference image until the traversal is complete, obtaining the eroded difference image, which is the difference image corrected by morphological closing operation.

[0047] Reference Figure 2 In addition, after step S300 in the above-described embodiments, the following steps are included, but are not limited to: Step S320: Determine the linear mapping relationship based on the minimum value of the grayscale difference, the maximum value of the grayscale difference, and the lower and upper limits of the preset compensation amplitude range; Step S330: Based on the linear mapping relationship, map each gray-level difference in the difference image and normalize each gray-level difference to the compensation range.

[0048] It should be noted that in some embodiments of the present invention, due to extreme differences in lighting conditions in actual production lines, the grayscale shift in certain local areas of the difference image generated in step S300 may be very large. If the original difference image is used directly for compensation, the brightness of the corresponding area may be excessively enhanced or excessively darkened, resulting in new image artifacts, and the compensation effect may also appear spatially uneven. Therefore, the present invention first statistically analyzes the minimum and maximum values ​​of all grayscale differences in the difference image to determine its original dynamic range; then, based on the lower and upper limits of the preset compensation amplitude range, a linear mapping relationship is constructed; finally, each grayscale difference is linearly mapped to normalize its value to within the compensation amplitude range. This process uniformly limits the fluctuation range of the compensation amount to a controllable range, effectively preventing overcompensation caused by excessive single-point compensation intensity; at the same time, the nature of linear mapping preserves the relative proportional relationship of compensation needs between different areas, ensuring that areas with severe lighting shifts still receive stronger compensation than areas with slight shifts, thereby maintaining the naturalness and spatial consistency of the compensation effect while avoiding over-enhancement.

[0049] Specifically, the compensation process is as follows: First, the minimum and maximum values ​​of the original difference are calculated: ······Mode ; in Take all The minimum value among the difference pixels Take all The maximum value among the difference pixels, and Together, they characterize the dynamic range of the difference image.

[0050] The stretched difference image is as follows: ······Mode ; in The lower limit of the target range can be configured. The default value is -50, and the value range is generally [-100, 0]. To stretch the upper limit of the target interval, a configurable parameter is available, with a default value of 50 and a typical value range of [0,100]; this maps the original difference to the [0,1] interval. The width of the target range, which defaults to 100.

[0051] Reference Figure 2 Furthermore, following step S400 in the above-described embodiments, the following steps are also included, but are not limited to: In step S410, if the grayscale value of the superimposed pixel exceeds the upper limit of the preset grayscale range, the grayscale value of the superimposed pixel is corrected to the upper limit; if the grayscale value of the superimposed pixel is lower than the lower limit of the preset grayscale range, the grayscale value of the superimposed pixel is corrected to the lower limit.

[0052] It should be noted that step S420 constrains the compensation result within a preset effective grayscale range by performing amplitude limiting processing on the superimposed pixel values. If the superimposed pixel values ​​exceed the upper or lower limit of this range, they are corrected to the corresponding upper or lower limit values ​​to prevent pixel value overflow due to excessive compensation, thus ensuring the validity and visual naturalness of the output image data.

[0053] Specifically, the limiting function is defined as: ······Mode ; Mode middle This is a limiting function that restricts the superposition result to the valid range of grayscale values ​​to prevent overflow; The pixel values ​​of the ROI image after compensation.

[0054] Reference Figure 2 In addition, prior to step S200 in the above-described embodiments, the following steps are included, but are not limited to: Step S201: Obtain the deviation between the average gray value of all pixels within the preset area and the preset target gray value; In step S202, determine whether the deviation is less than a preset threshold. If yes, output the image to be compensated directly. If no, continue to perform the steps of performing gridded downsampling processing on the preset area, generating a difference image, performing spatial interpolation upsampling processing, and performing grayscale compensation.

[0055] It should be noted that steps S201 to S202 are the grayscale anomaly judgment mechanism provided by the embodiments of the present invention, used to quickly assess whether the current image needs compensation processing before actually executing the compensation process. In the actual operation of industrial production lines, the image grayscale of most qualified products may already be close to the preset target grayscale reference. If the complete processing flow of gridded downsampling, difference calculation, upsampling and superposition compensation is fully executed for all images, it will cause unnecessary consumption of computing resources and reduce the overall detection efficiency. Therefore, step S201 first calculates the average grayscale value of all pixels in the preset area and obtains the deviation between the average grayscale value and the preset target grayscale value in the above-mentioned embodiments. Step S202 compares the deviation with a preset threshold: if the deviation is less than the preset threshold, it indicates that the grayscale level of the current image is within an acceptable range, and no compensation is required; the image to be compensated is directly output. If the deviation is greater than or equal to the preset threshold, it is determined that the current image has grayscale drift that needs to be corrected, and the subsequent compensation processing steps are continued. This pre-judgment mechanism enables rapid image filtering, triggering the full compensation process only for images that genuinely exhibit grayscale anomalies, thereby significantly improving overall processing efficiency and system throughput while ensuring compensation quality.

[0056] Specifically, calculate the average gray value of the original image in the ROI region: ······Mode ; Mode middle, This represents the total number of pixels in the ROI region. To sum the values ​​of all pixels within the ROI region, The average grayscale value of the ROI region, with a range of [value range missing]. .

[0057] Calculate the deviation between the average gray level and the target gray level reference T: ······Mode ; Mode middle, The target grayscale reference value is a configurable parameter with a value range of [value range missing]. It is usually pre-calibrated based on the grayscale values ​​of standard qualified products, with a default value of 128. It is an absolute value function, ensuring that the deviation is non-negative. This represents the global grayscale deviation, reflecting the degree of difference between the overall grayscale of the ROI region and the target baseline. If ,in If a preset anomaly threshold is set, the current image is determined to meet the grayscale requirements, and the original image I(x, y) is directly output without performing subsequent compensation steps. The full compensation process is only triggered at that time.

[0058] Reference Figure 2 In addition, prior to step S200 in the above-described embodiments, the following steps are included, but are not limited to: Step S203: Determine whether the grid side length is zero, negative, or greater than the shortest side of the preset area. If so, set the grid side length to 1 pixel.

[0059] It should be noted that this embodiment of the invention also provides a grid parameter validity verification mechanism, used to check the rationality and automatically correct the preset grid side length before actually performing gridded downsampling processing. In actual deployment in industrial sites, operators may input invalid grid side length values ​​due to misoperation or parameter configuration errors, such as setting the grid side length to zero, a negative number, or a side length value greater than the smaller of the preset area width and height. When the grid side length is zero or negative, the grid is physically meaningless and cannot effectively divide the image; when the grid side length is greater than the shortest side of the preset area, the resolution of the downsampled image will be less than 1×1 pixels, which cannot form a valid image data structure, causing subsequent filtering, interpolation calculation, and other operations to fail or even triggering program abnormalities. Step S203 verifies the validity of the grid side length before gridded downsampling processing, automatically identifies the above-mentioned abnormal parameter inputs, and forcibly sets the grid side length to 1 pixel when an invalid parameter is detected. Even if the grid contains only a single pixel, the gridded downsampling process degenerates into a pixel-by-pixel copy of the original preset area without spatial dimensionality reduction. The subsequent compensation process can still be executed normally without downsampling.

[0060] Specifically, the judgment logic of the above-mentioned embodiments is as follows: ······Mode ; Mode middle, Take the width of the ROI region and height The smaller value in. When The grid size has no physical meaning and is reset to 1; when If the grid size is larger than the shortest side of the ROI image, the downsampled image resolution will be less than 1×1, making it unprocessable. Therefore, it is reset to 1. Resetting to 1 means that each grid contains only one pixel, i.e., no downsampling is performed, and processing is done pixel-by-pixel at the original resolution.

[0061] According to an embodiment of a second aspect of the present invention, an image marker grayscale compensation system is provided, the system specifically comprising: The image acquisition module is used to acquire the image to be compensated, which includes image markers; The downsampling module is used to perform gridded downsampling processing on a preset region containing image markers in the image to be compensated based on a preset grid side length, so that the image markers are divided into multiple grids; wherein, the grid side length is less than the smaller of the width and height of the image markers in the image plane; the downsampling image includes multiple downsampling pixels corresponding to the grids, and the gray value of each downsampling pixel is the average gray value of all pixels in the corresponding grid; The difference image generation module is used to obtain the gray value difference between the gray value of each downsampled pixel and the preset target gray value, and to map and arrange all the gray value differences one by one according to the spatial coordinates of the corresponding downsampled pixels to generate a difference image. The upsampling module is used to perform spatial interpolation upsampling processing on the difference image to obtain a compensation image; wherein the resolution of the compensation image is the same as that of the image to be compensated. The grayscale compensation module is used to superimpose the compensation image pixel by pixel onto the corresponding pixels of the preset area to perform grayscale compensation on the preset area.

[0062] It should be noted that the grayscale compensation system for image marking provided in this embodiment works collaboratively across its modules. Through techniques such as grid size constraints, mean aggregation to remove marking details, low-resolution compensation calculations, and upsampling overlay, it achieves the technical effect of completely preserving the image marking morphology while eliminating local grayscale drift. This system can be deployed in the image processing unit, smart camera, embedded vision sensor, or host computer inspection software of an industrial vision inspection system. Furthermore, it may include at least one of the following modules: a local neighborhood smoothing module, a morphological correction module, an amplitude normalization mapping module, a grayscale anomaly detection module, and a grid side length verification module, to implement the corresponding optional steps in the aforementioned method embodiments, further improving compensation accuracy, processing efficiency, and engineering robustness.

[0063] like Figure 3 As shown, Figure 3 This is a structural diagram of an electronic device provided in one embodiment of the present invention. The present invention also provides an electronic device, comprising: The processor 801 can be implemented using a general-purpose central processing unit (CPU), microprocessor, application specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application. The memory 802 can be implemented as a read-only memory (ROM), static storage device, dynamic storage device, or random access memory (RAM). The memory 802 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 802 and is called and executed by the processor 801 to execute the grayscale compensation method for image marking in the embodiments of this application. The 803 input / output interface is used to implement information input and output. The communication interface 804 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.). Bus 805 transmits information between various components of the device (e.g., processor 801, memory 802, input / output interface 803, and communication interface 804); The processor 801, memory 802, input / output interface 803, and communication interface 804 are connected to each other within the device via bus 805.

[0064] It should be noted that the electronic device that performs the grayscale compensation method for image marking in the embodiments of the present invention, as well as any other electronic device that can be used to perform the method, are not specifically limited in the present invention.

[0065] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof. The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separate, and may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.

[0066] It will be understood by those skilled in the art that all or some of the steps and systems in the methods disclosed above can be implemented as software, firmware, hardware, and suitable combinations thereof. Some or all of the physical components can be implemented as software executed by a processor, such as a central processing unit, digital signal processor, or microprocessor, or as hardware, or as an integrated circuit, such as an application-specific integrated circuit. Such software can be distributed on a computer-readable medium, which can include computer storage media (or non-transitory media) and communication media (or transient media). As is known to those skilled in the art, the term computer storage media includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules, or other data). Computer storage media includes, but is not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disc (DVD) or other optical disc storage, magnetic cartridges, magnetic tape, disk storage or other magnetic storage devices, or any other medium that can be used to store desired information and is accessible to a computer. Furthermore, as is known to those skilled in the art, communication media typically include computer-readable instructions, data structures, program modules, or other data in modulated data signals such as carrier waves or other transmission mechanisms, and may include any information delivery medium.

[0067] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings. However, the present invention is not limited to the above embodiments. Within the scope of knowledge possessed by those skilled in the art, various changes can be made without departing from the spirit of the present invention.

Claims

1. A grayscale compensation method for image markers, characterized in that, include: Obtain the image to be compensated, which includes image markers; Based on a preset grid side length, a gridded downsampling process is performed on a preset region containing the image marker in the image to be compensated to obtain a downsampled image; wherein, the grid side length is less than the smaller of the width and height of the image marker in the image plane, so that the image marker is segmented into multiple grids; the downsampled image includes multiple downsampled pixels corresponding to the grids, and the gray value of each downsampled pixel is the average gray value of all pixels in the corresponding grid; Obtain the grayscale difference between the grayscale value of each downsampled pixel and the preset target grayscale value, and arrange all the grayscale differences according to the spatial coordinates of the corresponding downsampled pixels to generate a difference image; Spatial interpolation upsampling is performed on the difference image to obtain a compensation image; wherein the resolution of the compensation image is the same as that of the image to be compensated. The compensation image is superimposed pixel by pixel onto the pixels corresponding to the preset region to perform grayscale compensation on the preset region.

2. The grayscale compensation method for image marking according to claim 1, characterized in that, After the step of performing gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image, the method further includes: The downsampled image is subjected to local neighborhood smoothing to obtain a smoothed downsampled image; The absolute value of the difference between the downsampled image and the smoothed downsampled image is calculated pixel by pixel to obtain the difference map; Pixels with pixel values ​​greater than or equal to a preset defect determination threshold in the difference image are marked as defect areas, and pixel locations with pixel values ​​lower than the preset defect determination threshold are marked as normal lighting areas. A binary mask image is generated based on the defect areas and the normal lighting areas.

3. The grayscale compensation method for image marking according to claim 2, characterized in that, Following the step of generating the difference image, the method further includes: Based on the binary mask image, the difference image is corrected. The correction includes setting the grayscale difference of the pixels corresponding to the defect area in the difference image to zero, while keeping the grayscale difference of the normally illuminated area unchanged. Performing a morphological closing operation on the difference image includes: first performing a morphological dilation operation on the difference image, and then performing a morphological erosion operation on the dilated difference image.

4. The grayscale compensation method for image markers according to any one of claims 1 to 3, characterized in that, Following the step of generating the difference image, the method further includes: A linear mapping relationship is determined based on the minimum value of the grayscale difference, the maximum value of the grayscale difference, and the lower and upper limits of the preset compensation range. Based on the linear mapping relationship, each gray-level difference in the difference image is mapped, and each gray-level difference is normalized to the compensation range.

5. The grayscale compensation method for image markers according to any one of claims 1 to 3, characterized in that, After the step of superimposing the compensation image pixel by pixel onto the pixels corresponding to the preset region to perform grayscale compensation on the preset region, the method further includes: If the grayscale value of the superimposed pixel exceeds the upper limit of the preset grayscale range, the grayscale value of the superimposed pixel is corrected to the upper limit; if the grayscale value of the superimposed pixel is lower than the lower limit of the preset grayscale range, the grayscale value of the superimposed pixel is corrected to the lower limit.

6. The grayscale compensation method for image markers according to any one of claims 1 to 3, characterized in that, Before the step of performing gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image, the method further includes: Obtain the deviation between the average grayscale value of all pixels within the preset area and the preset target grayscale value; If the deviation is less than a preset threshold, the image to be compensated is directly output; otherwise, the steps of performing gridded downsampling processing on the preset region, generating a difference image, performing spatial interpolation upsampling processing, and performing grayscale compensation are continued.

7. The grayscale compensation method for image markers according to any one of claims 1 to 3, characterized in that, Before the step of performing gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image, the method further includes: Determine whether the grid side length is zero, negative, or greater than the shortest side of the preset area. If so, set the grid side length to 1 pixel.

8. A grayscale compensation system for image marking, characterized in that, include: The image acquisition module is used to acquire the image to be compensated, which includes image markers; A downsampling module is used to perform gridded downsampling processing on a preset region containing the image marker in the image to be compensated based on a preset grid side length to obtain a downsampled image; wherein, the grid side length is less than the smaller of the width and height of the image marker in the image plane, so that the image marker is segmented into multiple grids; the downsampled image includes multiple downsampled pixels corresponding to the grids, and the grayscale value of each downsampled pixel is the average grayscale value of all pixels in the corresponding grid; The difference image generation module is used to obtain the gray value difference between the gray value of each downsampled pixel and the preset target gray value, and to map and arrange all the gray value differences one by one according to the spatial coordinates of the corresponding downsampled pixels to generate a difference image. An upsampling module is used to perform spatial interpolation upsampling processing on the difference image to obtain a compensation image; wherein the resolution of the compensation image is the same as that of the image to be compensated; The grayscale compensation module is used to superimpose the compensation amount image pixel by pixel onto the pixels corresponding to the preset area to perform grayscale compensation on the preset area.

9. An electronic device, characterized in that, The device includes a memory and a processor, the memory being used to store at least one program, and the processor being used to load the at least one program to perform the grayscale compensation method for image marking according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions for causing a computer to perform the grayscale compensation method for image markers as described in any one of claims 1 to 7.