A two-dimensional code recognition method, system and medium under complex environmental conditions

By using preset detection model and threshold segmentation algorithm in complex environments to extract the QR code area and perform affine transformation correction, the problem of insufficient QR code recognition accuracy and anti-interference ability in the prior art is solved, and high accuracy is achieved rate and high efficiency recognition effect.

CN118396010BActive Publication Date: 2024-11-08HANGZHOU HUICUI INTELLIGENT TECH CO LTD
View PDF 8 Cites 0 Cited by

Patent Information

Application Number
CN202410850876.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-06-28
Publication Date
2024-11-08
Estimated Expiration
2044-06-28

AI Technical Summary

Technical Problem

The prior art lacks accuracy and environmental interference resistance of QR code recognition in complex environments, especially under affine transformation and replicated lighting conditions. The poor adaptability of traditional threshold segmentation algorithms leads to low positioning accuracy and efficiency.

Method used

The preset detection model is used to extract the QR code's area of ​​interest, perform median filtering and grayscale. If the recognition fails, the preset threshold segmentation algorithm is used to perform binary processing, and the back-type positioning angle is determined through contour extraction and affine is calculated. The transformation matrix performs image correction and is used for identification with a preset recognition algorithm.

Benefits of technology

It improves the positioning accuracy and anti-environmental interference capability of QR codes in complex environments, and improves the recognition success rate and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118396010B_ABST
    Figure CN118396010B_ABST
Patent Text Reader

Abstract

The present application discloses a method, system and medium for two-dimensional code recognition under complex environmental conditions, the method comprising: obtaining an original image, extracting a two-dimensional code ROI area in the original image; median filtering and graying the two-dimensional code ROI area to obtain a grayscale image of the ROI area; recognizing the grayscale image of the ROI area, and returning the two-dimensional code recognition result if the recognition is successful; if the recognition fails, binarizing the grayscale image; using the binarized image to extract the contour and determine the three return-shaped positioning angles of the two-dimensional code; according to the center point coordinates of the three return-shaped positioning angles of the two-dimensional code, determine the position of each positioning angle; according to the position of each positioning angle, obtain an affine transformation matrix, correct the binarized image and recognize it, and if the recognition is successful, return the recognition content and positioning parameters, otherwise, return the recognition failure. The present application has high positioning accuracy, strong anti-environmental interference ability, and high recognition success rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of two-dimensional code recognition, and in particular to a two-dimensional code recognition method, system and medium under complex environmental conditions. Background Art

[0002] QR code (Quick Response Code, two-dimensional code) is widely used due to its large information storage capacity, good confidentiality, and high traceability. However, in actual applications, the traceability environment is complex and changeable, and the difficulty of QR code recognition is greatly increased. Existing research focuses on the recognition of clear QR code images obtained at rest or low speed, and there is little research on QR under replica lighting conditions.

[0003] Most existing technologies use traditional threshold segmentation followed by contour and other features to obtain the QR area, which is then parsed using open source libraries such as zbar or zxing. When it comes to QR code positioning in complex environments, traditional threshold segmentation algorithms have poor adaptability, which greatly reduces the accuracy of QR positioning. At the same time, for QR code images with affine transformations, the direct parsing readiness rate will also be greatly reduced. In addition, industrial images are often relatively large, and directly scanning and analyzing the original image will also greatly reduce efficiency. Therefore, a QR code recognition method under complex environmental conditions is urgently needed. Summary of the invention

[0004] The purpose of this application is to provide a two-dimensional code recognition method, system and medium under complex environmental conditions. The present application has high positioning accuracy, strong anti-environmental interference ability and high recognition success rate.

[0005] The present application provides a method for identifying a two-dimensional code under complex environmental conditions, the method comprising:

[0006] Get the original image, and use the preset detection model to extract the ROI area of ​​interest of the QR code in the original image;

[0007] Performing median filtering and grayscale conversion on the ROI area of ​​the two-dimensional code to obtain a grayscale image of the ROI area;

[0008] Use the preset recognition algorithm to recognize the grayscale image of the ROI area, and return the QR code recognition result if the recognition is successful;

[0009] If the recognition fails, the grayscale image is binarized using a preset threshold segmentation algorithm;

[0010] Use the binary image to extract the contour and determine the three return angles of the QR code;

[0011] According to the center point coordinates of the three return-shaped positioning angles of the QR code, the position of each positioning angle is determined;

[0012] The affine transformation matrix is ​​obtained according to the position of each positioning angle, and the binary image is corrected using the affine transformation matrix; the corrected image is recognized using the preset recognition algorithm, and if the recognition is successful, the recognition content and positioning parameters are returned, otherwise, the recognition failure is returned.

