Two-dimensional code position detection method and device

By performing binarization and morphological operations on the QR code image, connecting dot-like elements and calculating the connectivity domain, the problem of inaccurate detection of dot-like QR code positions in the prior art is solved, and high-precision QR code position detection is achieved.

CN120181111APending Publication Date: 2025-06-20GOERTEK INC
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510227773.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-27
Publication Date
2025-06-20

AI Technical Summary

Technical Problem

The existing edge detection methods cannot accurately detect the position of the dot-shaped QR code, which affects the position judgment results of the QR code.

Method used

A QR code position detection method is proposed, including binary processing of the QR code image to be identified, morphological operations (such as corrosion and expansion) to connect dot-like elements, and filter out the target communication area through the connection domain calculation, and then calculate the position of the QR code relative to the product.

Benefits of technology

Connect dot-shaped elements into a connecting area through morphological operations, avoiding the limitations of traditional edge detection methods, accurately extracting the boundaries of dot-shaped QR codes, and improving the accuracy of position judgment.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120181111A_ABST
    Figure CN120181111A_ABST
Patent Text Reader

Abstract

The invention discloses a two-dimensional code position detection method and device, and relates to the technical field of image processing, and the two-dimensional code position detection method comprises the steps: obtaining a to-be-recognized two-dimensional code image; binarization processing is carried out on the two-dimensional code image, and a binary image is obtained; performing morphological operation on the binary image, connecting dotted elements in the binary image, and obtaining a morphological processing image; performing connected domain calculation on the morphological processing image, and screening out a target connected region according to a connected domain calculation result; and calculating the position of the two-dimensional code relative to the product according to the target connected region. According to the two-dimensional code position detection method and device provided by the invention, the boundary of the point-shaped two-dimensional code can be accurately extracted, and the accuracy of judging the position of the point-shaped two-dimensional code is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of image processing, and particularly relates to a method and device for detecting the position of a two-dimensional code. Background Art

[0002] In modern industrial production, in order to ensure the traceability of product information and improve management efficiency, many products need to mark two-dimensional codes on their surfaces. These two-dimensional codes not only carry the basic information of the products, but may also include important data such as production batches and dates. The position accuracy of the two-dimensional code on the product surface directly affects the appearance quality of the product and the recognition accuracy during the subsequent two-dimensional code scanning process.

[0003] With the rapid development of machine vision technology, by installing an industrial camera above the products on the production line, the image of the two-dimensional code area can be obtained in real time, and advanced image processing algorithms can be used to detect the position of the two-dimensional code. For a two-dimensional code with a regular shape, such as a square two-dimensional code, whose left and lower boundaries are continuous straight lines, the edge detection method is used to extract the boundary of the two-dimensional code, and then the distance between it and the characteristic edge of the product is calculated to determine whether the position of the two-dimensional code is accurate. This method greatly improves the detection efficiency and accuracy, making the industrial production process more efficient and reliable.

[0004] When the product size is small, the size of the two-dimensional code marked on the product surface is also small, and the dot-shaped two-dimensional code can improve the recognition rate, so this small dot-shaped two-dimensional code is widely used in small-sized products. However, the boundary of the dot-shaped two-dimensional code is not a continuous straight line, and the existing edge detection method often cannot accurately extract the boundary of the two-dimensional code, thus affecting the final position judgment result of the two-dimensional code. Summary of the Invention

[0005] The main object of the present invention is to propose a method and device for detecting the position of a two-dimensional code, aiming to solve the problem that the existing edge detection method cannot accurately detect the position of the dot-shaped two-dimensional code.

[0006] To achieve the above object, the method for detecting the position of a two-dimensional code proposed by the present invention includes the steps of:

[0007] Obtain the two-dimensional code image to be recognized;

[0008] Perform binarization processing on the two-dimensional code image to obtain a binary image;

[0009] Perform morphological operations on the binary image to connect the dot-shaped elements in the binary image and obtain a morphologically processed image;

[0010] Perform connected component calculation on the morphologically processed image, and according to the connected component calculation result, screen out the target connected region;

[0011] Calculate the position of the two-dimensional code relative to the product according to the target connected region.

[0012] In an embodiment of the present invention, the step of performing morphological operations on the binary image includes:

[0013] Perform an erosion operation on the binary image using a target erosion structure;

[0014] Perform a dilation operation on the eroded binary image using a target dilation structure.

[0015] In an embodiment of the present invention, the step of performing an erosion operation on the binary image using a target erosion structure includes:

[0016] Perform erosion on the binary image using a predefined erosion structure;

[0017] If there are noise elements in the eroded binary image, increase the size of the erosion structure and re-erosion the binary image using the enlarged erosion structure;

[0018] When all the noise elements at least at the boundary of the two-dimensional code in the binary image are removed, set the erosion structure used at this time as the target erosion structure.

[0019] In an embodiment of the present invention, the step of performing a dilation operation on the eroded binary image using a target dilation structure includes:

[0020] Perform the dilation on the binary image using a predefined dilation structure;

[0021] If there are gaps between adjacent dot-like elements at the boundary of the two-dimensional code in the dilated binary image, increase the size of the dilation structure and re-dilate the binary image using the enlarged dilation structure;

[0022] When adjacent dot-like elements at least at the boundary of the two-dimensional code in the binary image are connected, set the dilation structure used at this time as the target dilation structure.

[0023] In an embodiment of the present invention, the step of performing connected component calculation on the morphologically processed image and screening out the target connected region according to the connected component calculation result includes:

[0024] Perform connected component calculation on the morphologically processed image and segment the morphologically processed image into multiple connected regions;

[0025] Compare the areas of the connected regions and screen out the connected region with the largest area;

[0026] Set the connected region with the largest area as the target connected region.

[0027] In an embodiment of the present invention, the step of obtaining the two-dimensional code image to be recognized includes:

[0028] Obtain an initial image within the field of view of the camera;

