QR two-dimensional code correction method and system
By combining the Swing transformer model and the spatial transformation network, the problems of QR code distortion, defects, and low quality are solved, achieving efficient image correction and repair, and improving the QR code recognition capability.
Patent Information
- Application Number
- CN202410964544.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-18
- Publication Date
- 2026-11-24
- Estimated Expiration
- 2044-07-18
AI Technical Summary
Existing QR code correction and repair methods cannot effectively solve the problems of distortion, defects, and low quality. In particular, when the QR code imaging quality is insufficient or the printing is poor, perspective estimation and displacement estimation methods fail, and threshold estimation optimization methods can only solve the low quality problem caused by uneven illumination, but cannot deal with the problems of distortion, defects, and low quality.
The Swing transformer model is used for position encoding and feature extraction. The deformation field and grayscale field are predicted by low-resolution image features. The spatial transformation network is combined for elastic transformation. High-resolution features are fused for correction. Multilayer perceptron network and deconvolution technology are used for image inpainting.
It effectively corrects and repairs distorted, damaged, and low-quality QR codes, improves the imaging quality of QR code images, solves various imaging problems end-to-end, and improves the decoding success rate of QR codes.
Smart Images

Figure CN118940782B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of image processing technology, and in particular to a method and system for correcting and repairing QR codes. Background Technology
[0002] QR codes are an important information carrier, widely used on goods, tickets, and other items, providing them with authentication credentials and key information transmission capabilities, making them an indispensable part of these products. However, three major problems often exist during the printing or photography process: first, distortion, including insufficient shape due to bending, twisting, and miscutting; second, defects, including broken needles and skipped frames; and third, low quality, including insufficient image quality due to camera shake and uneven inkjet printing. These problems reduce the quality of the QR code image, making the information unreadable and thus negating its ability to serve as an authentication credential and transmit key information.
[0003] There are currently three main methods for QR code correction and repair: First, perspective estimation, which corrects QR code distortion. This method typically involves first acquiring key points in the QR code (such as positioning modules), then estimating the homography transformation matrix based on the positional relationships between these key points, and applying the homography transformation matrix to the QR code image to correct distortion. Second, displacement estimation, also used to correct QR code image distortion. This method generally estimates the positional offset of the distorted QR code image pixel by pixel, correcting the distortion through this offset. Third, threshold estimation optimization, used to improve the quality of QR code images. Because QR codes encounter uneven lighting and ink density during imaging, the binarization threshold is difficult to determine, reducing the decoding success rate. Threshold estimation optimization aims to propose a locally adaptive threshold estimation method, enabling low-quality QR codes to achieve better binarization results, thereby improving the decoding success rate.
[0004] Existing QR code image correction and restoration methods have the following drawbacks: Perspective estimation methods estimate perspective transformations through key points, correcting distortions caused by various perspective imaging techniques. However, perspective estimation relies on key points, and when the QR code image quality is insufficient or the QR code itself is poorly printed, key points are easily damaged, rendering perspective estimation ineffective. Perspective estimation also cannot handle distortions caused by elastic transformations such as cylindrical and torsional distortions, nor can it resolve distortions caused by wrinkles or misalignments, limiting its applicability. Furthermore, perspective estimation can only correct perspective distortions in QR code images, but cannot address issues of missing or low-quality images. Displacement estimation methods estimate the displacement of each pixel in the QR code image, addressing distortion problems in various scenarios. However, displacement estimation also cannot solve the problems of missing or low-quality QR code images. The threshold estimation optimization method can only solve the problem of inaccurate threshold estimation during binarization. It is only applicable to low-quality images caused by uneven lighting and slight blur. It cannot solve the problems of QR code image distortion, defects, and low-quality QR code images caused by motion blur. Summary of the Invention
[0005] Purpose of the invention: The present invention aims to provide a feasible and efficient QR code correction and repair method to correct distorted, damaged, and low-quality QR code images to a recognizable and easily identifiable state; another purpose of the present invention is to provide a QR code correction and repair system.
[0006] Technical solution: The QR code correction and repair method of the present invention includes the following steps:
[0007] (1) Encode the location of the QR code image;
[0008] (2) Extract features from the location-encoded image to obtain low-resolution image features and high-resolution image features;
[0009] (3) Based on the features of the low-resolution image, the deformation field and grayscale field are predicted, and the deformation field and grayscale field are embedded to convert the deformation field and grayscale field into global distortion and grayscale features.
[0010] (4) Using a spatial transformation network, elastic transformation is performed on the low-resolution image features based on the deformation field to obtain elastically transformed low-resolution image features.
[0011] (5) Upsample the low-resolution image features, global distortion and grayscale features and elastic transformation low-resolution image features, and fuse them with the high-resolution image features.
[0012] (6) Perform deconvolution on the fused features to obtain the corrected features with the same resolution as the QR code image in step (1);
[0013] (7) Based on the correction features, extract the correction deformation field and correction gray field, and perform correction and repair on the QR code image in step (1).
[0014] Furthermore, in step (1), the relative position encoding in the Swin transformer model is used to apply the position encoding information to the calculation of the self-attention similarity matrix of the feature extraction network.
[0015] Furthermore, in step (2), based on the Swin transformer model, image segmentation feature extraction is adopted. For the image segments after position encoding, window multi-head self-attention and sliding window multi-head self-attention are used to extract the features of the image segments, so as to obtain low-resolution image features and high-resolution image features.
[0016] Furthermore, multi-head self-attention with a window and multi-head self-attention with a sliding window are used to extract features from image slices, resulting in features at image scales of 1 / 32, 1 / 16, 1 / 8, and 1 / 4, denoted as C5, C4, C3, and C2.
[0017] Furthermore, C5 is upsampled and then superimposed onto C4. The superimposed features are used as low-resolution image features. The low-resolution image features are then passed through the output layer to output the deformation field and grayscale field.
[0018] Furthermore, the output layer is a multilayer perceptron network, which is implemented in the same way as the multilayer perceptron layer in the transformer model; at this time, the width and height of the output deformation field and grayscale field are both 1 / 16 of the width and height of the QR code image in step (1).
[0019] Furthermore, in step (5), the low-resolution image features, global distortion and grayscale features and elastic transformation low-resolution image features are stacked, convolved and upsampled, and then stacked with C3 to obtain C3_merged features; similarly, C3_merged features are stacked, convolved and upsampled, and then stacked with C2 to obtain C2_merged features; that is, feature fusion is completed.
[0020] Furthermore, the C2_merged feature undergoes two deconvolutions to obtain a corrected feature with the same resolution as the QR code image in step (1). This corrected feature is then processed by a multilayer perceptron to output the deformation field and grayscale field, respectively.
[0021] Furthermore, the objective loss function Loss of the Swin transformer model is...
[0022]
[0023] in, Indicates distortion loss. Indicates grayscale loss. Let represent the predicted normalized distortion correction offset matrix, W represent the true normalized distortion correction offset matrix, and ρ be the weight adjustment matrix. G represents the predicted grayscale correction offset matrix, and G represents the actual grayscale correction offset matrix.
[0024] When calculating overall distortion and grayscale loss, ρ is set to 0.1; when calculating local distortion and grayscale loss, ρ is set to 1.
[0025] The QR code correction and repair system of the present invention includes:
[0026] The location encoding module is used to encode the location of the QR code image;
[0027] The feature extraction module is used to extract image features at different resolutions;
[0028] The prediction module is used to predict the deformation field and grayscale field based on the features of the low-resolution image;
[0029] The spatial transformation network module is used to perform elastic transformation on the low-resolution image features based on the deformation field to obtain elastically transformed low-resolution image features.
[0030] The feature fusion module is used to upsample low-resolution image features, global distortion and grayscale features and elastic transformation low-resolution image features, and fuse them with high-resolution image features.
[0031] The correction module is used to deconvolve the fused features to obtain the correction deformation field and correction grayscale field, and to correct and repair the QR code image.
[0032] Beneficial Effects: Compared with existing technologies, the significant advantages of this invention are: 1. Based on low-resolution image features, this invention predicts the deformation field and grayscale field. The deformation field is used to correct the distortion of the QR code image, while the grayscale field is used to repair the defects of the QR code image and improve its quality; 2. This invention adopts a learning process from the whole to the local, which has a better effect on correcting distortions with large displacements. At the same time, this method uses the grayscale field and deformation field learning together as the two outputs of the learning model, solving the distortion problem, defect problem, and low quality problem of QR code images end-to-end, thus improving the imaging quality of QR code images; 3. This invention uses both low-resolution and high-resolution features of the image to estimate the deformation field and grayscale field of the QR code image. The low-resolution features are used to estimate the overall distortion and grayscale shift of the QR code image, while the high-resolution features are used to estimate the local distortion and grayscale shift of the QR code image. Attached Figure Description
[0033] Figure 1 This is a flowchart of the present invention. Detailed Implementation
[0034] The invention will now be further described with reference to the accompanying drawings.
[0035] The QR code correction and repair method of the present invention includes the following steps:
[0036] (1) Encode the location of the QR code image.
[0037] The location encoding method is related to the feature extraction method of the feature extraction network. Location encoding is used to introduce location features into the model, improving the model's perception and understanding of location information and relationships. This invention adopts a scheme of segmenting first and then extracting features. The location encoding uses the relative location encoding of the Swin transformer model. The location encoding information is used in the calculation of the self-attention similarity matrix of the feature extraction network. The calculation method of self-attention is as follows:
[0038]
[0039] Where B represents the relative position deviation, which is the position encoding information calculated by the position encoding module; Q, K, and V represent query, key, and value, respectively, and are all image features; d is the normalization factor, which is a hyperparameter constant; and SoftMax is the softmax function.
[0040] (2) Extract features from the location-encoded image to obtain low-resolution image features and high-resolution image features.
[0041] Feature extraction is achieved using the Swin transformer model, which employs image segmentation feature extraction technology. For the location-encoded QR code image, multi-head self-attention with a window and sliding window are used to extract features from the image segments, establishing feature relationships between them. This achieves high computational efficiency while maintaining high feature extraction capability. The feature extraction network outputs features at scales of 1 / 32, 1 / 16, 1 / 8, and 1 / 4 of the input QR code image, denoted as C5, C4, C3, and C2.
[0042] (3) Based on the features of the low-resolution image, the deformation field and gray field are predicted, and the deformation field and gray field are embedded to convert the deformation field and gray field into global distortion and gray features.
[0043] Features C5 and C4 were selected as features for predicting global distortion and grayscale. Since features C5 and C4 have different resolutions, an upsampling operation is needed to increase the resolution of feature C5 before it is superimposed onto feature C4. The superimposed feature is denoted as C4_merged. The C4_merged feature passes through the output layer, outputting the deformation field and grayscale field. The output layer is a multilayer perceptron network, implemented in the same way as the multilayer perceptron layer (MLP) in the transformer model. At this point, the scale of the output deformation field and grayscale field is much smaller than that of the input QR code image, with both width and height being 1 / 16 of the input QR code image's width and height.
[0044] (4) Using a spatial transformation network, elastic transformation is performed on the low-resolution image features based on the deformation field to obtain elastically transformed low-resolution image features.
[0045] The Spatial Transformation Network is a differentiable elastic transformation operation used to transform C4_merged features according to the deformation field, and is used to predict local distortion and gray-level difference.
[0046] (5) Upsample the low-resolution image features, global distortion and grayscale features and elastic transformation low-resolution image features, and fuse them with the high-resolution image features.
[0047] The fused features include low-resolution image features, global distortion and grayscale features, elastic transformation low-resolution image features, and high-resolution image features. Specifically, the embedded features of the deformation field and grayscale field predicted by the low-resolution image features—namely, the global distortion and grayscale features—are implemented through an embedding layer. This is achieved by embedding the deformation field and grayscale field predicted by the low-resolution image features, respectively. The channel dimension of the embedded features is the same as that of the C4 features. The embedded features, along with the C4_merged features before and after spatial transformation, undergo an Unsample(Conv(Concat())) operation (i.e., stacking + convolution + upsampling), and are then stacked with the C3 features to obtain the C3_merged features. Similarly, the C3_merged features undergo an Unsample(Conv(Concat())) operation and are then stacked with the C2 features to obtain the C2_merged features. This completes the fusion of low-resolution and high-resolution image features.
[0048] (6) Perform deconvolution on the fused features to obtain the corrected features with the same resolution as the QR code image in step (1).
[0049] Local distortion and grayscale are predicted by high-resolution image features because only high-resolution image features can retain enough details. The C2_merged features are deconvolved twice to obtain corrected features with the same resolution as the input QR code image. The C2_merged features combine low-resolution image features, overall distortion and grayscale features, as well as high-resolution image features.
[0050] (7) Based on the correction features, extract the correction deformation field and correction grayscale field, and perform correction and repair on the QR code image in step (1). This correction feature is then processed by a multilayer perceptron (MLP) layer to output the correction deformation field and correction grayscale field respectively. At this time, the deformation field and grayscale field can be directly applied to the input image to correct the distortion, defects and low quality problems of the image.
[0051] The model's output includes a deformation field and a grayscale field, and the model's objective loss function is:
[0052]
[0053] in, Indicates distortion loss. Indicates grayscale loss. Let represent the predicted normalized distortion correction offset matrix, W represent the true (or labeled) normalized distortion correction offset matrix, and ρ be the weight adjustment matrix. Let G represent the predicted grayscale correction offset matrix, G represent the true grayscale correction offset matrix, and ||*|| represent the L1 loss.
[0054] When calculating overall distortion and grayscale loss, ρ is set to 0.1; when calculating local distortion and grayscale loss, ρ is set to 1. The total loss is the sum of overall distortion and grayscale loss and local distortion and grayscale loss.
[0055] Model Training. The training samples are manually created sample pairs. Each sample pair includes the same standard QR code image (the baseline QR code image) and a flawed QR code image, both images being collected from the same QR code. All samples have a resolution of 256*256, with the center of the baseline QR code image coinciding with the center of the QR code, and the same amount of white space around the baseline QR code. Before training, the distortion and grayscale shift of all sample pairs are known. During training, the overall distortion and grayscale are reduced in resolution using feature downsampling as losses in the overall deformation field and grayscale field.
[0056] Inference and prediction. During inference, a 256*256 QR code image is input, and the local deformation field and grayscale field are output. The distortion is corrected by the deformation field, and then the grayscale field is superimposed to restore the image quality, thus completing the correction and restoration of the input QR code image.
Claims
1. A method for correcting and repairing QR codes, characterized in that, Includes the following steps: (1) Encode the location of the QR code image; (2) Extract features from the location-encoded image to obtain low-resolution image features and high-resolution image features; (3) Based on the features of the low-resolution image, the deformation field and grayscale field are predicted, and the deformation field and grayscale field are embedded to convert the deformation field and grayscale field into global distortion and grayscale features. (4) Using a spatial transformation network, elastic transformation is performed on the low-resolution image features based on the deformation field to obtain elastically transformed low-resolution image features; (5) Upsample the low-resolution image features, global distortion, grayscale features and elastic transformation low-resolution image features, and fuse them with the high-resolution image features; (6) Perform deconvolution on the fused features to obtain the corrected features with the same resolution as the QR code image in step (1); (7) Based on the correction features, extract the correction deformation field and correction gray field, and perform correction and repair on the QR code image in step (1); In step (1), the relative position encoding in the Swin transformer model is used to apply the position encoding information to the calculation of the self-attention similarity matrix of the feature extraction network.
2. The QR code correction and repair method according to claim 1, characterized in that, In step (2), image slice feature extraction is performed based on the Swintransformer model. For the slices of the image after position encoding, window multi-head self-attention and sliding window multi-head self-attention are used to extract the features of the image slices, so as to obtain low-resolution image features and high-resolution image features.
3. The QR code correction and repair method according to claim 2, characterized in that, The features of image slices are extracted using window multi-head self-attention and sliding window multi-head self-attention, resulting in features at image scales of 1 / 32, 1 / 16, 1 / 8, and 1 / 4, denoted as C5, C4, C3, and C2.
4. The QR code correction and repair method according to claim 3, characterized in that, After upsampling, C5 is superimposed on C4. The superimposed features are used as low-resolution image features. The low-resolution image features are then passed through the output layer to output the deformation field and grayscale field.
5. The QR code correction and repair method according to claim 4, characterized in that, The output layer is a multilayer perceptron network, which is implemented in the same way as the multilayer perceptron layer in the transformer model; at this time, the width and height of the output deformation field and grayscale field are both 1 / 16 of the width and height of the QR code image in step (1).
6. The QR code correction and repair method according to claim 5, characterized in that, In step (5), the low-resolution image features, global distortion and grayscale features and elastic transformation low-resolution image features are stacked, convolved and upsampled, and then stacked with C3 to obtain C3_merged features; Similarly, the C3_merged features are stacked, convolutional, and upsampling, and then stacked with C2 to obtain the C2_merged features; thus completing feature fusion.
7. The QR code correction and repair method according to claim 6, characterized in that, The C2_merged feature is deconvolved twice to obtain a corrected feature with the same resolution as the QR code image in step (1). This corrected feature is then processed by a multilayer perceptron to output the deformation field and grayscale field respectively.
8. The QR code correction and repair method according to claim 7, characterized in that, The objective loss function of the Swin transformer model Loss for ; in, Indicates distortion loss. Indicates grayscale loss. This represents the predicted normalized distortion correction offset matrix. This represents the true normalized distortion correction offset matrix. For the weight adjustment matrix, This represents the predicted grayscale correction offset matrix. This represents the actual grayscale correction offset matrix; When calculating overall distortion and grayscale loss The value is 0.1; when calculating local distortion and grayscale loss, Set to 1.
9. A QR code correction and repair system, characterized in that, include: The location encoding module is used to encode the location of the QR code image; The relative positional encoding in the Swing transformer model is used to apply the positional encoding information to the calculation of the self-attention similarity matrix of the feature extraction network; The feature extraction module is used to extract image features at different resolutions; The prediction module is used to predict the deformation field and grayscale field based on the features of the low-resolution image; The spatial transformation network module is used to perform elastic transformation on the low-resolution image features based on the deformation field to obtain elastically transformed low-resolution image features. The feature fusion module is used to upsample low-resolution image features, global distortion, grayscale features, and elastic transformation low-resolution image features, and fuse them with high-resolution image features. The correction module is used to deconvolve the fused features to obtain the correction deformation field and correction grayscale field, and to correct and repair the QR code image.
Citation Information
Patent Citations
Multi-source fused aerial view sensing target detection method, device and equipment and medium
CN117315424A
Generative system and method for enhancing readability of barcodes using frequency guided computer vision
US11875259B1