[0013] In this solution, the grayscale image is binarized using a preset threshold segmentation algorithm, including:

[0014] The grayscale image is binarized, and the maximum inter-class variance threshold outsValue, the average pixel value meanVal1 of the target area and the average pixel value meanVal2 of the background area are obtained, and the binarized image is processed using morphological opening.

[0015] In this solution, the binary image is used to extract the contour and determine the three return angles of the QR code, including:

[0016] Perform contour extraction on the binary image, and use the contour to screen the three return-shaped positioning angles. If the return-shaped positioning angles cannot be screened, update the segmentation threshold in the preset threshold segmentation algorithm to perform contour extraction until the three return-shaped positioning angles are extracted;

[0017] In this solution, the three return positioning angles are screened by contour, and the contour screening steps include:

[0018] Traverse the contours and find all contours that have two levels of parent contours;

[0019] Filter out qualified contours by contour area and aspect ratio;

[0020] Finally, all the selected contours are sorted from large to small according to the rectangularity, and the first three contours are taken as the QR code positioning contours;

[0021] Where: rectangularity = contour area / minimum outer contour area.

[0022] In this solution, if the return positioning angle cannot be screened, the segmentation threshold in the preset threshold segmentation algorithm is updated to perform contour extraction until three return positioning angles are extracted, including:

[0023] Using the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area, the segmentation threshold is updated, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is:

[0024] newThred = otusValue - (otusValue - meanVal1) / 2;

[0025] Perform morphological closing operation on the binary image, then extract the contour, traverse the contour again, and locate the three positioning angle contours;

[0026] If the positioning fails, the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area are used to update the segmentation threshold, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is:

[0027] newThred = otusValue + (meanVal2 - otusValue) / 2;

[0028] Perform morphological opening operation on the binary image, then extract the contour, traverse the contour again, and locate the contours of the three return-shaped positioning angles.

[0029] In this solution, the position of each positioning angle is determined according to the center point coordinates of the three return positioning angles of the QR code, specifically including:

[0030] Take the upper left corner of the image as the coordinate origin, calculate the coordinates of the center points of the three circular positioning angles, calculate the three vectors with the three coordinate points, calculate the angle between each vector respectively, and determine the upper left positioning angle and the other two positioning angles of the QR code through the angle;

[0031] In this solution, the upper left positioning angle and the other two positioning angles of the QR code are determined by the included angle, specifically:

[0032] If the angle between the two vectors is 90 degrees, or the difference between the angle and 90 degrees is the smallest, the intersection of the two vectors is the upper left positioning angle of the QR code. Then, the positive and negative inner product of the vectors is used to determine the other two return positioning angles.

[0033] In this solution, the center point P1 (x1, y1) and the center point P2 (x2, y2) of the positioning angle contour are defined, x1 and x2 are the horizontal coordinates of the center point, y1 and y2 are the vertical coordinates of the center point, then P1*P2= x1 * y2 - x2 * y1;

[0034] If p1 * p2>0, then p1 is in the clockwise direction of p2;

[0035] If p1 * p2<0, then p1 is counterclockwise of p2;

[0036] If p1 * p2 = 0, then p1 and p2 are collinear.

[0037] A second aspect of the present application provides a two-dimensional code recognition system under complex environmental conditions, the system comprising: a memory, a processor, the memory comprising a two-dimensional code recognition method program under complex environmental conditions, the two-dimensional code recognition method program under complex environmental conditions being executed by the processor to implement the following steps:

[0038] Get the original image, and use the preset detection model to extract the ROI area of ​​interest of the QR code in the original image;

[0039] Performing median filtering and grayscale conversion on the ROI area of ​​the two-dimensional code to obtain a grayscale image of the ROI area;

[0040] Use the preset recognition algorithm to recognize the grayscale image of the ROI area, and return the QR code recognition result if the recognition is successful;

[0041] If the recognition fails, the grayscale image is binarized using a preset threshold segmentation algorithm;

[0042] Use the binary image to extract the contour and determine the three return angles of the QR code;

[0043] According to the center point coordinates of the three return-shaped positioning angles of the QR code, the position of each positioning angle is determined;

[0044] The affine transformation matrix is ​​obtained according to the position of each positioning angle, and the binary image is corrected using the affine transformation matrix; the corrected image is recognized using the preset recognition algorithm, and if the recognition is successful, the recognition content and positioning parameters are returned, otherwise, the recognition failure is returned.