[0029] Crop the image within a preset area of the initial image to obtain the two-dimensional code image to be recognized.

[0030] In an embodiment of the present invention, the step of cropping the image within a preset area of the initial image to obtain the two-dimensional code image to be recognized includes:

[0031] Establish a first coordinate system;

[0032] Determine the first coordinate range of the two-dimensional code in the product in the first coordinate system;

[0033] In the first coordinate system, expand the first coordinate range along the periphery of the two-dimensional code to obtain a second coordinate range;

[0034] Set the area of the initial image located within the second coordinate range as the preset area, and crop the image within the preset area of the initial image to obtain the two-dimensional code image to be recognized.

[0035] In an embodiment of the present invention, the step of calculating the position of the two-dimensional code relative to the product according to the target connected region includes:

[0036] Draw the minimum bounding rectangle of the target connected region;

[0037] Calculate the position of the two-dimensional code relative to the product according to the minimum bounding rectangle.

[0038] In an embodiment of the present invention, the step of calculating the two-dimensional code image according to the minimum bounding rectangle includes:

[0039] Establish a first coordinate system;

[0040] Determine the relative coordinates of the product and the minimum bounding rectangle in the first coordinate system to obtain the position of the two-dimensional code on the product.

[0041] In an embodiment of the present invention, the step of determining the relative coordinates of the product and the minimum bounding rectangle in the first coordinate system to obtain the position of the two-dimensional code on the product includes:

[0042] Set the side of the product surface parallel to the side of the minimum bounding rectangle as the feature side;

[0043] Determine the feature edge position information of the feature edge in the first coordinate system;

[0044] Determine the side position information of the minimum circumscribed rectangle side in the first coordinate system;

[0045] According to the feature edge position information and the side position information, obtain the distance between the feature edge and the side.

[0046] The present invention also provides a two-dimensional code position detection device, which includes an image recognition module, a binarization processing module, a morphological processing module, a screening module, and a data processing module that are communicatively connected;

[0047] The image recognition module collects and obtains the two-dimensional code image to be recognized on the product surface;

[0048] The binarization processing module performs binarization processing on the two-dimensional code image to be recognized to obtain a binary image;

[0049] The morphological processing module performs morphological operations on the binary image to connect the dot elements in the binary image and obtain a morphologically processed image;

[0050] The screening module calculates the connected domain area of the morphologically processed image and screens out the target connected area;

[0051] The data processing module calculates the position of the two-dimensional code relative to the product according to the target connected area.

[0052] In an embodiment of the present invention, the morphological processing module includes an erosion module and a dilation module;

[0053] The erosion module performs an erosion operation on the binary image, and the dilation module performs a dilation operation on the eroded binary image to connect at least adjacent dot elements located at the boundary in the binary image.

[0054] The QR code position detection method proposed by the present invention is used to detect and determine the position of the QR code on the product surface. When performing position detection, first obtain the QR code image to be recognized on the product surface, and then perform binarization processing on the obtained QR code image to be recognized to convert the pixels in the QR code image to be recognized into a binary image of foreground (white) and background (black). Then perform morphological operations on the binary image, such as erosion operation and dilation operation, to connect the dot elements in the binary image and obtain a morphologically processed image. The morphologically processed image contains multiple connected regions. By performing connected component calculation on the morphologically processed image and according to the connected component calculation result, the target connected region is screened out. The target connected region contains the dot elements on the outermost edge of the dot QR code. Since the dot elements in the target connected region are connected, the boundary of the target connected region can be extracted by the edge detection method, and then the position of the QR code relative to the product can be obtained. In addition, the outer boundary of the dot QR code can be obtained by drawing the minimum bounding rectangle of the target connected region. Finally, by calculating the distance from the minimum bounding rectangle to a fixed point or edge of the product, the identification position of the QR code image on the product surface can be determined. Through the above QR code position detection method, the boundary of the dot QR code can be accurately extracted, improving the accuracy of judging the position of the dot QR code. BRIEF DESCRIPTION OF THE DRAWINGS

[0055] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on the structures shown in these drawings without creative efforts.

[0056] Figure 1 It is a flowchart of an embodiment of the QR code position detection method provided by the present invention;

[0057] Figure 2 For Figure 1 It is a flowchart of the steps of performing morphological operations on the binary image;

[0058] Figure 3 For Figure 2 It is a flowchart of the steps of performing erosion operation on the binary image;

[0059] Figure 4 For Figure 2 It is a flowchart of the steps of performing dilation operation on the eroded binary image;

[0060] Figure 5 For Figure 1Flow chart of the step of performing connected component calculation on the morphological processed image and screening the target connected region according to the calculation result of the connected component;

[0061] Figure 6 For Figure 1 Flow chart of the step of obtaining the two-dimensional code image to be recognized in [document];

[0062] Figure 7 For Figure 6 Flow chart of the step of cropping the image within the preset area of the initial image to obtain the two-dimensional code image to be recognized in [document];

[0063] Figure 8 For Figure 1 Flow chart of the step of calculating the position of the two-dimensional code relative to the product according to the target connected region in [document];

[0064] Figure 9 For Figure 8 Flow chart of the step of calculating the two-dimensional code image according to the minimum bounding rectangle in [document];

[0065] Figure 10 For Figure 9 Flow chart of the step of determining the relative coordinates of the product and the minimum bounding rectangle in the first coordinate system to obtain the position of the two-dimensional code on the product in [document];

[0066] Figure 11 Schematic diagram of the binary image obtained by binarizing the two-dimensional code image to be recognized;

[0067] Figure 12 Schematic diagram of the image after erosion operation on the binary image;

[0068] Figure 13 Schematic diagram of the image after dilation operation on the image after erosion operation;

[0069] Figure 14 Schematic diagram of the image after connected component calculation on the image after dilation operation;

[0070] Figure 15 Schematic diagram of the target connected region obtained after screening the image after connected component calculation;

[0071] Figure 16 Schematic diagram of the minimum bounding rectangle drawn based on the target connected region;

