Method and system for self-adaptive anonymization of specified image area
By using an adaptive anonymization method that dynamically calculates mosaic size and region, the problems of visual effect and dataset balance in image anonymization are solved, thereby improving the accuracy of anonymized regions and the recognition accuracy of AI models.
Patent Information
- Application Number
- CN202511064184.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-31
- Publication Date
- 2025-11-25
Smart Images

Figure CN121010848A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of computer vision, image processing, and data management, and in particular to a method and system for adaptive anonymization of specified image regions. Background Technology
[0002] In the fields of image processing and artificial intelligence, especially in the field of AI object detection technology, it is necessary to label some feature regions contained in the image with rectangular boxes and polygonal boxes to meet the training of AI models. In addition, it is also necessary to anonymize the target objects in the image that contain sensitive information or require specific processing, such as privacy protection or data quality control in face recognition, vehicle license plate recognition, and road crack defect detection.
[0003] Traditional image anonymization methods typically include simple image blurring, pixelation, or color block filling, which have the following limitations:
[0004] Poor visual effect: Simple mosaic and blurring often make the anonymized area stand out from the surrounding background, reducing the overall aesthetics and usability of the image.
[0005] Inaccurate anonymization: Traditional methods do not have fine-grained control over the anonymization region, which may lead to over-anonymization (covering non-sensitive areas) or under-anonymization (failing to fully cover sensitive information).
[0006] Imbalanced class distribution in datasets: In machine learning training, datasets often suffer from an imbalance in the number of classes, with a minority of classes being too few and a majority of classes being too many, which affects the training effect of the model.
[0007] Pixel interference: Using specific mosaic pixels on areas that need to be anonymized will introduce pixel pollution sources during the process, interfering with the extraction of image features by the convolutional neural network (CNN), causing pixel interference and resulting in "distorted" final recognition results of the AI model.
[0008] Therefore, there is an urgent need in this field for an image anonymization method that can provide a smarter, more efficient, and visually better approach that can also address the problem of class imbalance in datasets.
[0009] The information disclosed in this background section is intended only to enhance the understanding of the overall background of the invention and should not be construed as an admission or in any way implying that the information constitutes prior art known to those skilled in the art. Summary of the Invention
[0010] The purpose of this invention is to provide an image anonymization method that is more intelligent, more efficient, has better visual effects, and can solve the problem of class imbalance in datasets.
[0011] To achieve the above objectives, the present invention provides the following solution:
[0012] A method for adaptive anonymization of a specified image region includes:
[0013] Step 1: Mark the categories that need to be labeled in the image with rectangles or polygons, and export the labeling results as XML or TXT files; the image and labeling file names should be consistent and correspond one-to-one; labeling software (such as X-AnyLabeling) can be used.
[0014] Step 2: Use the traversal functions in the Python library to traverse the XML file, and use the XML document parsing function to parse the tag file to obtain the tag category name C. name_i Marker box coordinate information B i =[x min x max y min y max and the Count of each category c Generate a unique identifier (ID) for all the bounding boxes that are traversed. i Identifiers are indexed along with images and XML files;
[0015] Step 3: Determine the categories and quantities that need to be anonymized;
[0016] Step 4: Determine the required mosaic size, mosaic area, and anti-overlap criteria for each anonymization step, including:
[0017] Calculate the pixel dimensions of the dynamic mosaic in the image that needs to be anonymized, specifically:
[0018] Confirm the mosaic effect;
[0019] Dynamically calculate the anonymization region;
[0020] Step 5: Based on the results of Steps 3 and 4, perform anonymization operations on the regions that need to be anonymized. Specifically:
[0021] Based on the categories, quantities, and XML information extracted in step three, identify the regions that need to be anonymized;
[0022] Based on the basic pixel size and effect of the mosaic obtained in step four, mark the mosaic in the areas that need to be anonymized.
[0023] Synchronously delete instance-related information of the mosaic region in the XML file;
[0024] Step 6: Output the processed image and XML annotation file.
[0025] Optionally, the range of the balancing float percentage in step three is [0.05, 0.2].
[0026] Optionally, in step three, the categories and quantities that need to be anonymized are determined in the following two ways:
[0027] The first method: Directly specify the categories and quantities that need to be anonymized;
[0028] The second method: Determine the categories and number of categories that need to be anonymized based on the balance of category averages, specifically:
[0029] Calculate the average number of instances of all categories traversed in step two:
[0030]
[0031] In the formula, N c C represents the total number of categories. k The number of instances in the k-th category;
[0032] Determine the threshold range for the amount of anonymization needed: R∈(R min R max ),in:
[0033] R min =C avg ×(1-p) (2)
[0034] R max =C avg ×(1+p) (3)
[0035] In equations (2) and (3), p represents the balance float percentage;
[0036] For the k-th category, the logic for calculating whether it needs anonymization and the number of anonymizations is as follows:
[0037] When C k ≤R min In this case, the category does not need to be anonymized;
[0038] When R min ≤C k ≤R max At this point, the category needs to be anonymized, and the number of items that need to be anonymized is:
[0039]
[0040] When C k >R max At this point, the category needs to be anonymized, and the number of items that need to be anonymized is:
[0041]
[0042] Record the category name C that needs to be anonymized. name_k and quantity N m,k And construct a random sampling module, specifically:
[0043] Collect all IDs corresponding to the j-th class that need to be anonymized. i This forms a complete candidate set, which serves as the input sample pool.
[0044]
[0045] In equation (6), U j For the candidate set;
[0046] The RandomSample() function is used to perform random sampling without replacement from U j Randomly select N m,k An example.
[0047] Optionally, the pixel dimensions of the dynamic mosaic in the calculated image that needs to be anonymized include:
[0048] Calculate the pixel size B of the mosaic in each region of the image. i
[0049]
[0050] In equation (7), W o The original width of the marker box, in pixels;
[0051] H o The original height of the marker box, in pixels;
[0052] S f This is the scaling factor, used to proportionally convert the size of an object to the size of a mosaic block; typical values are 10, 20, etc.
[0053] B min This is the lower limit for the minimum mosaic size; the typical value range is (3-10). This prevents the mosaic blocks from being too small, which would make the mosaic effect less noticeable and look more like a blurred image than pixelated, and also avoids increasing the computational burden and memory consumption.
[0054] B max This is the upper limit of the minimum mosaic size; the typical value range is (20-30). If it is too large, it will cause excessive aggregation and loss of information on the target object, resulting in a loss of the mosaic's texture and affecting the overall visual acceptability of the image.
[0055] Calculate the size B of the mosaic blocks in a single image. s :
[0056]
[0057] Optionally, the calculation of the mosaic effect includes:
[0058] The image to be processed is resampled twice. Specifically, the entire image is first reduced to B using the cv2.INTER_AREA interpolation function (region interpolation algorithm). s The image size is then adjusted, and the cv2.INTER_NEAREST interpolation function (nearest neighbor interpolation algorithm) is used to enlarge the shrunk image back to its original size, resulting in a mosaic background covering the entire image.
[0059] Extract the image portion corresponding to the region that needs to be expanded and anonymized from the mosaic background;
[0060] The extracted image portion is then blended with the corresponding region of the original image using Gaussian blur and alpha. The alpha blending formula is as follows:
[0061] P f =P m ×M α +P o ×(1-M α (9)
[0062] In equation (9), P f P represents the final pixel value of the mosaic. m For from I m The pixel values of the extracted mosaic region, P o M represents the pixel value of the corresponding region in the original image. α I is the normalized value at the corresponding position in the Gaussian blur alpha mask matrix; m A mosaic background covering the entire image;
[0063] M α The generation process is as follows:
[0064] Initialize a zero matrix: Create an all-zero matrix M with the same size as the target anonymization region (W0×H0). α ′, all pixel values are initialized to 0;
[0065] Fill the center area: M α The value of the central rectangular area is assigned to 1.0, and the size of the central rectangular area is 5×5 pixels;
[0066] Applying Gaussian blur: The cv2.GaussianBlur function from OpenCV is used to apply a Gaussian blur function to a matrix. The formula is as follows:
[0067] M α =G(M α ′,Ks ,σ) (10)
[0068] In equation (10), K s σ is the Gaussian blur kernel size, set to 3; σ is the standard deviation of the Gaussian function, set to 0, indicating that it is automatically calculated by the blur function based on the kernel size.
[0069] Optionally, the dynamically computed anonymization region includes:
[0070] Confirm mosaic pixel size B s Confirm the background color of the mosaic area;
[0071] Based on the coordinate information B in the XML tag file i =[x min x max y min y max Confirm the original region size;
[0072] Whether to expand the mosaic region is determined based on the maximum expansion region coefficient and the intersection-union ratio. Specifically:
[0073] Set the maximum expansion area coefficient P. max P of low-resolution images max Between 5 and 10, the P of high-resolution images max Between 20 and 50.
[0074] The extended region was confirmed using binary search iterative search and IoU calculation, specifically:
[0075] Calculate intermediate extension (E) mid ),
[0076] Generate potential extended regions (R) p ),like Figure 2 As shown, the calculation formula is:
[0077] R p_i =[(x min -E mid ),(y min -E mid ),(x max +E mid ),(y max +E mid (11)
[0078] The IoU value between the extended region and other category-identified blocks in the image is calculated using the following formula:
[0079]
[0080] In equation (12), Rn This refers to the area within the image that serves as a label for other categories.
[0081] Determine if optimization and expansion are needed: When IoU = 0, select the current E. mid Perform expansion and end the traversal; when IoU > 0, it indicates that the expanded area overlaps with the existing category label box areas, and E mid Perform the binary splitting operation until IoU = 0, then stop the traversal.
[0082] A system for adaptive anonymization of a specified image region includes:
[0083] The image tagging module is used to mark the categories that need to be labeled in an image with rectangles or polygons, and export the tagging results as XML or TXT files; the image and tagging file names are consistent and correspond one-to-one;
[0084] The XML file traversal and parsing module is used to traverse XML files using traversal functions from the Python library and parse markup files using XML document parsing functions.
[0085] The anonymization category and quantity determination module is used to determine the categories and quantities that need to be anonymized;
[0086] The mosaic size, mosaic area, and anti-overlap determination module is used to determine the mosaic size, mosaic area, and anti-overlap determination required for each anonymization.
[0087] The anonymization operation execution module is used to perform anonymization operations on regions that need to be anonymized;
[0088] The output module is used to output the processed images and XML annotation files.
[0089] Optionally, the mosaic size, mosaic area, and anti-overlap determination module includes:
[0090] The pixel size calculation unit is used to calculate the pixel size of the dynamic mosaic in the image that needs to be anonymized;
[0091] Mosaic effect confirmation unit, used to confirm the mosaic effect;
[0092] Anonymized region calculation unit, used for dynamically calculating anonymized regions.
[0093] Compared with the prior art, the present invention has the following beneficial effects:
[0094] Rapidly optimize dataset balance: The common problem of class imbalance in existing image datasets leads to poor training performance of AI models and poor recognition ability of a few classes. By statistically analyzing the average number of class instances and the threshold range, we can intelligently identify and selectively anonymize class instances with an excessive number of instances, so that the number of each class tends to be balanced, which significantly improves the quality of the dataset.
[0095] Enhancing the visual effect and naturalness of anonymization: Traditional simple blurring or mosaic processing does not consider the adverse effects on the overall feature values of the image, leading to distorted results when used for machine learning or model training. This invention introduces dynamic mosaic size calculation and adopts advanced alpha blending technology, combined with Gaussian blur mask, to achieve a smooth and natural fusion of the anonymized region with the background. The generated anonymized image is more consistent with the original image and does not destroy the feature values of the original image.
[0096] Improving the accuracy and anti-overlap capability of anonymized regions: Traditional methods do not provide fine-grained control over anonymized regions, which may lead to over-anonymization. This invention adopts a dynamic region expansion strategy based on IoU and determines the optimal expansion range through binary search iteration. This ensures that the anonymized region completely covers the target while not overlapping with other important marker regions in the image, thus achieving highly accurate anonymization. Attached Figure Description
[0097] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0098] Figure 1 This is a schematic flowchart of a method for adaptive anonymization of a specified image region provided in an embodiment of the present invention.
[0099] Figure 2 The calculation formula is used to generate the potential extended region provided by the present invention.
[0100] Figure 3 The figure shows the verification results of an example provided by this invention.
[0101] Figure 4 This is a comparison chart of the training accuracy of the original dataset and the processed model provided by this invention. Detailed Implementation
[0102] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0103] The purpose of this invention is to provide an image anonymization method and system that can provide a more intelligent, efficient, and visually better solution to the problem of class imbalance in datasets.
[0104] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0105] Example 1:
[0106] This embodiment provides a method for adaptive anonymization of specified image regions, which is then used to address the sample imbalance problem in the YOLO model. Specifically:
[0107] This example primarily collects images of grouting results in bridge ducts. By training a YOLO model and deploying the optimal model, real-time detection of the grouting process is achieved.
[0108] During dataset processing and annotation, the main annotations included Inhole (grouting input hole), Ohole (grouting hole), OverF (grouting outlet hole), Vhole (high-level vent hole), and OAB (grouting blockage hole). This dataset contains 4593 images, and the number of instances for each category is shown in Table 1.
[0109] Table 1. Number of Instances in Each Category
[0110] category quantity Inhole 2897 Ohole 11745 OverF 2840 Vhole 2653 OAB 11649
[0111] As can be seen, the number of Ohole and OAB categories far exceeds that of the other categories. When this method was used for the first time, the mosaic color was fixed to RGB (114, 114, 114), and a total of 2204 images were processed. The sample size was balanced as shown in Table 2.
[0112] Table 2
[0113] category quantity Inhole 2897 Ohole 3045 OverF 2840 Vhole 2653 OAB 2986
[0114] The dataset was then fed into the YOLO model for training. After obtaining the optimal model, it was verified that it could not recognize the OAB and Ohole categories. It was considered that when the mosaic was applied, the specified mosaic color (114,114,114) caused the YOLO model to make a mistake when extracting image features. The reason for the error was that using specified mosaic pixels for areas that needed to be anonymized would introduce pixel pollution sources in the process, interfering with the convolutional neural network (CNN) in extracting image features, causing pixel interference, and resulting in the AI model's final recognition effect being "distorted".
[0115] A second improvement was subsequently made, where the mosaic pixel size and background were not specified. Using the mosaic background generation method described in this patent, the dataset was processed, and a second verification was performed, which was successful. The results of the two instance verifications are as follows: Figure 3 As shown.
[0116] Figure 3 In the image, the left image is a verification image after specifying the mosaic background color (it cannot recognize Ohole and OAB), while the right image is a model verification image after fully applying this patented technology, showing that both Ohole and OAB can be recognized.
[0117] In terms of accuracy improvement, Figure 4 This is a comparison chart showing the training accuracy of the original dataset and the model processed using this technique.
[0118] Figure 4 In the image, the left side shows the original dataset with a combined mAP@0.5 of 0.941, while the model processed using this technique has a combined mAP@0.5 of 0.968, significantly improving the model training accuracy.
[0119] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the systems disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the descriptions are relatively simple; relevant parts can be referred to the method section.
[0120] This document uses specific examples to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. Furthermore, those skilled in the art will recognize that, based on the ideas of the present invention, there will be changes in the specific implementation methods and application scope. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. A method for adaptive anonymization of a specified image region, characterized in that, include: Step 1: Mark the categories that need to be labeled in the image with rectangles or polygons, and export the labeling results as an XML or TXT file; The image and the tagged file names are identical and correspond one-to-one; Step 2: Use the traversal functions in the Python library to traverse the XML file, and use the XML document parsing function to parse the tag file to obtain the tag category name C. name_i Marker box coordinate information B i =[x min x max y min y max and the Count of each category c Generate a unique identifier (ID) for all the bounding boxes that are traversed. i Identifiers are indexed along with images and XML files; Step 3: Determine the categories and quantities that need to be anonymized; Step 4: Determine the required mosaic size, mosaic area, and anti-overlap criteria for each anonymization step, including: Calculate the pixel dimensions of the dynamic mosaic in the image that needs to be anonymized; Confirm the mosaic effect; Dynamically calculate the anonymization region; Step 5: Based on the results of Steps 3 and 4, perform anonymization operations on the regions that need to be anonymized. Specifically: Based on the categories, quantities, and XML information extracted in step three, identify the regions that need to be anonymized; Based on the basic pixel size and effect of the mosaic obtained in step four, mark the mosaic in the areas that need to be anonymized. Synchronously delete instance-related information of the mosaic region in the XML file; Step 6: Output the processed image and XML annotation file.
2. The method for adaptive anonymization of a specified image region according to claim 1, characterized in that, In step three, the range of the balancing float percentage is [0.05, 0.2].
3. The method for adaptive anonymization of a specified image region according to claim 1, characterized in that, In step three, determine the categories and quantities that need to be anonymized using the following two methods: The first method: Directly specify the categories and quantities that need to be anonymized; The second method: Determine the categories and number of categories that need to be anonymized based on the balance of category averages, specifically: Calculate the average number of instances of all categories traversed in step two: In the formula, N c C represents the total number of categories. k The number of instances in the k-th category; Determine the threshold range for the amount of anonymization needed: R∈(R min R max ),in: R min =C avg ×(1-p) (2) R max =C avg ×(1+p) (3) In equations (2) and (3), p represents the balance float percentage; For the k-th category, the logic for calculating whether it needs anonymization and the number of anonymizations is as follows: When C k ≤R min In this case, the category does not need to be anonymized; When R min ≤C k ≤R max At this point, the category needs to be anonymized, and the number of items that need to be anonymized is: When C k >R max At this point, the category needs to be anonymized, and the number of items that need to be anonymized is: Record the category name C that needs to be anonymized. name_k and quantity N m,k And construct a random sampling module, specifically: Collect all IDs corresponding to the j-th class that need to be anonymized. i This forms a complete candidate set, which serves as the input sample pool. In equation (6), U j For the candidate set; The RandomSample() function is used to perform random sampling without replacement from U j Randomly select N m,k An example.
4. The method for adaptive anonymization of a specified image region according to claim 1, characterized in that, The pixel dimensions of the dynamic mosaic in the calculated image that needs to be anonymized include: Calculate the pixel size B of the mosaic in each region of the image. i : In equation (7), W o The original width of the marker box, in pixels; H o The original height of the marker box, in pixels; S f This is the size scaling factor, used to proportionally convert the size of an object to the size of a mosaic block; B min This is the lower limit for the minimum mosaic size; B max This is the upper limit for the minimum mosaic size; Calculate the size B of the mosaic blocks in a single image. s :
5. The method for adaptive anonymization of a specified image region according to claim 1, characterized in that, The calculation of the mosaic effect includes: The image to be processed is resampled twice. Specifically, the entire image is first reduced to B using the cv2.INTER_AREA interpolation function. s The size is then adjusted, and the cv2.INTER_NEAREST interpolation function is used to enlarge the reduced image back to its original size, resulting in a mosaic background covering the entire image area; Extract the image portion corresponding to the region that needs to be expanded and anonymized from the mosaic background; The extracted image portion is then blended with the corresponding region of the original image using Gaussian blur and alpha. The alpha blending formula is as follows: P f =P m ×M α +P o ×(1-M α ) (9) In equation (9), P f P represents the final pixel value of the mosaic. m For from I m The pixel values of the extracted mosaic region, P o M represents the pixel value of the corresponding region in the original image. α This is the normalized value at the corresponding position in the Gaussian blur alpha mask matrix; M α The generation process is as follows: Initialize a zero matrix: Create an all-zero matrix M with the same size as the target anonymization region (W0×H0). α ′, all pixel values are initialized to 0; Fill the center area: M α The value of the central rectangular area is assigned to 1.0, and the size of the central rectangular area is 5×5 pixels; Applying Gaussian blur: The cv2.GaussianBlur function from OpenCV is used to apply a Gaussian blur function to a matrix. The formula is as follows: M α =G(M α ′,K s ,s) (10) In equation (10), K s σ is the Gaussian blur kernel size, set to 3; σ is the standard deviation of the Gaussian function, set to 0, indicating that it is automatically calculated by the blur function based on the kernel size.
6. The method for adaptive anonymization of a specified image region according to claim 1, characterized in that, The computational anonymization region includes: Confirm mosaic pixel size B s Confirm the background color of the mosaic area; Based on the coordinate information B in the XML tag file i =[x min x max y min y max Confirm the original region size; Whether to expand the mosaic region is determined based on the maximum expansion region coefficient and the intersection-union ratio. Specifically: Set the maximum expansion area coefficient P. max P of low-resolution images max Between 5 and 10, the P of high-resolution images max Between 20 and 50; The extended region was confirmed using binary search iterative search and IoU calculation, specifically: Calculate intermediate extension (E) mid ), Generate potential extended regions (R) p The calculation formula is: R p_i =[(x min -E mid ),(y min -E mid ),(x max +E mid ),(y max +E mid )] (11) The IoU value between the extended region and other category-identified blocks in the image is calculated using the following formula: In equation (12), R n This refers to the area within the image that serves as a label for other categories. Determine if optimization and expansion are needed: When IoU = 0, select the current E. mid Perform expansion and end the traversal; when IoU > 0, it indicates that the expanded area overlaps with the existing category label box areas, and E mid Perform the binary splitting operation until IoU = 0, then stop the traversal.
7. A system for adaptive anonymization of a specified image region according to any one of claims 1-6, characterized in that, include: The image tagging module is used to mark the categories that need to be tagged in an image with rectangles or polygons, and export the tagging results as XML or TXT files; The image and the tagged file names are identical and correspond one-to-one; The XML file traversal and parsing module is used to traverse XML files using traversal functions from the Python library and parse markup files using XML document parsing functions. The anonymization category and quantity determination module is used to determine the categories and quantities that need to be anonymized; The mosaic size, mosaic area, and anti-overlap determination module is used to determine the mosaic size, mosaic area, and anti-overlap determination required for each anonymization. The anonymization operation execution module is used to perform anonymization operations on regions that need to be anonymized; The output module is used to output the processed images and XML annotation files.
8. The system according to claim 7, characterized in that, The mosaic size, mosaic area, and anti-overlap determination module includes: The pixel size calculation unit is used to calculate the pixel size of the dynamic mosaic in the image that needs to be anonymized; Mosaic effect confirmation unit, used to confirm the mosaic effect; Anonymized region calculation unit, used for dynamically calculating anonymized regions.