[0045] A third aspect of the present application provides a computer-readable storage medium, which includes a two-dimensional code recognition method program under complex environmental conditions. When the two-dimensional code recognition method program under complex environmental conditions is executed by a processor, the steps of the two-dimensional code recognition method under complex environmental conditions are implemented.

[0046] As can be seen from the above, the two-dimensional code recognition method, system and medium provided by the present application under complex environmental conditions obtain the original image, and use the preset detection model to extract the ROI area of ​​interest of the two-dimensional code in the original image; the two-dimensional code ROI area is subjected to median filtering and graying to obtain a grayscale image of the ROI area; the grayscale image of the ROI area is recognized by a preset recognition algorithm, and if the recognition is successful, the two-dimensional code recognition result is returned; if the recognition fails, the grayscale image is binarized by a preset threshold segmentation algorithm; the binarized image is used to extract the contour and determine the three return-shaped positioning angles of the two-dimensional code; according to the center point coordinates of the three return-shaped positioning angles of the two-dimensional code, the position of each positioning angle is determined; according to the position of each positioning angle, an affine transformation matrix is ​​obtained, and the binarized image is corrected by the affine transformation matrix; the corrected image is recognized by a preset recognition algorithm, and if the recognition is successful, the recognition content and positioning parameters are returned, otherwise, the recognition failure is returned. The present application has high positioning accuracy, strong anti-environmental interference ability, and high recognition success rate.

[0047] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or be understood by practicing the embodiments of the present application. The purpose and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments of the present application will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.

[0049] Figure 1 A flow chart of a method for QR code recognition under complex environmental conditions provided in an embodiment of the present application.

[0050] Figure 2 A block diagram provided for an embodiment of the present application. DETAILED DESCRIPTION

[0051] The technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.

[0052] It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings. At the same time, in the description of this application, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.

[0053] Please refer to Figure 1 , is a flow chart of a two-dimensional code recognition method under complex environmental conditions in some embodiments of the present application. The two-dimensional code recognition method under complex environmental conditions is used for a terminal or system for two-dimensional code recognition. The present invention does not limit specific usage scenarios, such as mobile phone software or tablet software. The two-dimensional code recognition method under complex environmental conditions includes the following steps:

[0054] S101, obtaining an original image, and extracting a QR code ROI region of interest in the original image using a preset detection model;

[0055] S102, performing median filtering and grayscale conversion on the two-dimensional code ROI area to obtain a grayscale image of the ROI area;

[0056] S103, using a preset recognition algorithm to recognize the grayscale image of the ROI area, and if the recognition is successful, returning the QR code recognition result;

[0057] S104, if the recognition fails, binarizing the grayscale image using a preset threshold segmentation algorithm;

[0058] S105, using the binary image to extract the contour and determine the three return positioning angles of the two-dimensional code;

[0059] S106, determining the position of each positioning angle according to the center point coordinates of the three return positioning angles of the two-dimensional code;

[0060] S107, obtaining an affine transformation matrix according to the position of each positioning angle, and correcting the binary image using the affine transformation matrix;

[0061] S108, using a preset recognition algorithm to recognize the corrected image, if the recognition is successful, the recognition content and positioning parameters are returned, otherwise, the recognition failure is returned.

[0062] It should be noted that the preset detection model in the present application can adopt the yolov5 detection model, the preset threshold segmentation algorithm adopts the big law, and the preset recognition algorithm is the zbar recognition algorithm. The purpose of this technology is to obtain the original image, and use the preset detection model to extract the two-dimensional code interest region in the original image, which is recorded as the two-dimensional code ROI region; the two-dimensional code ROI region is median filtered and grayed to obtain a grayscale image of the ROI region; the ROI region grayscale image is recognized by the preset recognition algorithm, and if the recognition is successful, the two-dimensional code recognition result is returned; if the recognition fails, the grayscale image is binarized by the preset threshold segmentation algorithm; the binarized image is used to extract the contour and determine the three return positioning angles of the two-dimensional code; according to the center point coordinates of the three return positioning angles of the two-dimensional code, the position of each positioning angle is determined; according to the position of each positioning angle, an affine transformation matrix is ​​obtained, and the binarized image is corrected by the affine transformation matrix; the corrected image is recognized by the preset recognition algorithm, and if the recognition is successful, the recognition content and positioning parameters are returned, otherwise, the recognition failure is returned.

[0063] It should be noted that the use of the yolov5 detection model to obtain the QR code ROI area solves the problem of low success rate of traditional image processing in locating the QR code ROI area. Image preprocessing of the ROI area can greatly reduce the impact of interference on subsequent recognition, and at the same time can improve the recognition efficiency to a certain extent.