[0072] Figure 17 Schematic diagram of calculating the relative position of the two-dimensional code and the product according to the minimum bounding rectangle;

[0073] Figure 18 Schematic diagram of the module structure of the two-dimensional code position detection device of the present invention;

[0074] Figure 19 is Figure 18 a schematic diagram of the module structure of the morphological processing module in

[0075] Explanation of the reference numerals in the accompanying drawings:

[0076] 100, image recognition module; 200, binarization processing module; 300, morphological processing module; 400, screening module; 500, data processing module.

[0077] The realization, functional features and advantages of the object of the present invention will be further described with reference to the embodiments and the accompanying drawings. Specific embodiments

[0078] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative work shall fall within the protection scope of the present invention.

[0079] It should be noted that if there are directional indications (such as up, down, left, right, front, back...) involved in the embodiments of the present invention, the directional indications are only used to explain the relative position relationship and movement conditions between components in a specific posture. If the specific posture changes, the directional indications will also change accordingly.

[0080] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present invention, the descriptions of "first", "second", etc. are only for descriptive purposes and cannot be understood as indicating or implying their relative importance or implicitly indicating the quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include at least one of the features. In addition, if "and / or" or "and / or" appears throughout the text, its meaning includes three parallel solutions. Taking "A and / or B" as an example, it includes solution A, solution B, or a solution that satisfies both A and B at the same time. In addition, the technical solutions between the embodiments can be combined with each other, but it must be based on the fact that those of ordinary skill in the art can implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be considered that such a combination of technical solutions does not exist and is not within the protection scope required by the present invention.

[0081] The present invention provides a method for detecting the position of a two-dimensional code, which is used to detect a dot matrix two-dimensional code. The dot matrix two-dimensional code is composed of a plurality of dots arranged, and each dot is randomly distributed in a two-dimensional matrix. At the same time, a plurality of dots arranged adjacent to each other are arranged in the adjacent rows and columns on the outermost side of the matrix, that is, the outermost adjacent rows and columns are filled with dots. The method for detecting the position of the two-dimensional code proposed in this application can be applied to the position detection of a micro dot matrix two-dimensional code. Such a micro dot matrix two-dimensional code is very suitable for application on products with a small surface space (such as electronic components or precision parts) due to its small size (for example, the area is less than 1 square centimeter). In order to better mark the two-dimensional code on the surface of a product with limited space, the rectangular position detection patterns at the three corners of the micro dot matrix two-dimensional code are removed, thereby reducing the space volume occupied by the dot matrix two-dimensional code. In addition, when the size of the two-dimensional code is small, it is more difficult to implement a square structure than a circular structure in terms of technology. Therefore, there are no rectangular position detection patterns at the three corners of the dot matrix two-dimensional code. Since the boundary of the dot matrix two-dimensional code is not a continuous straight line and there are no position detection patterns at the three corners, it is impossible to quickly and accurately detect the position of the two-dimensional code through existing edge detection or other detection methods.

[0082] Combined with Figure 1 、 Figures 11 to 15 As shown, in an embodiment of the present invention, the method for detecting the position of the two-dimensional code includes the steps of:

[0083] S100: Obtain the two-dimensional code image to be recognized;

[0084] S200: Perform binarization processing on the two-dimensional code image to obtain a binary image;

[0085] S300: Perform morphological operations on the binary image to connect the dot elements in the binary image and obtain a morphologically processed image;

[0086] S400: Perform connected component calculation on the morphologically processed image, and filter out the target connected region according to the connected component calculation result;

[0087] S500: Calculate the position of the two-dimensional code relative to the product according to the target connected region.

[0088] In this embodiment, the two-dimensional code image to be recognized is obtained through an industrial camera or other image acquisition devices. The two-dimensional code image to be recognized contains a dot matrix two-dimensional code. Since the boundary of the dot matrix two-dimensional code is discontinuous, it is difficult to accurately extract its boundary by traditional edge detection methods.

[0089] Then, perform binarization processing on the obtained two-dimensional code image to be recognized, and convert the pixels in the two-dimensional code image to two colors, black and white. The specific steps are as follows:

[0090] If the QR code image to be recognized is in color, grayscale processing is first performed to convert the color image into a grayscale image. The method of grayscale processing can adopt the weighted average method (for example, calculate the grayscale value according to 0.299R + 0.587G + 0.114B for the RGB values).

[0091] Binarization processing: By setting a threshold, the grayscale image is converted into a binary image. For example, the Otsu algorithm is used to automatically determine the threshold, and the pixels with grayscale values greater than the threshold are set to 255 (white), and the pixels less than the threshold are set to 0 (black). Binarization processing simplifies the image information, highlights the outline and dot elements of the QR code, and facilitates subsequent morphological operations and connected component calculations.

[0092] Next, morphological operations are performed on the binary image. The purpose is to connect the discrete points in the dot QR code to form a connected region. Morphological operations include erosion operation and dilation operation. The erosion operation is used to remove the noise in the binary image, and the dilation operation is used to expand the dot elements of the QR code outward so that adjacent dot elements are connected. Through morphological operations, the discrete dot elements in the dot QR code are connected into a connected region, which is convenient for subsequent connected component calculations.

[0093] Then, each connected region is composed of a group of adjacent white pixels, and connected component calculation is performed on the image after morphological processing. Screening is performed according to the characteristics such as the area and shape of the connected region. Since the dot elements on the outermost adjacent rows and columns of the dot QR code are all adjacent, after performing connected component calculation on the image after morphological processing, the connected region containing the outermost adjacent rows and columns of the QR code is used as the target connected region. Since the dot elements in the target connected region are connected, the boundary of the target connected region can be extracted by edge detection, and then the position of the QR code relative to the product can be obtained. Of course, the area of each connected region can also be calculated, the regions with too small area (which may be noise) can be removed, and the connected region with the largest area can be set as the target connected region. Then, the minimum bounding rectangle of the target connected region is calculated, and according to the coordinates of the minimum bounding rectangle, the position of the QR code in the image is determined. For example, calculate the center point coordinates of the minimum bounding rectangle as the center position of the QR code, and calculate the distance between the center position and the characteristic edge of the product to judge whether the position of the QR code is correct. Of course, the distances between the four vertices of the minimum bounding rectangle and the characteristic edge of the product can also be calculated to judge whether the position of the QR code is correct.

