Method for generating damaged two-dimensional code image
By preprocessing and style transfer processing of QR code images and deeply fusing with the background image, the problem of abnormal color defects in QR codes in harsh environments is solved. The generated damaged QR code images are closer to the actual scene, improving recognition performance and image quality.
Patent Information
- Application Number
- CN202411968209.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-04-25
AI Technical Summary
The existing QR code generation methods are difficult to simulate and solve the abnormal QR code color defects caused by harsh environmental factors, which affects the recognition performance.
By preprocessing the ideal QR code image, it is converted into an image composed of regular arrayed dots, and affine transformation is performed. Then, the preprocessed QR code image and the background image are deeply fused using style transfer technology to generate damaged QR code image.
The generated damaged QR code images can be closer to the actual scene's expression, effectively overcome the problem of rigid fusion image effects in traditional methods, improve the recognition performance of QR codes, and provide important technical support for image quality evaluation and testing.
Smart Images

Figure CN119886183A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of barcode generation, and in particular to a method for generating a damaged two-dimensional code image. Background Art
[0002] With the widespread application of QR code technology, its role in areas such as product identification, payment systems, logistics tracking and information sharing has become increasingly important. However, in actual use, QR codes often exhibit color abnormalities due to complex environmental factors, such as low contrast between the QR code and the background or poor recognition, which seriously affects its recognition performance. This problem is particularly prominent in real shooting or printing scenes, and is often caused by the QR code being embedded in a complex background or the color design failing to consider visual coordination with the background. Most of the existing QR code generation methods are based on ideal conditions and lack simulation and research on common defects in actual environments, which poses challenges to QR code quality detection and testing. Summary of the invention
[0003] The present invention provides a method for generating a damaged two-dimensional code image, which is used to solve the defect that the prior art cannot generate damaged two-dimensional codes caused by harsh environmental factors, and provides strong support for the simulation and research work of common defects of two-dimensional codes in actual environments.
[0004] The present invention provides a method for generating a damaged two-dimensional code image, comprising:
[0005] Get the ideal QR code image and background image;
[0006] Preprocessing the ideal two-dimensional code image;
[0007] Perform style transfer processing on the preprocessed QR code image;
[0008] Perform deep background fusion of the style-transferred QR code image and the background image;
[0009] Outputs a damaged QR code image.
[0010] According to a method for generating a damaged two-dimensional code image provided by the present invention, the preprocessing of the ideal two-dimensional code image comprises:
[0011] Convert an ideal two-dimensional code image into a two-dimensional code image composed of regularly arranged dots;
[0012] Perform affine transformation on the dot QR code image.
[0013] According to a method for generating a damaged two-dimensional code image provided by the present invention, the method of converting an ideal two-dimensional code image into a two-dimensional code image composed of regularly arranged dots comprises:
[0014] Convert the ideal QR code image into a grayscale image;
[0015] Set the dot parameters of the dot QR code image and the preset threshold of the grayscale image;
[0016] If the grayscale value of a pixel on the grayscale image is lower than a preset threshold, a dot is drawn at the position corresponding to the pixel;
[0017] The drawn dots generate a dot QR code image according to the dot parameters.
[0018] According to a method for generating a damaged two-dimensional code image provided by the present invention, the dot parameters include the size and density of the dots.
[0019] According to a method for generating a damaged two-dimensional code image provided by the present invention, the method of performing style transfer processing on the pre-processed two-dimensional code image includes:
[0020] The preprocessed QR code image is used as the first original image and input into the convolutional neural network;
[0021] A first white noise image is randomly generated and input into the convolutional neural network;
[0022] Performing feature extraction on the first original image and the first white noise image in the convolutional neural network respectively, obtaining feature representations of the first original image and the first white noise image in the convolutional neural network, and calculating content loss in the convolutional neural network according to the feature representations of the two;
[0023] Input the background image into the convolutional neural network and perform feature extraction to obtain feature representation of the background image in the convolutional neural network, and calculate the style loss in the convolutional neural network according to the feature representations of the background image and the first white noise image in the convolutional neural network;
[0024] The total loss is obtained by weighted summing of content loss and style loss;
[0025] The total loss and the first white noise image are gradient calculated to continuously optimize the first white noise image until the set number of iterations is reached, and finally the first generated image is formed.
[0026] According to a method for generating a damaged two-dimensional code image provided by the present invention, the content loss is calculated using the following formula (1):
[0027]
[0028] in, is the content loss of the first original image and the first generated image at layer l, and are the first original image and the first generated image respectively, and Respectively represent the content features of the first original image and the first generated image at the lth layer and position (i, j).
[0029] According to a method for generating a damaged two-dimensional code image provided by the present invention, the style loss is calculated using the following formula (2):
[0030]
[0031] in, The style loss of each layer of the convolutional neural network for the first generated image and the background image is calculated according to the weight w l The total style loss obtained by weighted summation, and are the style features of the first generated image and the background image at the lth layer and position (i, j) respectively.
[0032] According to a method for generating a damaged two-dimensional code image provided by the present invention, the total loss is calculated by the following formula (3):
[0033]
[0034] Among them, α and β are the weights of content loss and style loss respectively, and the content loss weight α = 1×10 3 , style loss weight β = 1 × 10 -2 .
[0035] According to a method for generating a damaged two-dimensional code image provided by the present invention, the partial derivative of the pixel value of the first white noise image is calculated according to the total loss to obtain the gradient of the first white noise image relative to the total loss, and then the first generated image is generated by iterative optimization. The iterative formula is as shown in formula (4):
[0036]
[0037] in, For the first generated image, is the first white noise image, and η is the learning rate.
[0038] According to a method for generating a damaged two-dimensional code image provided by the present invention, the two-dimensional code image after style transfer is deeply fused with the background image, which includes two stages:
[0039] Stage 1: randomly generate a second white noise image and input it into the convolutional neural network;
[0040] The QR code image after style transfer is used as the second original image and input into the convolutional neural network;
[0041] fusing the second white noise image and the background image to form a preliminary fused image;
[0042] The preliminary fused image is optimized in turn using Poisson gradient loss, content loss and style loss, and regularization loss to form a refined fused image.
[0043] The second stage: optimize the content loss, style loss and regularization loss of the refined fusion image to form the final damaged QR code image.
[0044] The present invention provides a method for generating a damaged QR code image, which pre-processes an ideal QR code to increase the authenticity of its geometric deformation, combines style transfer and deep background fusion technology, and simulates the abnormal color and contrast characteristics of the QR code under a complex background environment. The unique image generation process can not only retain the content information of the original QR code, but also achieve a natural fusion with the style of the background image, effectively overcoming the problem of stiff fusion image effect in traditional methods. The generated damaged QR code is closer to the actual scene expression form, providing important technical support for QR code image quality assessment and testing, and has significant practical application value. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0046] Figure 1 It is a flow chart of a method for generating a damaged two-dimensional code image provided by the present invention.
[0047] Figure 2 It is a schematic diagram of a system for generating a damaged two-dimensional code image provided by the present invention. DETAILED DESCRIPTION
[0048] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0049] Combine the following Figure 1 A method for generating a damaged two-dimensional code image according to the present invention is described, comprising:
[0050] Get the ideal QR code image and background image;
[0051] Preprocessing the ideal two-dimensional code image;
[0052] Perform style transfer processing on the preprocessed QR code image;
[0053] Perform deep background fusion of the style-transferred QR code image and the background image;
[0054] Outputs a damaged QR code image.
[0055] The ideal two-dimensional code image is preprocessed, and the specific steps are as follows:
[0056] The ideal two-dimensional code image is converted into a grayscale image, and the dot parameters used to generate the dot two-dimensional code image are set, including the size, shape, color and spacing of the dots, and the preset threshold of the grayscale image is set at the same time to determine which pixels need to be drawn as dots; during the processing, each pixel on the grayscale image is traversed one by one. If the grayscale value of a pixel is lower than the preset threshold, a dot is drawn at the position corresponding to the pixel. The drawn dots will be adjusted according to the dot parameters, and finally a dot two-dimensional code image composed of multiple regularly arranged dots is generated.
[0057] The dot QR code image is translated, rotated, scaled or tilted by affine transformation to simulate the visual effect of the QR code image in actual applications. The affine transformation is achieved by the following formula:
[0058]
[0059] Among them, (x, y) is the original two-dimensional code coordinates, (x', y') is the transformed coordinates, a, b, c, d are the elements of the affine transformation matrix, t x ,t y are the translation components of the image in the X and Y directions respectively.
[0060] Perform style transfer on the preprocessed QR code image. The specific steps are as follows:
[0061] The preprocessed QR code image is used as the first original image and input into the convolutional neural network;
[0062] A first white noise image is randomly generated and input into the convolutional neural network;
[0063] Feature extraction is performed on the first original image and the first white noise image in the convolutional neural network respectively to obtain feature representations of the first original image and the first white noise image in the convolutional neural network. The content loss in the convolutional neural network is calculated based on the feature representations of the two. The content loss is calculated using formula (1):
[0064]
[0065] in, is the content loss of the first original image and the first generated image at layer l, and are the first original image and the first generated image respectively, and Respectively represent the content features of the first original image and the first generated image at the lth layer and position (i, j).
[0066] The background image is input into the convolutional neural network and features are extracted to obtain the feature representation of the background image in the convolutional neural network. The style loss in the convolutional neural network is calculated based on the feature representation of the background image and the first white noise image in the convolutional neural network. The style feature of the image is represented by the Gram matrix, as shown in formula (5):
[0067]
[0068] in, is the style representation feature of the image in layer l, and are the eigenvalues of the image at the i-th and j-th channels and position k in the l-th layer respectively.
[0069] The style loss between the background image and the first generated image is calculated using the Gram matrix, using formula (2):
[0070]
[0071] in, The style loss of each layer of the convolutional neural network for the first generated image and the background image is calculated according to the weight w l The total style loss obtained by weighted summation, and are the style features of the first generated image and the background image at layer l and position (i, j) respectively.
[0072] The total loss is obtained by weighted summing of content loss and style loss, and is calculated using formula (3):
[0073]
[0074] Among them, α and β are the weights controlling content loss and style loss respectively, and the content loss weight α = 1×10 3 , style loss weight β = 1 × 10 -2 .
[0075] The total loss and the first white noise image are gradient-calculated to continuously optimize the first white noise image until the set number of iterations is reached, and finally the first generated image is formed. Formula (4) is used for iteration:
[0076]
[0077] in, For the first generated image, is the first white noise image, and η is the learning rate.
[0078] The style-transferred QR code image is deeply fused with the background image, which includes two stages:
[0079] Stage 1: randomly generate a second white noise image and input it into the convolutional neural network;
[0080] The QR code image after style transfer is used as the second original image and input into the convolutional neural network;
[0081] The second white noise image and the background image are fused to form a preliminary fused image, using formula (6):
[0082] I B =I Z ⊙M+I T ⊙(1-M) (6)
[0083] Among them, ⊙ represents the element-wise product, I B is the initial fusion image, I z is the second white noise image, I T is the background image, and the mask M controls the initial fusion image I B and background image I T In the preliminary fusion image I B The weights in the image, where M is 1, select the second white noise image I z , the region where M is 0 selects the target image I T .
[0084] The preliminary fused image is optimized in turn by Poisson gradient loss, content loss, style loss, and regularization loss to form a refined fused image. The Poisson gradient loss optimization is performed using formula (7):
[0085]
[0086] in, represents the Laplace gradient operator, I B is the initial fusion image, I S is the second original image, I T is the background image; H and W are the preliminary fusion images IB , the second original image I S 、Background Image I T The width and height of T The gradient of I B The gradients outside the mixed region are exactly the same, so we can minimize I B The gradient of I S and I T The difference between the gradients of B The gradient gradually approaches the second original image I S Gradient
[0087] The content loss and style loss are calculated using formulas (8) and (9):
[0088]
[0089] Where: ⊙ represents the element-wise product, L is the number of convolutional layers, N l is the number of channels in layer L, M l is the number of eigenvalues in each channel, F l [I Z ] and F l [I S ] are the content features of the second generated image and the second original image in the Lth layer, G l [I Z ] and G l [I T ] are the style features of the second generated image and the background image in the Lth layer, i,j and i,k are the indices in the Gram matrix and the activation matrix, α l and β l is a weight used to control the influence of each layer in calculating content loss and style loss.
[0090] Regularization loss includes histogram loss and total variation loss. By introducing histogram loss and total variation loss, our regularization optimization can effectively improve the stability of the fusion area, reduce unnecessary texture distortion, and enhance the visual consistency of the image, so that the final fusion image has higher quality in terms of boundary smoothness and visual effect. The histogram loss is calculated using formula (10):
[0091]
[0092] Among them, γ l is the weight coefficient, which controls the influence of each layer. l (I B ) is the feature representation of the initial fusion image at layer l, R l (I B) is the feature representation of the preliminary fusion image after histogram matching.
[0093] The total variation loss is calculated using formula (11):
[0094]
[0095] Among them, H and W are the height and width of the preliminary fusion image, I m,n Represents the pixel value of the mth row and nth column in the preliminary fused image.
[0096] By simultaneously performing the gradient descent algorithm on the content loss, style loss, and regularization loss, the preliminary fused image is continuously optimized and iterated until the set number of iterations is reached to generate a refined fused image.
[0097] The second stage: the refined fusion image is optimized again in terms of content loss, style loss, and regularization loss to form the final damaged QR code image.
[0098] The content loss and style loss in the second stage are calculated using formulas (12) and (13):
[0099]
[0100] Where: i, j and i, k are the indices in the Gram matrix and feature matrix respectively, I BR is a refined fusion image obtained by further optimizing the preliminary fusion image in the first stage; the content loss is optimized in the second stage. BR and I B similarity; style loss is optimized in the second stage I BR with I T Consistency of style.
[0101] At the same time, the refined fusion image is regularized and optimized, and the histogram loss is calculated using formula (14):
[0102]
[0103] Among them, γ l is the weight coefficient, which controls the influence of each layer. l (I BR ) is the feature representation of the refined fusion image at layer l, R l (I BR ) is the feature representation of the refined fusion image after histogram matching.
[0104] The total variation loss is calculated using formula (15):
[0105]
[0106] Among them, H and W are the height and width of the refined fusion image, I m,n Represents the pixel value of the mth row and nth column in the refined fused image.
[0107] The gradient descent algorithm is applied to the content loss, style loss, and regularization loss to continuously optimize and iterate the refined fusion image until the set number of iterations is reached to generate the final damaged QR code image.
[0108] The present invention also provides a system for generating a damaged two-dimensional code image. The system for generating a damaged two-dimensional code image described below and the method for generating a damaged two-dimensional code image described above can refer to each other.
[0109] like Figure 2 As shown, a system for generating a damaged two-dimensional code image includes:
[0110] Input module: used to input ideal QR code images and background images, supporting standard QR code formats and multiple image formats (such as PNG, JPEG, etc.).
[0111] Preprocessing module: used to preprocess the input ideal QR code image to generate an ideal QR code image close to the real scene, including the following two sub-modules:
[0112] Regularized dot matrix generation module: creates a dot matrix effect for a QR code. By setting the dot size, density, and grayscale threshold, the QR code image is redrawn and the QR code image consisting of regularly arranged dots is output.
[0113] Affine transformation module: applies affine transformation (such as rotation, scaling, perspective transformation, etc.) to the rasterized QR code, simulates the geometric deformation of the QR code in the real scene, and generates a QR code image with visual realism.
[0114] Style transfer module: used to transfer the style of the pre-processed QR code image, including three sub-modules:
[0115] Feature extraction module: uses a pre-trained convolutional neural network to extract multi-level features of the QR code image and background image (including global style and local texture information);
[0116] Content preservation module: ensures that the foreground information of the QR code is completely preserved through content loss, avoiding distortion of important content due to style transfer;
[0117] Style matching module: optimizes the style consistency between the QR code and the background through style loss, reducing the visual abruptness of the boundary.
[0118] Deep background fusion module: used to perform deep background fusion of the QR code image after style transfer with the background image; it uses the Poisson image fusion algorithm and multiple loss functions to seamlessly fuse the QR code after style transfer with the specified background image. It includes three sub-modules:
[0119] Poisson gradient optimization module: ensures the smoothness and visual consistency of the QR code boundary by calculating the Poisson gradient loss of the fusion area;
[0120] Fusion image generation module: By optimizing the content loss and style loss of the fusion image, a QR code image with no obvious boundary contrast and natural transition between texture and style is generated;
[0121] Regularization processing module: Introducing histogram loss and total variation loss to stabilize the style changes in the fusion area, while removing redundant details to improve the overall quality of the image.
[0122] Output module: used to output damaged QR code images, providing multiple saving options (PNG, JPEG, etc.), and supporting parameter adjustment and optimized regeneration.
[0123] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for generating a damaged two-dimensional code image, characterized in that: include: Get the ideal QR code image and background image; Preprocessing the ideal two-dimensional code image; Perform style transfer processing on the preprocessed QR code image; Perform deep background fusion of the style-transferred QR code image and the background image; Outputs the damaged QR code image.
2. The method for generating a damaged two-dimensional code image according to claim 1, characterized in that: The preprocessing of the ideal two-dimensional code image includes: Convert an ideal two-dimensional code image into a two-dimensional code image composed of regularly arranged dots; Perform affine transformation on the dot QR code image.
3. The method for generating a damaged two-dimensional code image according to claim 2, characterized in that: The method of converting the ideal two-dimensional code image into a two-dimensional code image composed of regularly arranged dots includes: Convert the ideal QR code image into a grayscale image; Set the dot parameters of the dot QR code image and the preset threshold of the grayscale image; If the grayscale value of a pixel on the grayscale image is lower than a preset threshold, a dot is drawn at the position corresponding to the pixel; The drawn dots generate a dot QR code image according to the dot parameters.
4. The method for generating a damaged two-dimensional code image according to claim 3, characterized in that: The dot parameters include the size and density of the dots.
5. The method for generating a damaged two-dimensional code image according to claim 1, characterized in that: The performing style transfer processing on the pre-processed two-dimensional code image includes: The preprocessed QR code image is used as the first original image and input into the convolutional neural network; A first white noise image is randomly generated and input into the convolutional neural network; Performing feature extraction on the first original image and the first white noise image in the convolutional neural network respectively, obtaining feature representations of the first original image and the first white noise image in the convolutional neural network, and calculating content loss in the convolutional neural network according to the feature representations of the two; Input the background image into the convolutional neural network and perform feature extraction to obtain feature representation of the background image in the convolutional neural network, and calculate the style loss in the convolutional neural network according to the feature representations of the background image and the first white noise image in the convolutional neural network; The total loss is obtained by weighted summing of content loss and style loss; The total loss and the first white noise image are gradient calculated to continuously optimize the first white noise image until the set number of iterations is reached, and finally the first generated image is formed.
6. The method for generating a damaged two-dimensional code image according to claim 5, characterized in that: The content loss is calculated using formula (1): in, is the content loss of the first original image and the first generated image at layer l, and are the first original image and the first generated image respectively, and Respectively represent the content features of the first original image and the first generated image at the lth layer and position (i, j).
7. The method for generating a damaged two-dimensional code image according to claim 6, characterized in that: The style loss is calculated using the following formula (2): in, The style loss of each layer of the convolutional neural network for the first generated image and the background image is calculated according to the weight w l The total style loss obtained by weighted summation, and are the style features of the first generated image and the background image at the lth layer and position (i, j) respectively.
8. The method for generating a damaged two-dimensional code image according to claim 7, characterized in that: The total loss is calculated by the following formula (3): Among them, α and β are the weights of content loss and style loss respectively, and the content loss weight α = 1×10 3 , style loss weight β = 1 × 10 -2 .
9. The method for generating a damaged two-dimensional code image according to claim 8, characterized in that: The partial derivative of the pixel value of the first white noise image is calculated according to the total loss to obtain the gradient of the first white noise image relative to the total loss, and then the first generated image is generated by iterative optimization. The iterative formula is as follows: in, For the first generated image, is the first white noise image, and η is the learning rate.
10. The method for generating a damaged two-dimensional code image according to claim 1, characterized in that: The depth background fusion of the style-transferred QR code image and the background image includes two stages: Stage 1: randomly generate a second white noise image and input it into the convolutional neural network; The QR code image after style transfer is used as the second original image and input into the convolutional neural network; fusing the second white noise image and the background image to form a preliminary fused image; The preliminary fused image is optimized in turn using Poisson gradient loss, content loss and style loss, and regularization loss to form a refined fused image. The second stage: optimize the content loss, style loss and regularization loss of the refined fusion image to form the final damaged QR code image.