[0064] According to an embodiment of the present invention, the binarization process of the grayscale image using a preset threshold segmentation algorithm includes:

[0065] The grayscale image is binarized, and the maximum inter-class variance threshold outsValue, the average pixel value meanVal1 of the target area and the average pixel value meanVal2 of the background area are obtained, and the binarized image is processed using morphological opening.

[0066] It should be noted that, in a specific embodiment, the preset threshold segmentation algorithm adopts the big law, that is, the maximum inter-class variance method, which is an adaptive method for determining the image binarization segmentation threshold. This method divides the image into two parts, foreground (target) and background, according to the grayscale characteristics of the image, and finds the optimal segmentation threshold by calculating the inter-class variance. When the inter-class variance is the largest, it means that the difference between the foreground and the background is the largest, so the probability of misclassification is the smallest.

[0067] To binarize a grayscale ROI (region of interest) image, you can use the Otsu method by following these steps:

[0068] Grayscale ROI image: First, make sure the ROI image is grayscale. If the original image is in color, it needs to be converted to grayscale first. Grayscale can be achieved by calculating the average value of the R, G, B components of each pixel or other methods.

[0069] Grayscale histogram: Calculate the grayscale histogram of the grayscale ROI image, that is, count the number of pixels at each grayscale level. This will provide information about the distribution of different grayscale levels in the image.

[0070] Iterate over all possible thresholds: Iterate over all possible thresholds from 0 to 255. For each threshold, divide the image into two parts: pixels with grayscale values ​​less than or equal to the threshold are considered background, and pixels greater than the threshold are considered foreground.

[0071] Calculate inter-class variance: For each threshold, calculate the inter-class variance between foreground and background. The inter-class variance is a measure of the difference between foreground and background, which is calculated based on the grayscale mean of the two parts of pixels and the overall grayscale mean.

[0072] Determine the optimal threshold: Find the threshold that maximizes the inter-class variance. This threshold is the optimal threshold to be used for binarizing the ROI image.

[0073] Apply thresholding for binarization: Use the determined optimal threshold to set all pixel values ​​in the ROI image greater than the threshold to 255 (white, representing the foreground) and those less than or equal to the threshold to 0 (black, representing the background), thus completing the binarization process.

[0074] According to an embodiment of the present invention, using a binary image to extract contours and determine three return positioning angles of a two-dimensional code specifically includes:

[0075] Perform contour extraction on the binary image, and use the contour to screen the three return-shaped positioning angles. If the return-shaped positioning angles cannot be screened, update the segmentation threshold in the preset threshold segmentation algorithm to perform contour extraction until the three return-shaped positioning angles are extracted;

[0076] According to an embodiment of the present invention, the step of using the contour to screen the three return positioning angles includes:

[0077] Traverse the contours and find all contours that have two levels of parent contours;

[0078] Filter out qualified contours by contour area and aspect ratio;

[0079] Finally, all the selected contours are sorted from large to small according to the rectangularity, and the first three contours are taken as the QR code positioning contours;

[0080] Where: rectangularity = contour area / minimum outer contour area.

[0081] According to an embodiment of the present invention, if the return positioning angle cannot be screened, the segmentation threshold in the preset threshold segmentation algorithm is updated to perform contour extraction until three return positioning angles are extracted, including:

[0082] Using the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area, the segmentation threshold is updated, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is:

[0083] newThred = otusValue - (otusValue - meanVal1) / 2;

[0084] Perform morphological closing operation on the binary image, then extract the contour, traverse the contour again, and locate the three positioning angle contours;

[0085] If the positioning fails, the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area are used to update the segmentation threshold, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is:

[0086] newThred = otusValue + (meanVal2 - otusValue) / 2;

[0087] Perform morphological opening operation on the binary image, then extract the contour, traverse the contour again, and locate the contours of the three return positioning angles.

[0088] According to an embodiment of the present invention, the position of each positioning angle is determined according to the center point coordinates of the three return-shaped positioning angles of the two-dimensional code, specifically including:

[0089] Taking the upper left corner of the image as the coordinate origin, calculate the coordinates of the center points of the three circular positioning angles, calculate the three coordinate points to obtain three vectors, calculate the angle between each vector respectively, and determine the upper left positioning angle and the other two positioning angles of the QR code through the angle.

[0090] According to an embodiment of the present invention, the upper left positioning angle and the other two positioning angles of the QR code are determined by the included angle, specifically:

[0091] If the angle between the two vectors is 90 degrees, or the difference between the angle and 90 degrees is the smallest, the intersection of the two vectors is the upper left positioning angle of the QR code. Then, the positive and negative inner product of the vectors is used to determine the other two return positioning angles.

[0092] According to an embodiment of the present invention, the center point P1 (x1, y1) and the center point P2 (x2, y2) of the positioning angle profile are defined, x1 and x2 are the horizontal coordinates of the center point, y1 and y2 are the vertical coordinates of the center point, then P1*P2= x1 * y2 - x2 * y1;

[0093] If p1 * p2>0, then p1 is in the clockwise direction of p2;

[0094] If p1 * p2<0, then p1 is counterclockwise of p2;

[0095] If p1 * p2 = 0, then p1 and p2 are collinear.

[0096] like Figure 2 The figure shows a block diagram of a QR code recognition system under complex environmental conditions.

[0097] The second aspect of the present application provides a two-dimensional code recognition system under complex environmental conditions, the system comprising: a memory 901, a processor 902, the memory comprising a two-dimensional code recognition method program under complex environmental conditions, the two-dimensional code recognition method program under complex environmental conditions being executed by the processor to implement the following steps:

[0098] S101, obtaining an original image, and extracting a QR code ROI region of interest in the original image using a preset detection model;

[0099] S102, performing median filtering and grayscale conversion on the two-dimensional code ROI area to obtain a grayscale image of the ROI area;

[0100] S103, using a preset recognition algorithm to recognize the grayscale image of the ROI area, and if the recognition is successful, returning the QR code recognition result;

[0101] S104, if the recognition fails, binarizing the grayscale image using a preset threshold segmentation algorithm;

[0102] S105, using the binary image to extract the contour and determine the three return positioning angles of the two-dimensional code;

[0103] S106, determining the position of each positioning angle according to the center point coordinates of the three return positioning angles of the two-dimensional code;

[0104] S107, obtaining an affine transformation matrix according to the position of each positioning angle, and correcting the binary image using the affine transformation matrix;

[0105] S108, using a preset recognition algorithm to recognize the corrected image, if the recognition is successful, return the recognition content and positioning parameters, otherwise, return recognition failure.

[0106] It should be noted that the preset detection model in the present application can adopt the yolov5 detection model, the preset threshold segmentation algorithm adopts the big law, and the preset recognition algorithm is the zbar recognition algorithm. The purpose of this technology is to obtain the original image, and use the preset detection model to extract the two-dimensional code interest region in the original image, which is recorded as the two-dimensional code ROI region; the two-dimensional code ROI region is median filtered and grayed to obtain a grayscale image of the ROI region; the ROI region grayscale image is recognized by the preset recognition algorithm, and if the recognition is successful, the two-dimensional code recognition result is returned; if the recognition fails, the grayscale image is binarized by the preset threshold segmentation algorithm; the binarized image is used to extract the contour and determine the three return positioning angles of the two-dimensional code; according to the center point coordinates of the three return positioning angles of the two-dimensional code, the position of each positioning angle is determined; according to the position of each positioning angle, an affine transformation matrix is ​​obtained, and the binarized image is corrected by the affine transformation matrix; the corrected image is recognized by the preset recognition algorithm, and if the recognition is successful, the recognition content and positioning parameters are returned, otherwise, the recognition failure is returned.

[0107] It should be noted that the use of the yolov5 detection model to obtain the QR code ROI area solves the problem of low success rate of traditional image processing in locating the QR code ROI area. Image preprocessing of the ROI area can greatly reduce the impact of interference on subsequent recognition, and at the same time can improve the recognition efficiency to a certain extent.

[0108] According to an embodiment of the present invention, the binarization process of the grayscale image using a preset threshold segmentation algorithm includes:

[0109] The grayscale image is binarized, and the maximum inter-class variance threshold outsValue, the average pixel value meanVal1 of the target area and the average pixel value meanVal2 of the background area are obtained, and the binarized image is processed using morphological opening.

[0110] It should be noted that, in a specific embodiment, the preset threshold segmentation algorithm adopts the big law, that is, the maximum inter-class variance method, which is an adaptive method for determining the image binarization segmentation threshold. This method divides the image into two parts, foreground (target) and background, according to the grayscale characteristics of the image, and finds the optimal segmentation threshold by calculating the inter-class variance. When the inter-class variance is the largest, it means that the difference between the foreground and the background is the largest, so the probability of misclassification is the smallest.

[0111] To binarize a grayscale ROI (region of interest) image, you can use the Otsu method by following these steps:

[0112] Grayscale ROI image: First, make sure the ROI image is grayscale. If the original image is in color, it needs to be converted to grayscale first. Grayscale can be achieved by calculating the average value of the R, G, B components of each pixel or other methods.

[0113] Grayscale histogram: Calculate the grayscale histogram of the grayscale ROI image, that is, count the number of pixels at each grayscale level. This will provide information about the distribution of different grayscale levels in the image.

[0114] Iterate over all possible thresholds: Iterate over all possible thresholds from 0 to 255. For each threshold, divide the image into two parts: pixels with grayscale values ​​less than or equal to the threshold are considered background, and pixels greater than the threshold are considered foreground.

[0115] Calculate inter-class variance: For each threshold, calculate the inter-class variance between foreground and background. The inter-class variance is a measure of the difference between foreground and background, which is calculated based on the grayscale mean of the two parts of pixels and the overall grayscale mean.

[0116] Determine the optimal threshold: Find the threshold that maximizes the inter-class variance. This threshold is the optimal threshold to be used for binarizing the ROI image.

[0117] Apply thresholding for binarization: Use the determined optimal threshold to set all pixel values ​​in the ROI image greater than the threshold to 255 (white, representing the foreground) and those less than or equal to the threshold to 0 (black, representing the background), thus completing the binarization process.

[0118] According to an embodiment of the present invention, using a binary image to extract contours and determine three return positioning angles of a two-dimensional code specifically includes:

[0119] Perform contour extraction on the binary image, and use the contour to screen the three return-shaped positioning angles. If the return-shaped positioning angles cannot be screened, update the segmentation threshold in the preset threshold segmentation algorithm to perform contour extraction until the three return-shaped positioning angles are extracted;

[0120] According to an embodiment of the present invention, the step of using the contour to screen the three return positioning angles includes:

[0121] Traverse the contours and find all contours that have two levels of parent contours;

[0122] Filter out qualified contours by contour area and aspect ratio;

[0123] Finally, all the selected contours are sorted from large to small according to the rectangularity, and the first three contours are taken as the QR code positioning contours;

[0124] Where: rectangularity = contour area / minimum outer contour area.

[0125] According to an embodiment of the present invention, if the return positioning angle cannot be screened, the segmentation threshold in the preset threshold segmentation algorithm is updated to perform contour extraction until three return positioning angles are extracted, including:

[0126] Using the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area, the segmentation threshold is updated, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is:

[0127] newThred = otusValue - (otusValue - meanVal1) / 2;

[0128] Perform morphological closing operation on the binary image, then extract the contour, traverse the contour again, and locate the three positioning angle contours;

[0129] If the positioning fails, the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area are used to update the segmentation threshold, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is:

[0130] newThred = otusValue + (meanVal2 - otusValue) / 2;

[0131] Perform morphological opening operation on the binary image, then extract the contour, traverse the contour again, and locate the contours of the three return-shaped positioning angles.

[0132] According to an embodiment of the present invention, the position of each positioning angle is determined according to the center point coordinates of the three return-shaped positioning angles of the two-dimensional code, specifically including:

[0133] Taking the upper left corner of the image as the coordinate origin, calculate the coordinates of the center points of the three circular positioning angles, calculate the three coordinate points to obtain three vectors, calculate the angle between each vector respectively, and determine the upper left positioning angle and the other two positioning angles of the QR code through the angle.

[0134] According to an embodiment of the present invention, the upper left positioning angle and the other two positioning angles of the QR code are determined by the included angle, specifically:

[0135] If the angle between the two vectors is 90 degrees, or the difference between the angle and 90 degrees is the smallest, the intersection of the two vectors is the upper left positioning angle of the QR code. Then, the positive and negative inner product of the vectors is used to determine the other two return positioning angles.

[0136] According to an embodiment of the present invention, the center point P1 (x1, y1) and the center point P2 (x2, y2) of the positioning angle profile are defined, x1 and x2 are the horizontal coordinates of the center point, y1 and y2 are the vertical coordinates of the center point, then P1*P2= x1 * y2 - x2 * y1;

[0137] If p1 * p2>0, then p1 is in the clockwise direction of p2;

[0138] If p1 * p2<0, then p1 is counterclockwise of p2;

[0139] If p1 * p2 = 0, then p1 and p2 are collinear.

[0140] A third aspect of the present application provides a computer-readable storage medium, which includes a two-dimensional code recognition method program under complex environmental conditions. When the two-dimensional code recognition method program under complex environmental conditions is executed by a processor, the steps of the two-dimensional code recognition method under complex environmental conditions are implemented.