[0094] The position detection method provided in this embodiment solves the problems of discontinuous boundaries and difficult accurate positioning of dot QR codes through steps such as binarization processing, morphological operations, connected component calculations, and position judgment. Compared with the prior art, this method has at least the following advantages:

[0095] By performing morphological operations to connect dot - like elements into connected regions, the limitations of traditional edge detection methods are avoided, and dot - like QR codes with small sizes can be detected to meet the detection requirements of QR codes with different sizes.

[0096] Through connected - component calculation and screening, noise and interference regions are effectively excluded, improving the reliability of detection.

[0097] Combined with Figure 2 、 Figure 12 and Figure 13 As shown in, in an embodiment of the present invention, the steps of performing morphological operations on a binary image include:

[0098] S310: Erode the binary image using a target erosion structuring element;

[0099] S320: Dilate the eroded binary image using a target dilation structuring element.

[0100] In this embodiment, first, the binary image is eroded. The target erosion structuring element used for the erosion operation can be in the shape of a circle, square, etc. In this embodiment, a circular structuring element is adopted to better match the dot - like elements of the QR code. By traversing each pixel in the binary image, the minimum value in the area covered by the target erosion structuring element is assigned to the central pixel. This operation shrinks the white areas (dot - like elements of the QR code) in the image, removes small noises and isolated dot - like elements in the image, and at the same time retains the main QR code structure. The effect of the erosion operation is to make the boundaries of the dot - like elements of the QR code clearer, remove the noise in the binary image, and lay a foundation for the subsequent dilation operation.

[0101] Next, the eroded binary image is dilated. The target dilation structuring element for the dilation operation is also circular or square. By traversing each pixel in the image, the maximum value in the area covered by the target dilation structuring element is assigned to the central pixel. This operation expands the white areas in the image, making adjacent dot - like elements connected together to form larger connected regions. The effect of the dilation operation is to connect adjacent dot - like elements and solve the problem of discontinuous boundaries of dot - like QR codes.

[0102] Because the boundaries of dot - like QR codes are discontinuous, traditional edge detection methods are difficult to effectively extract their boundaries. Through the operation sequence of erosion first and then dilation, this embodiment not only effectively removes the noise in the image, but also significantly enhances the connectivity of the QR code area, providing a clear input for the subsequent connected - component calculation.

[0103] In addition, the morphological operation sequence (erosion first and then dilation) of this embodiment also has the following advantages:

[0104] Noise removal: The erosion operation can effectively remove the fine noise in the image, avoiding noise interference with subsequent dilation operations to ensure clear boundaries of the dot matrix QR code after the dilation operation.

[0105] Boundary enhancement: The dilation operation can enhance the connectivity of the QR code area, connecting the dot elements on the boundary of the dot matrix QR code to make the boundary of the QR code clearer.

[0106] In other embodiments, when there are no noise elements or few noise elements outside the dot elements at the boundary where the dot matrix QR code is located, the dot elements can also be connected only by dilation.

[0107] Combined Figure 3 As shown, in an embodiment of the present invention, the steps of performing an erosion operation on a binary image using a target erosion structure include:

[0108] S311: Erode the binary image using a predefined erosion structure;

[0109] S312: If there are noise elements in the eroded binary image, increase the size of the erosion structure and re-erosion the binary image using the erosion structure with the increased size;

[0110] S313: When at least the noise elements at the QR code boundary in the binary image are completely removed, set the erosion structure used at this time as the target erosion structure.

[0111] In this embodiment, first, an erosion operation is performed on the binary image using a predefined erosion structure. The shape of the erosion structure is circular, and of course, it can also be square. The erosion operation traverses each pixel in the image and assigns the minimum value in the area covered by the structure to the central pixel. This operation shrinks the white area (dot elements of the QR code) in the image, removes the fine noise and isolated dot elements in the image, while retaining the main dot matrix structure of the QR code.

[0112] Since the size of the erosion structure affects the erosion radius of the erosion operation, an overly large erosion radius will cause dot elements to be incomplete, irregular or disappear, making it difficult to form a continuous boundary for the dot matrix QR code during subsequent dilation operations. An overly small erosion radius cannot effectively remove the noise in the image, and during subsequent dilation operations, the noise will cause the boundary of the QR code to be unclear or exceed the original range.

[0113] Therefore, before the erosion operation, erosion tests and validations are required, and the size of the erosion structure is dynamically adjusted according to the erosion results to change the erosion radius of the erosion operation. Specifically, it is achieved through the following steps:

[0114] Initial erosion operation: Perform an erosion operation on the binary image using an erosion structure of the initial size (pre-defined erosion structure), and observe the erosion effect.

[0115] Evaluate the erosion effect: Calculate the area change of the white region in the eroded image. If the noise elements are not completely removed after erosion, it indicates that the size of the erosion structure is small and needs to be increased. If the area of the white region of the dot elements decreases significantly after erosion, it indicates that the size of the erosion structure may be too large, resulting in over-erosion of the QR code region, and the size of the erosion structure needs to be reduced.

[0116] Adjust the size of the erosion structure: Gradually increase or decrease the size of the erosion structure according to the erosion effect.

[0117] Iterative optimization: Repeat the above steps until the erosion operation can effectively remove the noise elements outside the peripheral of the dot QR code while retaining the main structure of the QR code region.

[0118] When the noise elements located outside the peripheral of the dot elements in the binary image are removed, set the erosion structure used at this time as the target erosion structure. Using this target erosion structure to erode the binary image can achieve a better noise removal effect and avoid noise interference with subsequent dilation operations and connected component calculations. In addition, when batch erosion is required for the same batch of products, since the QR code noise of the same batch of products has little difference, this target erosion structure can also be used for the erosion operation of the QR code images to be recognized of other products, thereby improving the efficiency of the erosion operation.

[0119] Combined Figure 4 As shown, in an embodiment of the present invention, the steps of performing a dilation operation on the eroded binary image using the target dilation structure include:

[0120] S321: Dilate the binary image using a pre-defined dilation structure;

[0121] S322: If there is a gap between adjacent dot elements located at the QR code boundary in the dilated binary image, increase the size of the dilation structure and re-dilate the binary image using the dilation structure with the increased size;

[0122] S323: When at least adjacent dot elements located at the QR code boundary in the binary image are connected, set the dilation structure used at this time as the target dilation structure.

[0123] In this embodiment, first, a predefined dilation structuring element is used to perform a dilation operation on the eroded binary image. The shape of the dilation structuring element is circular, and of course, it can also be square. The dilation operation traverses each pixel in the image and assigns the maximum value within the area covered by the structuring element to the central pixel. This operation expands the white regions (dot-like elements of the QR code) in the image, causing adjacent dot-like elements to connect together to form larger connected regions.

[0124] Since the size of the dilation structuring element affects the dilation radius of the dilation operation, an overly large dilation radius will cause the connected region formed after connecting the dot-like elements to exceed the original QR code boundary, affecting the result of QR code position detection. An overly small dilation radius cannot connect the dot-like elements located at the image boundary, which will affect the size of the minimum bounding rectangle of the subsequent connected region, and thus affect the QR code position detection accuracy.

[0125] Therefore, before the dilation operation, dilation testing and verification are required, and the size of the dilation structuring element is dynamically adjusted according to the dilation result to change the dilation radius of the dilation operation. Specifically, it is achieved through the following steps:

[0126] Initial dilation operation: Use a dilation structuring element with an initial size (the predefined dilation structuring element) to perform a dilation operation on the eroded binary image and observe the dilation effect.

[0127] Evaluate the dilation effect: Calculate the area change of the white region in the dilated image. If the area of the white region increases after dilation, but the adjacent dot-like elements located at the QR code boundary are still not fully connected, it indicates that the size of the dilation structuring element is too small. If the size of the dilated dot-like elements significantly exceeds the size of the dot-like elements before erosion, and the overlapping area of two adjacent dot-like elements is too large, for example, exceeding 50%, it indicates that the size of the dilation structuring element is too large.

[0128] Adjust the size of the dilation structuring element: According to the dilation effect, gradually increase or decrease the size of the dilation structuring element.

[0129] Iterative optimization: Repeat the above steps until the dilation operation can effectively connect the adjacent dot-like elements located at the QR code boundary while avoiding excessive expansion of the QR code area.

[0130] When at least the adjacent dot-like elements located at the boundary in the binary image are connected, the corresponding dilation structuring element is set as the target dilation structuring element.

[0131] By dynamically adjusting the size of the dilation structure and obtaining the target dilation structure to ensure a good dilation effect, effectively connecting adjacent dot elements, solving the problem of discontinuous boundaries of dot QR codes, facilitating subsequent connected component calculation, and improving the QR code position detection accuracy. Additionally, when batch dilation is required for products of the same batch, since the QR code structures of products in the same batch have small differences, the target dilation structure can also be used for the dilation operation of the QR code images to be recognized of other products in the same batch, improving the efficiency of the dilation operation.

[0132] Combined with Figure 5 、 Figure 14 and Figure 15 As shown, in an embodiment of the present invention, in the step of performing connected component calculation on the morphological processing image and screening out the target connected region according to the connected component calculation result, it includes:

[0133] S410: Perform connected component calculation on the morphological processing image and segment the morphological processing image into multiple connected regions;

[0134] S420: Compare the areas of the connected regions and screen out the connected region with the largest area;

[0135] S430: Set the connected region with the largest area as the target connected region.

[0136] In this embodiment, first, perform connected component calculation on the image after morphological processing. The connected component calculation identifies and marks pixel regions with the same attributes (such as color, grayscale value, or texture) in the image through an algorithm based on pixel marking (such as the Two-Pass algorithm). In the image, if two pixel points can be connected by a series of pixel points with the same attributes (for example, all are white pixels), then these two pixel points belong to the same connected component.

[0137] After erosion and dilation of the binary image, multiple dot elements are connected. Therefore, through connected component calculation, the morphological processing image can be segmented into multiple connected regions, and each connected region is composed of a group of connected white pixels.

[0138] Next, compare the areas of the connected regions. In the dot QR code, most dot elements are adjacent, and a small number of dot elements are spaced from other dot elements. At the same time, at the outermost four boundaries of this dot QR code in this application, the dot elements on two adjacent boundaries are adjacent. Therefore, after connected component calculation, it is necessary to screen out the connected region with the largest area, and this connected region with the largest area is the target connected region.

[0139] In this embodiment, by comparing the areas of connected regions, the target connected region is quickly screened out without screening and judging the shapes of each connected region, avoiding complex feature analysis and calculations.

[0140] Combined with Figure 6 As shown, in an embodiment of the present invention, the steps of obtaining the two-dimensional code image to be recognized include:

[0141] S110: Obtain an initial image within the camera's field of view;

[0142] S120: Crop the image within a preset region of the initial image to obtain the two-dimensional code image to be recognized.

[0143] In this embodiment, the method of cropping the initial image obtained by the camera to obtain the two-dimensional code image to be recognized improves the efficiency of image processing and also reduces unnecessary computational amounts, and is particularly applicable to scenarios where the dot matrix two-dimensional code in industrial production is small in size and there are many complex backgrounds within the camera's field of view.