[0141] The two-dimensional code recognition method, system and medium provided by the present application under complex environmental conditions obtain the original image, and use the preset detection model to extract the ROI area of ​​interest of the two-dimensional code in the original image; median filter and grayscale the two-dimensional code ROI area to obtain the grayscale image of the ROI area; use the preset recognition algorithm to recognize the grayscale image of the ROI area, and if the recognition is successful, return the two-dimensional code recognition result; if the recognition fails, use the preset threshold segmentation algorithm to binarize the grayscale image; use the binarized image to extract the contour and determine the three return-shaped positioning angles of the two-dimensional code; according to the center point coordinates of the three return-shaped positioning angles of the two-dimensional code, determine the position of each positioning angle; according to the position of each positioning angle, obtain the affine transformation matrix, and use the affine transformation matrix to correct the binarized image; use the preset recognition algorithm to recognize the corrected image, and if the recognition is successful, return the recognition content and positioning parameters, otherwise, return the recognition failure. The present application has high positioning accuracy, strong anti-environmental interference ability, and high recognition success rate.

[0142] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as: multiple units or components can be combined, or can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.

[0143] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units; they may be located in one place or distributed on multiple network units; some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.

[0144] In addition, all functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may be separately used as a unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.

[0145] Those skilled in the art can understand that: all or part of the steps of implementing the above method embodiments can be completed by hardware related to program instructions, the aforementioned program can be stored in a readable storage medium, and when the program is executed, it executes the steps of the above method embodiments; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), disks or optical disks, and other media that can store program codes.

[0146] Alternatively, if the above-mentioned integrated unit of the present invention is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present invention can be essentially or partly reflected in the form of a software product that contributes to the prior art. The software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROM, RAM, magnetic disks or optical disks.

Claims

1. A two-dimensional code recognition method under complex environmental conditions, characterized in that: The method comprises: Get the original image, and use the preset detection model to extract the ROI area of ​​interest of the QR code in the original image; Performing median filtering and grayscale conversion on the ROI area of ​​the two-dimensional code to obtain a grayscale image of the ROI area; Use the preset recognition algorithm to recognize the grayscale image of the ROI area, and return the QR code recognition result if the recognition is successful; If the recognition fails, the grayscale image is binarized using a preset threshold segmentation algorithm; Use the binary image to extract the contour and determine the three return angles of the QR code; According to the center point coordinates of the three return-shaped positioning angles of the QR code, the position of each positioning angle is determined; An affine transformation matrix is ​​obtained according to the position of each positioning angle, and the binary image is corrected using the affine transformation matrix; Use the preset recognition algorithm to recognize the corrected image. If the recognition is successful, the recognition content and positioning parameters are returned. Otherwise, the recognition failure is returned. The binary image is used to extract the contour and determine the three return angles of the QR code, including: Perform contour extraction on the binary image, and use the contour to screen the three return-shaped positioning angles. If the return-shaped positioning angles cannot be screened, update the segmentation threshold in the preset threshold segmentation algorithm to perform contour extraction until the three return-shaped positioning angles are extracted; The three return positioning angles are screened by using the contour, and the contour screening steps include: Traverse the contours and find all contours that have two levels of parent contours; Filter out qualified contours by contour area and aspect ratio; Finally, all the selected contours are sorted from large to small according to the rectangularity, and the first three contours are taken as the QR code positioning contours; Where: rectangularity = contour area / minimum outer contour area; If the return positioning angle cannot be screened, the segmentation threshold in the preset threshold segmentation algorithm is updated to perform contour extraction until three return positioning angles are extracted, including: Using the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area, the segmentation threshold is updated, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is: newThred = otusValue - (otusValue - meanVal1) / 2; Perform morphological closing operation on the binary image, then extract the contour, traverse the contour again, and locate the three positioning angle contours; If the positioning fails, the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area are used to update the segmentation threshold, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is: newThred = otusValue + (meanVal2 - otusValue) / 2; Perform morphological opening operation on the binary image, then extract the contour, traverse the contour again, and locate the contours of the three return positioning angles.

2. The method for identifying a two-dimensional code under complex environmental conditions according to claim 1, characterized in that: The method of performing binarization processing on the grayscale image by using a preset threshold segmentation algorithm includes: The grayscale image is binarized, and the maximum inter-class variance threshold outsValue, the average pixel value meanVal1 of the target area and the average pixel value meanVal2 of the background area are obtained, and the binarized image is processed using morphological opening.