[0144] First, obtain an initial image within the camera's field of view. The initial image includes a dot matrix two-dimensional code, background noise, or other interfering elements. In order to reduce the computational amount of subsequent image processing and improve the accuracy of two-dimensional code position detection, it is necessary to crop the initial image to obtain the two-dimensional code image to be recognized within a preset region. The specific steps are as follows:

[0145] Definition of the preset region: According to the actual application scenario, define the position and size of the preset region. The preset region is located at the center of the camera's field of view. Of course, it can also be located around the camera's field of view. The size of the preset region is determined according to the size of the two-dimensional code and the camera resolution.

[0146] Image cropping: According to the preset region, crop the corresponding sub-image from the initial image. The cropped image only contains the content within the preset region, removing unnecessary backgrounds and interfering elements.

[0147] By cropping the preset region, the computational amount of image processing is reduced, and the detection efficiency is improved. The cropped image contains the dot matrix two-dimensional code within the preset region, removing background noise and other interfering elements, and improving the detection accuracy.

[0148] Combined with Figure 7 As shown, in an embodiment of the present invention, the steps of cropping the image within a preset region of the initial image to obtain the two-dimensional code image to be recognized include:

[0149] S121: Establish a first coordinate system;

[0150] S122: Determine the first coordinate range of the two-dimensional code in the product in the first coordinate system;

[0151] S123: In the first coordinate system, expand the first coordinate range around the four sides of the QR code to obtain the second coordinate range;

[0152] Set the area within the second coordinate range in the initial image as the preset area, and crop the image within the preset area of the initial image to obtain the QR code image to be recognized.

[0153] In this embodiment, in the detection of the QR code position of batch products, since the positions of different products entering the camera's field of view and the positions of the QR codes relative to the products will change, it is necessary to increase a certain detection range on the basis of the size range of the QR code to ensure that the QR code image to be recognized cropped from the initial image contains the complete QR code. Therefore, in this embodiment, it is necessary to determine how much to expand on the basis of the first coordinate range to obtain the second coordinate range. The specific steps are as follows:

[0154] First, provide a template product with a QR code. The position and size of the QR code on the template product are known and are used as a reference to determine the preset area. The template product can be a sample with relatively accurate QR code size and position in actual production, or a specially designed calibration tool.

[0155] Next, establish a first coordinate system with a fixed point within the camera's field of view as the origin. The fixed point can be the center point, boundary point, or other easily recognizable positions of the camera's field of view. The X-axis and Y-axis of the coordinate system respectively correspond to the horizontal and vertical directions of the camera's field of view.

[0156] Then, determine the first coordinate range of the QR code in the template product in the first coordinate system. For example, if the upper left corner coordinate of the QR code is (x1, y1) and the lower right corner coordinate is (x2, y2), then the first coordinate range is from (x1, y1) to (x2, y2).

[0157] Then, on the basis of the first coordinate range, expand a certain number of pixels (such as 50 - 500 pixels) around the four sides of the QR code, or expand the coordinate range according to a certain ratio (for example, 1 - 2 times the first coordinate range) to obtain the second coordinate range. For example, the expanded upper left corner coordinate is (x1 - 50, y1 - 50), and the lower right corner coordinate is (x2 + 50, y2 + 50). It can be understood that a smaller coordinate range will result in the inability to completely crop the QR code image; a larger coordinate range will affect the processing efficiency of the image.

[0158] Finally, set the area corresponding to the second coordinate range in the first coordinate system as the preset area. The preset area includes the QR code and a certain range around it, ensuring that the QR code is completely presented within the preset area.

[0159] By establishing a coordinate system and determining the coordinate range of the QR code, the preset area can be accurately located to ensure that the QR code is completely presented within the preset area. By expanding the first coordinate range, the preset area includes the QR code and a certain range around it, avoiding the risk of the QR code edge being cut and removing unnecessary background noise.

[0160] Combined with Figure 8 , Figure 16 and Figure 17 As shown, in an embodiment of the present invention, in the step of calculating the position of the QR code relative to the product according to the target connected region, it includes:

[0161] S510: Draw the minimum bounding rectangle of the target connected region;

[0162] S520: Calculate the position of the QR code relative to the product according to the minimum bounding rectangle.

[0163] In this embodiment, edge chain code tracking is performed on the filtered target connected region to determine its minimum bounding rectangle. Since the target connected region contains the dot-like elements of the outermost adjacent rows and columns in the dot-like QR code, the minimum bounding rectangle is tangent to the dot-like elements of the outermost adjacent rows and columns. By this method, clear and continuous lines can be formed, and the minimum bounding rectangle can represent the position of the dot-like QR code. When the minimum bounding rectangle is obtained, the position of the QR code can be obtained by establishing a coordinate system and calculating the distance between the minimum bounding rectangle and a certain feature point or feature edge of the product. Therefore, through the calculation method of this embodiment, the position of the QR code relative to the product can be obtained more accurately.

[0164] Combined with Figure 9 As shown, in an embodiment of the present invention, in the step of calculating the QR code image according to the minimum bounding rectangle, it includes:

[0165] S521: Establish a first coordinate system;

[0166] S522: Determine the relative coordinates of the product and the minimum bounding rectangle in the first coordinate system to obtain the position of the QR code on the product.

[0167] In this embodiment, first, a first coordinate system with a fixed point within the camera's field of view as the origin is established. The establishment of the first coordinate system can refer to the above embodiment and will not be elaborated further here.

[0168] Next, determine the relative coordinates of the product and the minimum bounding rectangle in the first coordinate system. The specific steps are as follows:

[0169] Locate the product: Locate the position of the product in the first coordinate system through image processing techniques (such as edge detection or template matching). The position of the product can be determined by its edge or feature points.

[0170] Locating the minimum bounding rectangle: When drawing the minimum bounding rectangle of the target connected region, the vertex coordinates of the minimum bounding rectangle in the first coordinate system can be determined.

[0171] Calculating relative coordinates: According to the positions of the product and the minimum bounding rectangle in the first coordinate system, calculate the coordinate offset of the minimum bounding rectangle relative to the product.

[0172] According to the calculated relative position of the QR code, it can be judged whether the marked position of the QR code meets the requirements, so as to facilitate subsequent screening, output the position information of the QR code on the product for subsequent production or inspection processes.

[0173] Combined with Figure 10 and Figure 17 As shown, in an embodiment of the present invention, in the step of determining the relative coordinates of the product and the minimum bounding rectangle in the first coordinate system to obtain the position of the QR code on the product, it includes:

[0174] S523: Set the edge of the product surface parallel to the side of the minimum bounding rectangle as the feature edge;

[0175] S524: Determine the feature edge position information of the feature edge in the first coordinate system;

[0176] S525: Determine the side position information of the side of the minimum bounding rectangle in the first coordinate system;

[0177] S526: According to the feature edge position information and the side position information, obtain the distance between the feature edge and the side.

[0178] In this embodiment, first, set the edge of the product surface parallel to the side of the minimum bounding rectangle as the feature edge. The feature edge is usually an obvious edge on the product surface, which is easy to identify and locate. For example, if the side of the minimum bounding rectangle is horizontal, select the horizontal edge of the product surface as the feature edge.

[0179] Next, determine the feature edge position information of the feature edge in the first coordinate system. The specific steps are as follows:

[0180] Locating the feature edge: Locate the position of the feature edge in the first coordinate system through image processing techniques (such as edge detection or Hough transform). The position of the feature edge can be represented by its starting point and ending point coordinates.

[0181] Calculating the feature edge position information: According to the starting point and ending point coordinates of the feature edge, calculate the inclination direction of the feature edge and obtain the equation of the feature edge.

[0182] Then, according to the two vertex coordinates of the side of the minimum bounding rectangle, calculate the inclination direction of the side and obtain the equation of the side.

[0183] Next, according to the equations of the feature edges and the side edges, the distance between the feature edges and the side edges can be calculated.

[0184] Finally, based on the distance between the feature edges and the side edges, it can be determined whether the position of the QR code on the product meets the requirements.

[0185] By setting the feature edges and calculating the distance between the feature edges and the side edges of the minimum circumscribed rectangle, the exact position of the QR code on the product is finally obtained. This method improves the accuracy of position calculation compared to calculating the distance between the fixed points of the product and the feature points of the minimum circumscribed rectangle, and can also determine whether there is a rotational offset of the QR code relative to the product.

[0186] The present invention also provides a QR code position detection device.

[0187] Combined with Figure 18 As shown in the figure, in an embodiment of the present invention, the QR code position detection device includes an image recognition module 100, a binarization processing module 200, a morphological processing module 300, a screening module 400, and a data processing module 500 that are communicatively connected;

[0188] The image recognition module 100 collects and obtains the QR code image to be recognized on the product surface;

[0189] The binarization processing module 200 performs binarization processing on the QR code image to be recognized to obtain a binary image;

[0190] The morphological processing module 300 performs morphological operations on the binary image to connect the dot elements in the binary image and obtain a morphologically processed image;

[0191] The screening module 400 calculates the area of the connected regions of the morphologically processed image and screens out the target connected regions;

[0192] The data processing module 500 draws the minimum circumscribed rectangle of the target connected region and calculates the position of the QR code image based on the minimum circumscribed rectangle.

[0193] In this embodiment, the device realizes the accurate position detection of the dot-shaped QR code on the product surface through the collaborative work of each module, and is applicable to scenarios that require efficient and accurate detection of the QR code position in industrial production.

[0194] The image recognition module 100 is responsible for collecting and obtaining the QR code image to be recognized on the product surface. Through an industrial camera or other image acquisition devices, the image of the product surface is obtained. The image may contain dot-shaped QR codes, background noise, or other interfering elements. The image recognition module 100 provides a high-quality input image for subsequent processing, ensuring the reliability of the detection process.

[0195] The binarization processing module 200 performs binarization processing on the two-dimensional code image to be recognized obtained by the image recognition module 100 to obtain a binary image. If the image is in color, it is first converted into a grayscale image. By setting a threshold, the grayscale image is converted into a binary image. For example, the Otsu algorithm is used to automatically determine the threshold, and the pixels with grayscale values greater than the threshold are set to white (255), and the pixels less than the threshold are set to black (0). The binarization processing simplifies the image information, highlights the outline and dot elements of the two-dimensional code, and facilitates subsequent morphological operations and connected component calculations.

[0196] The morphological processing module 300 performs morphological operations on the binary image to connect the dot elements in the binary image and obtain a morphologically processed image. Through morphological operations, the discrete points in the dot two-dimensional code are connected into connected regions, solving the problem of discontinuous boundaries of the dot two-dimensional code and providing a clear input for subsequent connected component calculations.

[0197] The screening module 400 calculates the area of the connected components of the morphologically processed image and screens out the target connected region. An algorithm based on pixel labeling (such as the Two-Pass algorithm) is used to calculate the connected regions in the image. Each connected region consists of a group of adjacent white pixels. Screening is performed according to the characteristics such as the area and shape of the connected regions. For example, the area of each connected region is calculated, the regions with too small area (which may be noise) are excluded, and the connected region with the largest area is screened out as the target connected region.

[0198] Through connected component calculation and screening, the two-dimensional code region is accurately recognized, noise and other interference regions are excluded, and the accuracy of position detection is improved.

[0199] The data processing module 500 calculates the position of the two-dimensional code relative to the product according to the target connected region obtained by the screening module 400. By calculating the minimum bounding rectangle of the target connected region or performing edge detection on the target connected region, the coordinate offset of the two-dimensional code relative to the product is calculated, and then the exact position of the two-dimensional code on the product is obtained.

[0200] Combined Figure 19 As shown, in an embodiment of the present invention, the morphological processing module 300 includes an erosion module and a dilation module;