3. The two-dimensional code recognition method under complex environmental conditions according to claim 1, characterized in that: According to the center point coordinates of the three return-shaped positioning angles of the QR code, the position of each positioning angle is determined, including: Taking the upper left corner of the image as the coordinate origin, calculate the coordinates of the center points of the three circular positioning angles, calculate the three coordinate points to obtain three vectors, calculate the angle between each vector respectively, and determine the upper left positioning angle and the other two positioning angles of the QR code through the angle.

4. The two-dimensional code recognition method under complex environmental conditions according to claim 3 is characterized in that: The upper left positioning angle and the other two positioning angles of the QR code are determined by the included angle, specifically: If the angle between the two vectors is 90 degrees, or the difference between the angle and 90 degrees is the smallest, the intersection of the two vectors is the upper left positioning angle of the QR code. Then, the positive and negative inner product of the vectors is used to determine the other two return positioning angles.

5. The method for identifying a two-dimensional code under complex environmental conditions according to claim 4, characterized in that: Define the center point P1 (x1, y1) and the center point P2 (x2, y2) of the positioning angle contour. x1 and x2 are the horizontal coordinates of the center point, and y1 and y2 are the vertical coordinates of the center point. Then P1*P2= x1 * y2 - x2 * y1; If p1 * p2 > 0, then p1 is clockwise of p2; If p1 * p2 < 0, then p1 is counterclockwise of p2; If p1 * p2 = 0, then p1 and p2 are collinear.

6. A two-dimensional code recognition system under complex environmental conditions, characterized in that: The system includes: a memory and a processor, wherein the memory includes a two-dimensional code recognition method program under complex environmental conditions, and the two-dimensional code recognition method program under complex environmental conditions is executed by the processor to implement the following steps: Get the original image, and use the preset detection model to extract the ROI area of ​​interest of the QR code in the original image; Performing median filtering and grayscale conversion on the ROI area of ​​the two-dimensional code to obtain a grayscale image of the ROI area; Use the preset recognition algorithm to recognize the grayscale image of the ROI area, and return the QR code recognition result if the recognition is successful; If the recognition fails, the grayscale image is binarized using a preset threshold segmentation algorithm; Use the binary image to extract the contour and determine the three return angles of the QR code; According to the center point coordinates of the three return-shaped positioning angles of the QR code, the position of each positioning angle is determined; An affine transformation matrix is ​​obtained according to the position of each positioning angle, and the binary image is corrected using the affine transformation matrix; Use the preset recognition algorithm to recognize the corrected image. If the recognition is successful, the recognition content and positioning parameters are returned. Otherwise, the recognition failure is returned. Use the binary image to extract the contour and determine the three return angles of the QR code, including: Perform contour extraction on the binary image, and use the contour to screen the three return-shaped positioning angles. If the return-shaped positioning angles cannot be screened, update the segmentation threshold in the preset threshold segmentation algorithm to perform contour extraction until the three return-shaped positioning angles are extracted; The three return positioning angles are screened by using the contour, and the contour screening steps include: Traverse the contours and find all contours that have two levels of parent contours; Filter out qualified contours by contour area and aspect ratio; Finally, all the selected contours are sorted from large to small according to the rectangularity, and the first three contours are taken as the QR code positioning contours; Where: rectangularity = contour area / minimum outer contour area; If the return positioning angle cannot be screened, the segmentation threshold in the preset threshold segmentation algorithm is updated to perform contour extraction until three return positioning angles are extracted, including: Using the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area, the segmentation threshold is updated, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is: newThred = otusValue - (otusValue - meanVal1) / 2; Perform morphological closing operation on the binary image, then extract the contour, traverse the contour again, and locate the three positioning angle contours; If the positioning fails, the maximum inter-class variance threshold outsValue and the average pixel value meanVal1 of the target area are used to update the segmentation threshold, and the grayscale image is re-segmented to obtain a new binary image. The updated threshold newThred is: newThred = otusValue + (meanVal2 - otusValue) / 2; Perform morphological opening operation on the binary image, then extract the contour, traverse the contour again, and locate the contours of the three return positioning angles.

7. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a two-dimensional code recognition method program under complex environmental conditions. When the two-dimensional code recognition method program under complex environmental conditions is executed by a processor, the steps of the two-dimensional code recognition method under complex environmental conditions as described in any one of claims 1 to 4 are implemented.

Citation Information

Patent Citations

  • Method for positioning region of interest

    CN104331697A

  • Application of area-of-interest positioning method to instrument monitoring identification

    CN104361353A

  • Fuzzy QR code restoration method based on affine transformation

    CN106485182A

  • Two-dimensional code recognition method, device and system

    CN108959998A

  • Two-dimensional code recognition method, storage medium and computer

    CN114692661A