[0201] The erosion module performs erosion operations on the binary image, and the dilation module performs dilation operations on the eroded binary image so that at least the adjacent dot elements located at the boundary in the binary image are connected.

[0202] In this embodiment, the erosion module is used to perform an erosion operation on the binary image. This operation shrinks the white areas (the dot elements of the QR code) in the image, removes the fine noises and isolated dot elements in the image, while preserving the main QR code structure.

[0203] The dilation module is used to perform a dilation operation on the eroded binary image. This operation expands the white areas in the image, causing adjacent dot elements to be connected together to form larger connected regions. By connecting adjacent dot elements, the dilation module enhances the connectivity of the QR code area, solves the problem of discontinuous boundaries of the dot QR code, and provides a clear input for subsequent connected component calculation.

[0204] The above description is only an exemplary embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent structural transformation made under the technical concept of the present invention by using the content of the specification and drawings of the present invention, or any direct / indirect application in other related technical fields is included in the patent protection scope of the present invention.

Claims

1. A two-dimensional code position detection method, characterized in that: Includes steps: Get the QR code image to be identified; Binarizing the two-dimensional code image to obtain a binary image; Performing a morphological operation on the binary image, connecting point elements in the binary image, and obtaining a morphologically processed image; Performing connected domain calculation on the morphologically processed image, and screening out target connected regions according to the connected domain calculation result; The position of the two-dimensional code relative to the product is calculated according to the target connected area.

2. The two-dimensional code position detection method according to claim 1, characterized in that: The step of performing morphological operation on the binary image comprises: Performing an erosion operation on the binary image using a target erosion structure; The target expansion structure is used to perform an expansion operation on the eroded binary image.

3. The two-dimensional code position detection method according to claim 2, characterized in that: The step of using the target corrosion structure to perform an corrosion operation on the binary image includes: Corroding the binary image using a predefined corrosion structure; If there are noise elements in the binary image after the erosion, the size of the erosion structure is increased, and the binary image is eroded again using the erosion structure with the increased size; When all noise elements in the binary image at least located at the boundary of the two-dimensional code are removed, the corrosion structure used at this time is set as the target corrosion structure.

4. The two-dimensional code position detection method according to claim 2, characterized in that: The step of using the target expansion structure to perform an expansion operation on the eroded binary image includes: dilating the binary image using a predefined dilation structure; If there are gaps between adjacent dot-shaped elements located at the boundary of the two-dimensional code in the binary image after expansion, increasing the size of the expansion structure, and using the expanded structure with the increased size to expand the binary image again; When the adjacent dot-shaped elements located at least at the boundary of the two-dimensional code in the binary image are connected, the expansion structure used at this time is set as the target expansion structure.

5. The two-dimensional code position detection method according to any one of claims 1 to 4, characterized in that: The step of performing connected domain calculation on the morphologically processed image and screening out target connected regions according to the connected domain calculation result comprises: Performing connected domain calculation on the morphologically processed image, and dividing the morphologically processed image into a plurality of connected regions; Comparing the areas of the connected regions, and selecting the connected region with the largest area; The connected region with the largest area is set as the target connected region.

6. The two-dimensional code position detection method according to any one of claims 1 to 4, characterized in that: The step of obtaining the two-dimensional code image to be identified includes: Get the initial image within the camera's field of view; The image within the preset area of ​​the initial image is cropped to obtain the two-dimensional code image to be identified.

7. The two-dimensional code position detection method according to claim 6, characterized in that: The step of cropping the image within the preset area of ​​the initial image to obtain the two-dimensional code image to be identified includes: Establish the first coordinate system; Determine a first coordinate range of the two-dimensional code in the product in the first coordinate system; In the first coordinate system, the first coordinate range is expanded along the four sides of the two-dimensional code to obtain a second coordinate range; The area in the second coordinate range of the initial image is set as a preset area, and the image in the preset area of ​​the initial image is cropped to obtain the two-dimensional code image to be identified.

8. The two-dimensional code position detection method according to any one of claims 1 to 4, characterized in that: The step of calculating the position of the two-dimensional code relative to the product according to the target connected area includes: Draw the minimum circumscribed rectangle of the target connected area; The position of the two-dimensional code relative to the product is calculated according to the minimum circumscribed rectangle.

9. The two-dimensional code position detection method according to claim 8, characterized in that: The step of calculating the two-dimensional code image according to the minimum circumscribed rectangle includes: Establish the first coordinate system; Determine the relative coordinates of the product and the minimum circumscribed rectangle in the first coordinate system to obtain the position of the two-dimensional code on the product.

10. The two-dimensional code position detection method according to claim 9, characterized in that: The step of determining the relative coordinates of the product and the minimum circumscribed rectangle in the first coordinate system to obtain the position of the two-dimensional code on the product includes: Setting the edge of the product surface parallel to the side of the minimum circumscribed rectangle as a characteristic edge; Determining feature edge position information of the feature edge in the first coordinate system; Determine side position information of the side of the minimum circumscribed rectangle in the first coordinate system; The distance between the feature edge and the side edge is obtained according to the feature edge position information and the side edge position information.

11. A two-dimensional code position detection device, characterized in that: It includes a communication-connected image recognition module, a binarization processing module, a morphological processing module, a screening module and a data processing module; The image recognition module collects and obtains the image of the two-dimensional code to be recognized on the surface of the product; The binarization processing module performs binarization processing on the two-dimensional code image to be identified to obtain a binary image; The morphological processing module performs a morphological operation on the binary image to connect point elements in the binary image and obtain a morphologically processed image; The screening module calculates the connected domain area of ​​the morphologically processed image and screens out the target connected area; The data processing module calculates the position of the two-dimensional code relative to the product according to the target connected area.

12. The two-dimensional code position detection device according to claim 11, characterized in that: The morphological processing module includes a corrosion module and a dilation module; The erosion module performs an erosion operation on the binary image, and the dilation module performs an dilation operation on the eroded binary image, so as to connect adjacent point elements at least at the boundary of the binary image.