A method and apparatus for detecting barcode defects

CN122335859BActive Publication Date: 2026-08-18HEFEI ARTIFICIAL INTELLIGENCE & BIG DATA RES INST CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202610779740.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-06-02
Publication Date
2026-08-18
Estimated Expiration
2046-06-02

AI Technical Summary

Technical Problem

[0005]本申请提供一种条码缺陷检测方法及装置,旨在解决现有条码标签缺陷检测中对微小缺陷和局部异常识别能力不足、对多种缺陷类型的检测适应性较差以及缺陷定位精度不高的技术问题

Benefits of technology

[0018] This application provides a barcode defect detection method and apparatus. The method acquires an image containing a barcode region to be detected, and first performs positioning and posture correction on the barcode region to obtain a target barcode image with uniform posture, thereby reducing interference from factors such as barcode tilt and positional offset on subsequent detection. Further, based on the grayscale anomaly information, barcode structural continuity information, and pixel distribution information of each column of the target barcode image, a first candidate defect region, a second candidate defect region, and a third candidate defect region are determined. Essentially, this characterizes barcode defects from three different dimensions: grayscale change, structural change, and column distribution change, thus improving the ability to identify minute defects, local anomalies, and different types of defects. Then, the first, second, and third candidate defect regions are merged to obtain a comprehensive defect region, and the barcode defect detection result is output based on this comprehensive defect region, thereby enabling the detection result to more comprehensively reflect the barcode defect situation and improve defect location accuracy. Therefore, the method provided in this application can solve the technical problems in related technologies such as insufficient ability to identify minute defects and local anomalies, poor adaptability to multiple defect types, and low defect location accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122335859B_ABST
    Figure CN122335859B_ABST
Patent Text Reader

Abstract

The application provides a bar code defect detection method and device, and relates to the technical field of bar code detection. The method comprises the following steps: acquiring a to-be-detected image containing a bar code region, positioning and correcting the posture of the bar code region to obtain a target bar code image; determining a first candidate defect region, a second candidate defect region and a third candidate defect region according to gray abnormal information, bar code structure continuity information and bar code column pixel distribution information of the target bar code image; performing merging processing on the first candidate defect region, the second candidate defect region and the third candidate defect region to obtain a comprehensive defect region; and outputting a bar code defect detection result based on the comprehensive defect region. By using the method provided in the application, the recognition capability for micro defects, local abnormalities and various defect types can be improved, and the defect positioning precision can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of barcode inspection technology, and in particular to a method and apparatus for barcode defect detection. Background Technology

[0002] Barcode labels, as a common information identification carrier, are widely used in industrial manufacturing, logistics warehousing, pharmaceutical distribution, food packaging, and retail management. Barcode labels enable product coding, information traceability, sorting and identification, and process management; therefore, the readability of barcode labels directly affects production efficiency and the accuracy of information collection. In automated production and high-speed workflow scenarios, to ensure that barcode labels can be reliably identified by scanning equipment, it is usually necessary to inspect the image quality and surface condition of the barcode labels.

[0003] In related technologies, barcode label inspection typically employs manual sampling, rule-based image processing methods, and model-trained intelligent detection methods. Manual sampling relies on human experience and judgment, resulting in low efficiency, poor consistency, and difficulty adapting to continuous online inspection. While model-trained detection methods can improve recognition capabilities in complex scenarios, they usually require a large number of samples and often necessitate parameter adjustments or readjustment when label materials, printing processes, acquisition environments, or defect morphologies change. Meanwhile, image analysis-based inspection methods are still widely used in industrial settings, performing grayscale processing, threshold segmentation, region analysis, and feature judgment on acquired barcode images to detect abnormal areas. However, in practical applications, barcode labels can be affected by various factors such as printing fluctuations, uneven ink adhesion, surface friction, adhesion deviations, background interference, and lighting changes, leading to various defects such as breakage, missing prints, stains, blurring, and scratches. Existing detection methods still suffer from instability, insufficient adaptability, and inaccurate positioning under complex conditions.

[0004] Therefore, in the defect detection of barcode labels, the insufficient ability to identify minor defects and local anomalies, the poor adaptability to the detection of multiple defect types, and the low accuracy of defect location have become technical problems that urgently need to be solved. Summary of the Invention

[0005] This application provides a barcode defect detection method and apparatus, aiming to solve the technical problems of insufficient ability to identify minor defects and local anomalies, poor adaptability to the detection of multiple defect types, and low defect location accuracy in existing barcode label defect detection.

[0006] In a first aspect, this application provides a barcode defect detection method, the method comprising: Acquire an image to be detected, wherein the image to be detected contains a barcode area; The barcode region in the image to be detected is located and its orientation is corrected to obtain the target barcode image; Based on the grayscale anomaly information of the target barcode image, a first candidate defect region is determined; Based on the barcode structure continuity information of the target barcode image, a second candidate defect region is determined; Based on the pixel distribution information of each column of the target barcode image, a third candidate defect region is determined; The first candidate defect region, the second candidate defect region, and the third candidate defect region are merged to obtain a comprehensive defect region. Based on the comprehensive defect area, the barcode defect detection result is output.

[0007] In one possible design, the step of locating and correcting the pose of the barcode region in the image to be detected to obtain the target barcode image includes: Based on a preset minimum bounding rectangle size threshold, regions in the image to be detected are filtered to obtain candidate barcode regions; Determine the minimum bounding rectangle corresponding to the candidate barcode region; The attitude offset angle is determined based on the deflection angle of the minimum circumscribed rectangle relative to the horizontal reference direction. Based on the posture offset angle, the candidate barcode region is rotated and corrected to obtain the target barcode image.

[0008] In one possible design, determining the first candidate defect region based on the grayscale anomaly information of the target barcode image includes: The target barcode image is converted to grayscale to obtain a grayscale image; The grayscale image is subjected to a cap transform to obtain an abnormally enhanced image; Based on structural elements adapted to the barcode orientation, the abnormal enhancement image is subjected to orientation enhancement processing to obtain an orientation-enhanced image; The enhanced image is subjected to threshold segmentation to obtain a first candidate defect mask; The first candidate defect region is determined based on the first candidate defect mask.

[0009] In one possible design, determining the first candidate defect region based on the first candidate defect mask includes: Connectivity analysis is performed within the candidate range defined by the first candidate defect mask to obtain multiple first candidate connected regions; Based on the first preset area filtering condition, the first candidate connected regions that satisfy the first preset area filtering condition are removed from the plurality of first candidate connected regions to obtain the first target connected region; wherein, the first preset area filtering condition is that the area of ​​the first candidate connected region is less than the first minimum area threshold, or the area of ​​the first candidate connected region is greater than the first maximum area threshold. The first candidate defect region is determined based on the pixel coordinates of the first target connected region.

[0010] In one possible design, determining the second candidate defect region based on the barcode structure continuity information of the target barcode image includes: The target barcode image is converted to grayscale to obtain a grayscale image; The grayscale image is subjected to a first binarization and inversion process to obtain an inverted binary image; Based on the vertical structural elements, the inverted binary image is morphologically processed to obtain the main structure image of the barcode. Based on the vertical projection result of the barcode main structure image, potential barcode column regions are divided to obtain multiple potential barcode column regions; For each of the plurality of potential barcode column regions, determine the vertical missing region of the potential barcode column region; The second candidate defect region is determined based on the pixel coordinates of the vertical missing regions of the plurality of potential barcode column regions.

[0011] In one possible design, determining the vertical missing region of each of the plurality of potential barcode column regions includes: For each potential barcode column region, determine the effective vertical range of the potential barcode column region within the barcode region to obtain the target vertical interval; Based on the target vertical interval, construct the theoretical complete mask corresponding to the potential barcode column region; Based on the actual foreground pixel regions within the potential barcode column region, the actual existing mask is obtained; Based on the difference between the theoretically complete mask and the actual existing mask, the vertical missing area of ​​the potential barcode column region is determined.

[0012] In one possible design, determining the third candidate defect region based on the pixel distribution information of each column of the target barcode image includes: The target barcode image is converted to grayscale to obtain a grayscale image; The grayscale image is subjected to a second binarization process to obtain a statistical binary image; The number of black pixels in each column of the statistical binary image is counted to obtain the column pixel distribution statistics. Based on a preset threshold for the number of black pixels, the statistical results of the pixel distribution in the column are filtered to obtain potential defect columns; Based on the potential defect list, a second candidate defect mask is constructed; The third candidate defect region is determined based on the second candidate defect mask.

[0013] In one possible design, determining the third candidate defect region based on the second candidate defect mask includes: Connectivity analysis is performed within the candidate range defined by the second candidate defect mask to obtain multiple second candidate connected regions; Based on the second preset area filtering condition, the second candidate connected regions that satisfy the second preset area filtering condition are removed from the plurality of second candidate connected regions to obtain the second target connected region; wherein, the second preset area filtering condition is that the area of ​​the second candidate connected region is less than the second minimum area threshold, or the area of ​​the second candidate connected region is greater than the second maximum area threshold; The third candidate defect region is determined based on the pixel coordinates of the second target connected region.

[0014] In one possible design, the step of outputting barcode defect detection results based on the comprehensive defect area includes: The defect pixels in the comprehensive defect region are marked to obtain the defect annotation result; The defect annotation results are superimposed onto the target barcode image to obtain a defect-annotated image; The number of defective pixels in the comprehensive defect area is counted to obtain the defect statistics results; Based on the defect-annotated image and / or the defect statistics, the barcode defect detection result is output.

[0015] Secondly, this application provides a barcode defect detection device, the device comprising: An image acquisition module is used to acquire an image to be detected, wherein the image to be detected includes a barcode area; The correction module is used to locate and correct the posture of the barcode region in the image to be detected, so as to obtain the target barcode image; The first defect detection module is used to determine the first candidate defect region based on the grayscale anomaly information of the target barcode image; The second defect detection module is used to determine the second candidate defect region based on the barcode structure continuity information of the target barcode image; The third defect detection module is used to determine the third candidate defect region based on the pixel distribution information of each column of the target barcode image. The merging module is used to merge the first candidate defect region, the second candidate defect region and the third candidate defect region to obtain a comprehensive defect region. The output module is used to output the barcode defect detection results based on the comprehensive defect area.

[0016] Thirdly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed, implement the method described in the first aspect or various possible designs of the first aspect.

[0017] Fourthly, this application provides a computer program product, which includes computer program code that, when run on a computer, causes the computer to implement the method described in the first aspect or various possible designs of the first aspect.

[0018] This application provides a barcode defect detection method and apparatus. The method acquires an image containing a barcode region to be detected, and first performs positioning and posture correction on the barcode region to obtain a target barcode image with uniform posture, thereby reducing interference from factors such as barcode tilt and positional offset on subsequent detection. Further, based on the grayscale anomaly information, barcode structural continuity information, and pixel distribution information of each column of the target barcode image, a first candidate defect region, a second candidate defect region, and a third candidate defect region are determined. Essentially, this characterizes barcode defects from three different dimensions: grayscale change, structural change, and column distribution change, thus improving the ability to identify minute defects, local anomalies, and different types of defects. Then, the first, second, and third candidate defect regions are merged to obtain a comprehensive defect region, and the barcode defect detection result is output based on this comprehensive defect region, thereby enabling the detection result to more comprehensively reflect the barcode defect situation and improve defect location accuracy. Therefore, the method provided in this application can solve the technical problems in related technologies such as insufficient ability to identify minute defects and local anomalies, poor adaptability to multiple defect types, and low defect location accuracy. Attached Figure Description

[0019] Figure 1 A flowchart illustrating a barcode defect detection method provided in an embodiment of this application; Figure 2 A visual example of a barcode defect detection method provided in this application embodiment; Figure 3This is a schematic diagram of the structure of a barcode defect detection device provided in an embodiment of this application; Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0020] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0021] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein in the specification of the application is for the purpose of describing particular embodiments only and is not intended to limit the application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims and drawings of this application are intended to cover non-exclusive inclusion.

[0022] The term "embodiment" as used herein means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of the phrase "embodiment" in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.

[0023] In this article, the term "and / or" simply describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can mean: A exists, A and B can exist simultaneously, and B exists. Additionally, the character " / " in this article generally indicates that the preceding and following related objects have an "or" relationship.

[0024] Furthermore, the terms "first," "second," etc., in the specification and claims of this application or in the aforementioned drawings are used to distinguish different objects rather than to describe a specific order, and may explicitly or implicitly include one or more of the features.

[0025] In the description of this application, unless otherwise stated, "multiple" and "at least two" mean two or more (including two), and similarly, "multiple groups" and "at least two groups" mean two or more (including two groups).

[0026] In the description of this application, it should be noted that, unless otherwise explicitly specified and limited, the terms "connected" and "linked" should be interpreted broadly. For example, "connected" or "linked" can refer not only to a physical connection, but also to an electrical connection or a signal connection. For instance, it can be a direct connection, i.e., a physical connection, or an indirect connection through at least one intermediate component, as long as the circuit is connected. It can also refer to the internal connection between two components. A signal connection can refer not only to a signal connection through a circuit, but also to a signal connection through a medium, such as radio waves. Those skilled in the art can understand the specific meaning of the above terms in this application based on the specific circumstances.

[0027] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. It should be noted that, unless otherwise specified, different technical features in this application can be combined with each other.

[0028] Barcode labels, as crucial information carriers in industrial production, logistics warehousing, product traceability, and commodity circulation, are widely used in electronics manufacturing, food packaging, pharmaceuticals, and fast-moving consumer goods. The printing quality and surface imaging condition of barcode labels directly affect the accuracy and stability of barcode scanning equipment. Defects such as breaks, missing ink, stains, scratches, blurriness, or background contamination can easily lead to recognition failures, misreads, or missed reads, thus impacting production cycle time, sorting efficiency, and the reliability of product flow. Therefore, efficient and accurate defect detection of barcode labels is a critical issue in industrial automation inspection.

[0029] In actual production and distribution processes, barcode labels are prone to various forms of defects due to factors such as aging printing equipment, uneven ink distribution, paper quality fluctuations, label misalignment, and transportation friction. These defects are typically characterized by high randomness, diverse shapes, significant scale differences, and indistinct local features, increasing the difficulty of automated detection. Existing barcode label detection methods include manual sampling, rule-based detection, and model-based detection. Manual sampling relies on human experience, resulting in low efficiency, poor consistency, and difficulty adapting to continuous online detection. Model-based detection methods typically rely on a large number of samples and training processes, leading to high application costs, and their adaptability needs improvement when barcode styles, printing processes, or usage environments change. Overall, existing detection methods may still suffer from insufficient identification of local anomalies, limited adaptability to multiple defect types, and low precision of detection results under complex operating conditions, making it difficult to fully meet the accuracy and stability requirements of industrial quality inspection scenarios.

[0030] Based on this, this application provides a barcode defect detection method and apparatus. The method first acquires an image to be detected containing a barcode region, and then performs positioning and orientation correction on the barcode region to obtain a target barcode image. Next, candidate defect regions are extracted from different information dimensions based on the target barcode image. Finally, multiple candidate defect regions are merged to obtain a comprehensive defect region, and the barcode defect detection result is output. The method provided in this application can improve the detection capability of barcode defects, enhance the accuracy and refinement of the detection results, and thus better meet the barcode quality inspection needs of industrial sites.

[0031] Figure 1 This is a flowchart illustrating a barcode defect detection method provided in an embodiment of this application. Figure 1 As shown, the method provided in this application embodiment specifically includes S101 to S107, and S101 to S107 will be described in detail below.

[0032] It should be noted that the barcode defect detection method provided in this application embodiment can be executed by a barcode defect detection device or by an electronic device with the above-mentioned module functions, and this application does not limit it in this regard.

[0033] S101. Obtain the image to be detected, which contains a barcode area.

[0034] The image to be detected can be a label image acquired by an image acquisition device. The image acquisition device may include an industrial camera, an area scan camera, a line scan camera, an imaging unit in a barcode scanning module, a scanner, or other image acquisition devices capable of acquiring label images. This application does not limit the scope of the application.

[0035] The barcode area in the image to be inspected can be the entire barcode area on the label, or only a portion of the barcode area on the label, as long as the image contains the barcode information to be detected for defects.

[0036] Optionally, the image to be inspected can be an online image acquired in real time on the production line, or an offline image pre-stored in a database, local storage, or cloud storage medium. This application does not limit the acquisition method, image resolution, image color space, or image storage format of the image to be inspected.

[0037] For example, the image to be detected can be an RGB color image or a grayscale image; if the image to be detected is a color image, it can be converted to a grayscale image as needed in subsequent steps.

[0038] It should be noted that the barcode area in the image to be detected can be located anywhere in the image, and the orientation of the barcode is not predetermined. The image only needs to contain the barcode area to be detected.

[0039] S102. Locate and correct the orientation of the barcode region in the image to be detected to obtain the target barcode image.

[0040] First, regions in the image to be detected are filtered to obtain candidate barcode regions. Then, the orientation information of the candidate barcode regions is used for posture correction to obtain the target barcode image.

[0041] It should be noted that the candidate barcode region is an image region in the image to be detected that meets the preset conditions. The preset conditions may include region size conditions, region shape conditions, or other conditions used to characterize the barcode region, which are not limited in this application.

[0042] The region orientation information is used to characterize the offset of the candidate barcode region relative to the preset reference direction. The attitude correction includes rotating the candidate barcode region according to the region orientation information.

[0043] Specifically, the target barcode image is a barcode image after attitude correction, and the barcode area in the target barcode image is aligned with a preset reference direction.

[0044] For example, the preset reference direction can be a horizontal reference direction or other preset reference directions, which is not limited in this application.

[0045] S103. Based on the grayscale anomaly information of the target barcode image, determine the first candidate defect region.

[0046] Among them, grayscale anomaly information is used to characterize image information in the target barcode image that has abnormal grayscale changes compared with the normal barcode area. Grayscale anomalies can be caused by stains, scratches, missing ink, local blurring, or other factors that affect the barcode imaging quality, and this application does not limit them.

[0047] The first candidate defect region can be an image region in the target barcode image that deviates from the characteristics of a normal barcode region in terms of grayscale distribution.

[0048] Specifically, the target barcode image can be first converted to grayscale to obtain a grayscale image, and then anomaly enhancement processing can be performed based on the grayscale image to obtain an anomaly enhanced image. The first candidate defect region can then be determined based on the anomaly enhanced image.

[0049] Anomaly enhancement processing is used to enhance the difference between gray-scale abnormal regions and normal regions in the target barcode image, so as to facilitate the subsequent extraction of the first candidate defect region.

[0050] S104. Determine the second candidate defect region based on the barcode structure continuity information of the target barcode image.

[0051] Among them, the barcode structure continuity information is used to characterize the continuous distribution of the barcode structure in the target barcode image in a preset direction. Changes in the continuity of the barcode structure can be caused by breaks, missing prints, local blurring, or other factors that affect the integrity of the barcode structure, and this application does not limit this.

[0052] The second candidate defect region can be an image region in the target barcode image that has abnormal continuity of barcode structure.

[0053] Specifically, the target barcode image can be processed first to obtain the main structure image of the barcode. Then, potential barcode column regions can be divided based on the main structure image of the barcode. Vertical missing regions can be determined for the potential barcode column regions, and second candidate defect regions can be determined based on the vertical missing regions.

[0054] S105. Based on the pixel distribution information of each column of the target barcode image, determine the third candidate defect area.

[0055] The pixel distribution information of each column of the barcode is used to characterize the distribution of corresponding pixels in each column of the target barcode image. The pixel distribution change can be caused by dirt, dark scratches, partial occlusion or other factors that affect the barcode column distribution state, and this application does not limit this.

[0056] The third candidate defect region can be an image region in the target barcode image where the pixel distribution of each column of the barcode is abnormal.

[0057] Specifically, the target barcode image can be processed first to obtain a statistical binary image. Then, the number of black pixels in each column of the statistical binary image can be counted to obtain the column pixel distribution statistics. Based on the column pixel distribution statistics, the third candidate defect area can be determined.

[0058] S106. Merge the first candidate defect region, the second candidate defect region, and the third candidate defect region to obtain the comprehensive defect region.

[0059] The merging process includes integrating the regional information corresponding to the first candidate defect region, the second candidate defect region, and the third candidate defect region. The regional information can be pixel coordinate information, regional mask information, or regional contour information, and this application does not limit it.

[0060] It should be noted that the comprehensive defect area is the image region in the target barcode image that comprehensively represents abnormal grayscale, abnormal barcode structural continuity, and abnormal pixel distribution in each column of the barcode.

[0061] Specifically, the defect pixel coordinates corresponding to the first candidate defect region, the second candidate defect region, and the third candidate defect region can be obtained respectively, and the defect pixel coordinates can be merged to obtain a comprehensive defect coordinate set. Then, the comprehensive defect region can be determined based on the comprehensive defect coordinate set.

[0062] Optionally, when there are overlapping defect pixels in the first candidate defect region, the second candidate defect region, and the third candidate defect region, the overlapping defect pixels are deduplicated; when there are adjacent or connected defect regions in the first candidate defect region, the second candidate defect region, and the third candidate defect region, the adjacent or connected defect regions can be merged into the same comprehensive defect region, and this application does not limit this.

[0063] For example, when the first candidate defect region, the second candidate defect region, and the third candidate defect region are represented in the form of region masks, the corresponding region masks can be logically merged to obtain a comprehensive defect mask, and then the comprehensive defect region can be determined based on the comprehensive defect mask.

[0064] For example, when the first candidate defect region, the second candidate defect region, and the third candidate defect region are represented in the form of pixel coordinate sets, the corresponding pixel coordinate sets can be combined to obtain a comprehensive defect coordinate set, and then the comprehensive defect region can be determined based on the comprehensive defect coordinate set.

[0065] S107. Based on the comprehensive defect area, output the barcode defect detection results.

[0066] The barcode defect detection results are used to characterize the defect detection output information in the target barcode image. The barcode defect detection output information may include defect annotation results, defect statistics results, or other detection results corresponding to the comprehensive defect area. This application does not limit this.

[0067] Specifically, defect pixels in the comprehensive defect area can be marked to obtain defect annotation results, and then the defect annotation results can be superimposed on the target barcode image to obtain a defect annotation image; the number of defect pixels in the comprehensive defect area can be counted to obtain defect statistics results, and the barcode defect detection results can be output based on the defect annotation image and / or defect statistics results.

[0068] This application provides a barcode defect detection method. It acquires an image containing the barcode region to be detected, and first performs positioning and posture correction on the barcode region to obtain a target barcode image with uniform posture, thereby reducing interference from factors such as barcode tilt and positional offset on subsequent detection. Furthermore, based on the grayscale anomaly information, barcode structural continuity information, and pixel distribution information of each column of the target barcode image, a first candidate defect region, a second candidate defect region, and a third candidate defect region are determined. Essentially, this characterizes the barcode defect from three different dimensions: grayscale change, structural change, and column distribution change, thus improving the ability to identify minute defects, local anomalies, and different types of defects. Then, by merging the first, second, and third candidate defect regions, a comprehensive defect region is obtained, and the barcode defect detection result is output based on this comprehensive defect region. This allows the detection result to more comprehensively reflect the barcode defect situation and improve defect location accuracy. Therefore, the method provided in this application can solve the technical problems in related technologies, such as insufficient ability to identify minute defects and local anomalies, poor adaptability to multiple defect types, and low defect location accuracy.

[0069] In one possible embodiment, the method steps shown in S102 can be implemented by S1021 to S1024, which are described in detail below.

[0070] S1021. Based on the preset minimum bounding rectangle size threshold, the regions in the image to be detected are filtered to obtain candidate barcode regions.

[0071] The region in the image to be detected is one or more candidate image regions extracted from the image to be detected. The candidate image regions can be obtained by image segmentation, contour extraction, edge detection, connected component analysis or other region extraction methods, and this application does not limit them.

[0072] A preset minimum bounding rectangle size threshold is used to characterize the screening criteria for candidate regions in terms of size. The size threshold may include at least one of a minimum width threshold, a minimum height threshold, and a minimum area threshold, or it may be a combined threshold composed of width, height, and area. This application does not limit this.

[0073] The candidate barcode region is the image region retained in the image to be detected after being filtered by size criteria.

[0074] Specifically, the minimum bounding rectangle corresponding to each candidate region in the image to be detected can be determined first, and then the size information of each minimum bounding rectangle can be obtained. The size information of the minimum bounding rectangle corresponding to the candidate region can be compared with the preset minimum bounding rectangle size threshold. When the size information of the minimum bounding rectangle corresponding to a certain candidate region meets the preset minimum bounding rectangle size threshold, the candidate region is retained as a candidate barcode region.

[0075] Optionally, the size information may include at least one of the width, height, area, or aspect ratio of the minimum bounding rectangle.

[0076] For example, when the width of the minimum bounding rectangle corresponding to a candidate region is greater than or equal to a preset minimum width threshold and the height is greater than or equal to a preset minimum height threshold, the region can be retained as a candidate barcode region; or, for example, when the area of ​​the minimum bounding rectangle corresponding to a region is greater than or equal to a preset minimum area threshold, the region can also be retained as a candidate barcode region. This application does not limit this.

[0077] In this embodiment, regions that are more likely to contain the barcode subject can be selected from multiple regions in the image to be detected, providing a basis for subsequent determination of the minimum bounding rectangle and attitude correction.

[0078] S1022. Determine the minimum bounding rectangle corresponding to the candidate barcode area.

[0079] The minimum bounding rectangle is a relatively small rectangular region that can enclose the candidate barcode area. The minimum bounding rectangle can be used to characterize the overall spatial distribution and orientation information of the candidate barcode area.

[0080] Specifically, the boundary contour of the candidate barcode region can be extracted, and the minimum bounding rectangle surrounding the candidate barcode region can be determined based on the boundary contour. The boundary contour can be the outer contour of the foreground pixels in the candidate barcode region, or it can be the closed boundary of the entire candidate barcode region; this application does not limit it in this regard.

[0081] Optionally, when determining the minimum bounding rectangle corresponding to the candidate barcode area, at least one of the following information can be obtained: the center point coordinates, width, height, and direction angle of the rectangle's side, for subsequent determination of the attitude offset angle and rotation correction processing.

[0082] For example, when the candidate barcode area is slanted, the determined minimum bounding rectangle is also slanted accordingly; when the candidate barcode area is nearly horizontal, the side direction of the determined minimum bounding rectangle is close to the horizontal reference direction.

[0083] In this embodiment, by determining the minimum bounding rectangle corresponding to the candidate barcode region, information characterizing the directional state of the candidate barcode region can be further obtained.

[0084] S1023. Determine the attitude offset angle based on the deflection angle of the smallest circumscribed rectangle relative to the horizontal reference direction.

[0085] The horizontal reference direction is a pre-defined reference direction, which corresponds to the horizontal direction in the image coordinate system. The attitude offset angle is used to characterize the offset of the current direction of the minimum bounding rectangle relative to the horizontal reference direction.

[0086] Specifically, the angle between the target side of the minimum bounding rectangle and the horizontal reference direction can be obtained, and the angle can be determined as the attitude offset angle. The target side can be either the long side or the short side of the minimum bounding rectangle, and this application does not limit it in this regard.

[0087] Optionally, when the longer side of the smallest bounding rectangle better represents the overall extension direction of the candidate barcode area, the angle between the longer side and the horizontal reference direction can be selected as the attitude offset angle; when the shorter side of the smallest bounding rectangle better represents the barcode arrangement direction, the angle between the shorter side and the horizontal reference direction can also be selected as the attitude offset angle.

[0088] When the minimum bounding rectangle deflects clockwise relative to the horizontal reference direction, the attitude offset angle can be characterized as the corresponding clockwise offset angle; when the minimum bounding rectangle deflects counterclockwise relative to the horizontal reference direction, the attitude offset angle can be characterized as the corresponding counterclockwise offset angle.

[0089] In this embodiment, the attitude offset angle reflects the degree of deviation of the current attitude of the candidate barcode area from the preset reference direction. By determining the attitude offset angle, an angular basis can be provided for subsequent rotation correction.

[0090] S1024. Based on the attitude offset angle, perform rotation correction on the candidate barcode region to obtain the target barcode image.

[0091] Rotation correction involves rotating the candidate barcode region according to the attitude offset angle to align the candidate barcode region relative to the horizontal reference direction. The target barcode image is the rotated-corrected barcode image.

[0092] Specifically, the rotation angle can be determined based on the attitude offset angle, and the candidate barcode area can be rotated according to the rotation angle to obtain the rotated image area, and then the rotated image area can be used as the target barcode image.

[0093] Optionally, the rotation angle can be the opposite angle to the attitude offset angle to counteract the offset of the candidate barcode area relative to the horizontal reference direction.

[0094] For example, when the attitude offset angle represents the candidate barcode area being deflected clockwise relative to the horizontal reference direction, the candidate barcode area can be rotated and corrected by the corresponding counterclockwise rotation angle; when the attitude offset angle represents the candidate barcode area being deflected counterclockwise relative to the horizontal reference direction, the candidate barcode area can be rotated and corrected by the corresponding clockwise rotation angle.

[0095] Furthermore, after rotation correction, the rotated image area can be cropped to retain the image portion containing the complete barcode body, thus obtaining the target barcode image.

[0096] In this embodiment, candidate barcode regions are obtained by filtering regions in the image to be detected based on a preset minimum bounding rectangle size threshold. The minimum bounding rectangle corresponding to the candidate barcode regions is further determined, and the attitude offset angle is determined based on the deflection angle of the minimum bounding rectangle relative to the horizontal reference direction. Then, the candidate barcode regions are rotated and corrected based on the attitude offset angle to obtain the target barcode image. This allows for more accurate positioning of the barcode regions and completion of barcode attitude correction, reducing the impact of barcode tilt and region offset on subsequent defect detection and improving the stability and accuracy of subsequent defect detection.

[0097] In one possible embodiment, the method steps shown in S103 can be implemented by S1031 to S1035, which are described in detail below.

[0098] S1031. Perform grayscale processing on the target barcode image to obtain a grayscale image.

[0099] The target barcode image can be a color image or an image in other color spaces; this application does not limit this. Grayscale processing is used to convert the target barcode image into a single-channel grayscale image to facilitate subsequent analysis of grayscale anomalies in the barcode area. The grayscale image is an image that characterizes the brightness distribution of the target barcode image and can be used as the input image for subsequent cap transform processing.

[0100] Specifically, when the target barcode image is a color image, multiple color channel information in the target barcode image can be converted into grayscale information according to a preset grayscale conversion rule to obtain a grayscale image.

[0101] Optionally, the grayscale conversion rule can be a weighted summation, average calculation, or other conversion rule that can achieve grayscale conversion for each color channel, and this application does not limit this.

[0102] Optionally, during the grayscale processing, at least one of denoising processing, smoothing processing, or brightness equalization processing can be combined to improve the stability of the grayscale image.

[0103] S1032. Perform a cap transform on the grayscale image to obtain an abnormally enhanced image.

[0104] The hat transform process is used to enhance image regions in a grayscale image that exhibit abnormal brightness compared to their surroundings, thereby highlighting potentially defective areas. The enhanced image is the result of the hat transform; the abnormal regions in the enhanced image have higher distinguishability compared to the normal regions.

[0105] It should be noted that the hat transformation process may include top hat transformation, black hat transformation or other hat transformation methods, and this application does not limit it.

[0106] Specifically, based on the grayscale relationship between the abnormal region and the background region in the grayscale image, a corresponding cap transformation can be performed on the grayscale image to obtain an abnormal enhancement image.

[0107] Optionally, when the abnormal region exhibits a decrease in brightness relative to the surrounding region, a top-hat transform can be used for processing; when the abnormal region exhibits an increase in brightness relative to the surrounding region, a black-hat transform can be used for processing.

[0108] Furthermore, top-hat transformation and black-hat transformation can be performed separately, and one of the processing results or a combination of the two processing results can be selected according to actual needs. This application does not limit this.

[0109] S1033. Based on structural elements adapted to the barcode orientation, perform orientation enhancement processing on the abnormal enhancement image to obtain an orientation enhancement image.

[0110] Among them, the structural elements adapted to the barcode orientation are used to characterize the structural features that match the orientation of the barcode body.

[0111] The structural element can be a bar-shaped structural element, a rectangular structural element, or other structural elements adapted to the barcode orientation; this application does not limit this.

[0112] It should be noted that the orientation-enhanced image is the image obtained after orientation enhancement processing. Abnormal responses in the orientation-enhanced image that are consistent with the barcode orientation are enhanced, while interference responses that are inconsistent with the barcode orientation are suppressed.

[0113] Specifically, structural elements in the corresponding direction can be selected according to the arrangement direction of the barcodes in the target barcode image, and the structural elements can be used to perform directional enhancement processing on the abnormal enhancement image to obtain a directional enhancement image.

[0114] Optionally, when the barcode is arranged vertically in the target barcode image, vertical bar structure elements can be selected for processing; when the barcode is arranged in other preset directions in the target barcode image, structure elements in the corresponding directions can also be selected for processing. This application does not limit this.

[0115] Optionally, the size of the structural element can be set according to the barcode line width, barcode spacing, image resolution, or defect scale, which is not limited in this application.

[0116] S1034. Threshold segmentation is performed on the directional enhancement image to obtain the first candidate defect mask.

[0117] Thresholding segmentation is used to distinguish abnormal response regions from non-abnormal response regions based on the differences in pixel values ​​in the directional enhancement image.

[0118] Threshold segmentation can be performed using fixed threshold segmentation, adaptive threshold segmentation, or other threshold segmentation methods, and this application does not limit this to any particular method.

[0119] Specifically, the pixel values ​​in the orientation-enhanced image can be compared with a preset segmentation threshold, and based on the comparison results, it can be determined whether each pixel belongs to a candidate defect pixel, thereby obtaining the first candidate defect mask. The first candidate defect mask is a mask image that characterizes the distribution of candidate defect pixels in the orientation-enhanced image.

[0120] The first candidate defect mask may include a first type of pixel value representing the candidate defect pixel and a second type of pixel value representing the non-defect pixel; this application does not limit this.

[0121] Optionally, after obtaining the first candidate defect mask, at least one of the following post-processing methods can be applied to the first candidate defect mask: hole filling, edge smoothing, or isolated noise removal, in order to improve the integrity and stability of the first candidate defect mask. This application does not limit this.

[0122] S1035. Determine the first candidate defect region based on the first candidate defect mask.

[0123] Specifically, the image region corresponding to the candidate defect pixel in the first candidate defect mask can be determined as the first candidate defect region. The first candidate defect region is the candidate defect image region in the target barcode image that corresponds to the grayscale anomaly information.

[0124] In this embodiment of the application, by enhancing the grayscale anomalies in the target barcode image and extracting the corresponding candidate defect regions, weak anomalies caused by factors such as stains, scratches, missing ink, and local blurring in the barcode region can be more effectively separated from the background and normal barcode texture, thereby improving the detection capability of local anomalies and minor defects, and providing more reliable candidate results for further screening and localization of subsequent defect regions.

[0125] In one possible embodiment, the method steps shown in S1035 can be implemented by Sa1 to Sa3, which are described in detail below.

[0126] Sa1. Perform connected component analysis within the candidate range defined by the first candidate defect mask to obtain multiple first candidate connected regions.

[0127] The candidate range defined by the first candidate defect mask is the image range within the first candidate defect mask that represents the distribution of candidate defect pixels. Connected component analysis is used to aggregate spatially connected candidate defect pixels within the candidate range to form multiple distinct connected regions.

[0128] Specifically, based on a preset connectivity rule, the candidate defect pixels in the first candidate defect mask can be traversed, and interconnected candidate defect pixels can be divided into the same connected region, thereby obtaining multiple first candidate connected regions. The preset connectivity rule can be a 4-neighborhood connectivity rule, an 8-neighborhood connectivity rule, or other pixel connectivity determination rules, and this application does not limit it in this regard.

[0129] Optionally, each of the multiple first candidate connected regions can correspond to a set of continuously distributed candidate defect pixels.

[0130] For example, when there are multiple separate candidate defect pixel clusters in the first candidate defect mask, each candidate defect pixel cluster can be divided into different first candidate connected regions through connected component analysis. This application does not limit this.

[0131] Multiple first candidate connected regions are multiple connected pixel regions obtained within the candidate range defined by the first candidate defect mask, which can be used as input objects for subsequent area filtering.

[0132] Sa2. Based on the first preset area filtering condition, remove the first candidate connected regions that meet the first preset area filtering condition from multiple first candidate connected regions to obtain the first target connected region.

[0133] The first preset area filtering condition is that the area of ​​the first candidate connected region is less than the first minimum area threshold, or the area of ​​the first candidate connected region is greater than the first maximum area threshold.

[0134] It should be noted that the first preset area filtering condition is used to constrain the area of ​​multiple first candidate connected regions, and the first candidate connected regions that satisfy the first preset area filtering condition are the connected regions that need to be eliminated.

[0135] Specifically, the area of ​​each of the multiple first candidate connected regions can be calculated separately, and the area of ​​each first candidate connected region can be compared with a first minimum area threshold and a first maximum area threshold. When the area of ​​a first candidate connected region is less than the first minimum area threshold or greater than the first maximum area threshold, the first candidate connected region is removed. The first candidate connected regions that are not removed are retained as the first target connected regions.

[0136] Optionally, the area can be obtained by counting the number of pixels contained in the first candidate connected region, and this application does not limit this.

[0137] For example, if the area of ​​a first candidate connected region is too small, it may correspond to a noise point or a local isolated interference; if the area of ​​a first candidate connected region is too large, it may correspond to a non-target large-area abnormal region or a background interference region. Both can be eliminated by the first preset area filtering condition.

[0138] Sa3. Determine the first candidate defect region based on the pixel coordinates of the first target connected region.

[0139] The pixel coordinates of the first target connected region are used to characterize the positional distribution of the first target connected region in the target barcode image. The first candidate defect region is the image region characterized by the pixel coordinates corresponding to the first target connected region.

[0140] Specifically, the coordinate information corresponding to each pixel in the first target connected region can be extracted, and the first candidate defect region can be determined based on the coordinate information.

[0141] In this embodiment of the application, by performing connectivity analysis and area filtering on the candidate connected regions within the scope of the candidate defect mask, the effective regions corresponding to the real defects can be further distinguished from discrete noise, isolated small spots, and large-area non-target interference regions, thereby reducing false detections and missed detections, improving the accuracy and stability of the first candidate defect region extraction results, and providing a more reliable regional basis for the subsequent determination of the comprehensive defect region.

[0142] In one possible embodiment, the method steps shown in S104 can be implemented by S1041 to S1046, which are described in detail below.

[0143] S1041. Perform grayscale processing on the target barcode image to obtain a grayscale image.

[0144] The implementation of the method steps shown in S1041 is the same as that shown in S1031 above, and will not be described again in this embodiment.

[0145] S1042. Perform first binarization and inversion on the grayscale image to obtain an inverted binary image.

[0146] The first binarization process divides the pixels in the grayscale image into two categories based on the distribution of their grayscale values, resulting in a binary image. The inversion process inverts the pixel values ​​in the binary image, producing an inverted binary image.

[0147] Specifically, the grayscale image can first be binarized according to a preset segmentation threshold to obtain a binary image, and then the pixel values ​​in the binary image can be inverted to obtain an inverted binary image. Optionally, the first binarization process can use global threshold segmentation, adaptive threshold segmentation, or other binarization methods, which are not limited in this application.

[0148] For example, a segmentation threshold can be determined based on the grayscale distribution of the grayscale image, and pixels with grayscale values ​​higher or lower than the segmentation threshold can be assigned different binary values. Subsequently, the binary values ​​are inverted so that the main structure of the barcode is presented as a preset foreground form in the inverted binary image.

[0149] S1043. Based on the vertical structural elements, morphological processing is performed on the inverted binary image to obtain the main structure image of the barcode.

[0150] Among them, the vertical structural element is a structural element that adapts to the extension direction of the barcode body, and is used to highlight the main structure of the barcode and suppress local interference that is inconsistent with the main structure of the barcode.

[0151] The vertical structural element can be a vertical elongated structural element, a vertical rectangular structural element, or other structural elements extending along the vertical direction; this application does not limit this.

[0152] It should be noted that the dimensions of the vertical structural elements can be set according to the barcode line width, barcode height, image resolution, or preset detection requirements, and this application does not limit them.

[0153] The barcode main structure image is obtained after morphological processing, in which the main structure of the barcode is preserved, while local noise, small-area interference or non-main structure information is weakened.

[0154] Morphological processing may include opening operations, a combination of erosion and dilation, or other morphological processing methods that can preserve the main structure of the barcode, and this application does not limit this.

[0155] S1044. Based on the vertical projection results of the barcode main structure image, divide the potential barcode column regions to obtain multiple potential barcode column regions.

[0156] The vertical projection result is used to characterize the pixel distribution of the barcode main structure image along the vertical projection direction. Potential barcode column regions are candidate regions in the barcode main structure image that correspond to the barcode column structure.

[0157] It should be noted that the multiple potential barcode column regions are multiple candidate barcode column regions obtained by dividing the barcode main structure image, which can be used as input regions for subsequent determination of vertical missing regions.

[0158] Specifically, the foreground pixels in the barcode main structure image can be statistically analyzed along the preset projection direction to obtain the vertical projection result. Then, multiple potential barcode column areas can be divided according to the changes in pixel distribution in the vertical projection result.

[0159] Optionally, the preset projection direction can be the direction corresponding to the barcode column distribution, and this application does not limit this.

[0160] For example, potential barcode column areas can be divided based on continuously distributed high-response intervals, response peak intervals, or intervals that meet preset projection conditions in the vertical projection results.

[0161] S1045. For each potential barcode column region among multiple potential barcode column regions, determine the vertical missing region of the potential barcode column region.

[0162] The vertical missing region is used to characterize the image region in the corresponding potential barcode column region where the main barcode structure is not continuously presented in the vertical direction.

[0163] Specifically, for each potential barcode column area, the corresponding vertical missing area can be determined based on the structural distribution of that potential barcode column area.

[0164] S1046. Determine the second candidate defect region based on the pixel coordinates of the vertical missing regions of multiple potential barcode column regions.

[0165] The pixel coordinates of the vertically missing regions are used to characterize the positional distribution of each vertically missing region in the target barcode image. The second candidate defect region is the image region characterized by the pixel coordinates corresponding to the vertically missing regions of multiple potential barcode column regions.

[0166] It should be noted that the second candidate defect region is a candidate defect image region determined after barcode structure continuity analysis, which can be used to characterize the image region in the target barcode image that corresponds to the barcode structure continuity anomaly.

[0167] Specifically, the coordinate information of each pixel in the vertical missing area of ​​multiple potential barcode column areas can be extracted, and the second candidate defect area can be determined based on the coordinate information.

[0168] Optionally, the second candidate defect region can be determined by a single vertical missing region or by multiple vertical missing regions. This application does not limit this.

[0169] For example, when there are vertical missing areas in multiple potential barcode column areas, the pixel coordinates corresponding to the multiple vertical missing areas can be used together as the coordinate range of the second candidate defect area.

[0170] In this embodiment, the main structure of the barcode is extracted by binarizing, inverting, and performing morphological processing based on vertical structural elements on the target barcode image. Then, potential barcode column regions are divided based on the vertical projection results of the main barcode structure. The second candidate defect region is determined based on the vertical missing regions in each potential barcode column region. This enables the characterization of structural continuity anomalies such as breaks and missing prints from the overall barcode structure level, improves the ability to identify structural defects, and provides a more accurate candidate region basis for the subsequent determination of comprehensive defect regions.

[0171] In one possible embodiment, the method steps shown in S1045 can be implemented by Sc1 to Sc4, which are described in detail below.

[0172] Sc1. For each potential barcode column area, determine the effective vertical range of the potential barcode column area within the barcode area to obtain the target vertical interval.

[0173] The vertical effective range characterizes the effective interval within the barcode region where structural continuity analysis should be performed along the vertical direction. The target vertical interval can correspond to the interval between the top and bottom positions of the barcode region in the vertical direction.

[0174] Specifically, the vertical boundary range of the barcode region in the target barcode image can be determined first, and then the vertical boundary range can be mapped to each potential barcode column region to obtain the target vertical interval of the corresponding potential barcode column region. Optionally, the vertical boundary range can be determined based on the overall outer range of the barcode region, the foreground distribution range in the barcode main structure image, or other positional ranges that can characterize the upper and lower boundaries of the barcode main body. This application does not limit this.

[0175] For example, when the top and bottom boundary positions of the barcode area have been determined, the interval between the top and bottom boundary positions can be used as the target vertical interval for each potential barcode column area; or, for example, a certain boundary redundancy can be reserved on the basis of the overall vertical range of the barcode area to enhance the stability of subsequent vertical missing area judgment, which is not limited in this application.

[0176] Sc2. Based on the target vertical interval, construct the theoretical complete mask corresponding to the potential barcode column region.

[0177] The theoretically complete mask is used to characterize the theoretical structural region that should exist continuously within the target vertical interval under normal conditions, corresponding to the potential barcode column region. The theoretically complete mask can be understood as a mask image describing the complete barcode column shape of the potential barcode column region within its effective vertical range.

[0178] Specifically, a theoretical complete mask covering the complete barcode column corresponding to the potential barcode column region can be constructed based on the horizontal position range of the potential barcode column region and the vertical position range of the target vertical interval. Optionally, the mask region in the theoretical complete mask corresponds to the horizontal range of the potential barcode column region in the horizontal direction and the vertical range of the target vertical interval in the vertical direction; this application does not limit this.

[0179] For example, when a potential barcode column area corresponds to the width range of a barcode column in the horizontal direction, and the target vertical range corresponds to the top to bottom range of the barcode area, a theoretically complete mask can be constructed within the area formed by the horizontal and vertical ranges to represent the complete structure that the potential barcode column area should have under normal circumstances.

[0180] Sc3: Based on the actual foreground pixel regions within the potential barcode column region, obtain the actual existing mask.

[0181] The actual mask is used to represent the actual detected barcode main structure area within the corresponding potential barcode column region. The foreground pixel region is the actual pixel area within the potential barcode column region used to represent the main structure of the barcode.

[0182] Specifically, foreground pixels within the potential barcode column region can be extracted, and an actual mask can be generated based on the distribution position of the foreground pixels in the image. Optionally, the foreground pixels can originate from the foreground portion of the aforementioned barcode main structure image, or from a preprocessed set of pixels that can characterize the barcode main structure; this application does not impose any limitations on this.

[0183] For example, when the main barcode structure exists only in a portion of the vertical position within a potential barcode column region, an actual existence mask can be constructed based on the actual foreground pixels in that portion to characterize the actual structural distribution state within the potential barcode column region.

[0184] Sc4. Based on the difference between the theoretically complete mask and the actual existing mask, determine the vertical missing areas of the potential barcode column region.

[0185] The difference between the theoretically complete mask and the actual mask is used to characterize the missing parts of the potential barcode column region between the theoretically complete structure and the actual structure. Vertical missing regions are image areas in the potential barcode column region that should theoretically exist but are not contiguous in the actual image.

[0186] Specifically, the theoretically complete mask and the actual mask can be compared to determine the areas where they do not overlap, and the missing parts in these non-overlapping areas can be used as the vertical missing areas of potential barcode column regions. Optionally, the vertical missing areas can be determined by logical difference, pixel-level comparison, or other methods that can characterize the difference between the theoretical and actual areas; this application does not limit this method.

[0187] For example, when the theoretical complete mask continuously covers the entire vertical range of the target, while the actual mask only covers part of the vertical position, the area between the theoretical complete mask and the actual mask that is not covered by the actual mask can be identified as the vertical missing area.

[0188] In this embodiment, by determining the effective vertical range of the potential barcode column region within the barcode region, a corresponding theoretically complete mask is constructed. Furthermore, based on the actual foreground pixel region within the potential barcode column region, an actual mask is obtained. Then, the vertical missing region is determined according to the difference between the theoretically complete mask and the actual mask. This allows for the comparison of the structure that should exist continuously in the theoretically complete state of the barcode column with the actual detected structure, thereby more accurately identifying structural defects such as vertical breaks and partial missing prints in the barcode column, and improving the detection accuracy and positioning precision of structural defects.

[0189] In one possible embodiment, the method steps shown in S105 can be implemented by S1051 to S1056, which are described in detail below.

[0190] S1051. Perform grayscale processing on the target barcode image to obtain a grayscale image.

[0191] The implementation of the method steps shown in S1051 is the same as that shown in S1031 above, and will not be described again in this embodiment.

[0192] S1052. Perform a second binarization process on the grayscale image to obtain a statistical binary image.

[0193] The second binarization process divides the pixels in the grayscale image into black pixels and non-black pixels based on the distribution of pixel grayscale values, resulting in a statistical binary image for column statistical analysis. The black pixels in the statistical binary image represent the pixel regions corresponding to the barcode lines.

[0194] Specifically, a second binarization process can be performed on the grayscale image according to a preset segmentation threshold to obtain a statistical binary image. Optionally, the second binarization process can employ global threshold segmentation, adaptive threshold segmentation, or other binarization methods, which are not limited in this application.

[0195] For example, the segmentation threshold corresponding to the second binarization can be determined based on the grayscale distribution of the grayscale image, and the pixels in the grayscale image can be divided into black pixels and non-black pixels based on the segmentation threshold to obtain a statistical binary image.

[0196] S1053. Count the number of black pixels in each column of the binary image to obtain the column pixel distribution statistics.

[0197] The column pixel distribution statistics are used to characterize the distribution of black pixels in each column of the statistical binary image. The number of black pixels reflects the distribution of the barcode line area in the corresponding column.

[0198] Specifically, the number of black pixels in each column can be counted along the column direction of the statistical binary image, and the number of black pixels in each column can be used as the column pixel distribution statistics result.

[0199] Optionally, the column pixel distribution statistics can be a sequence of black pixel counts arranged in column order, or other data formats that can characterize the distribution of black pixel counts in each column. This application does not limit this.

[0200] For example, a high number of black pixels in a column indicates that there are many pixel areas corresponding to barcode lines in that column; conversely, a low number of black pixels in a column indicates that there are fewer pixel areas corresponding to barcode lines in that column, or that there are local anomalies affecting the normal distribution of that column. S1054. Based on a preset threshold for the number of black pixels, the statistical results of the column pixel distribution are filtered to obtain potential defect columns.

[0201] The preset black pixel count threshold is used to determine the number of black pixels in each column, thus filtering out columns with abnormal black pixel counts. Potentially defective columns are those with a black pixel count lower than the preset black pixel count threshold.

[0202] Specifically, the number of black pixels in each column of the column pixel distribution statistics can be compared with a preset black pixel number threshold; when the number of black pixels in a certain column is lower than the preset black pixel number threshold, the column is identified as a potential defect column.

[0203] Optionally, the preset threshold for the number of black pixels can be a fixed threshold or can be set according to the distribution of black pixels in the statistical binary image. This application does not limit this.

[0204] For example, if the number of black pixels in a column is significantly lower than the number of black pixels in a normal barcode column, the column can be identified as a potentially defective column to indicate that the column may have dirt, scratches, partial occlusion or other abnormalities.

[0205] S1055. Based on the potential defect column, construct the second candidate defect mask.

[0206] The second candidate defect mask is used to characterize the candidate range in the target barcode image corresponding to the potential defect column. The masked area in the second candidate defect mask corresponds to the position of the potential defect column in the image.

[0207] Specifically, the corresponding column regions of potential defects in the statistical binary image or target barcode image can be marked as candidate regions, and a second candidate defect mask can be constructed based on the candidate regions.

[0208] Optionally, when there are multiple potential defect columns, the column regions corresponding to the multiple potential defect columns can be jointly marked in the same second candidate defect mask, and this application does not limit this.

[0209] For example, the pixel regions corresponding to each potential defect in the vertical range of the image can be marked as candidate mask regions to form a second candidate defect mask.

[0210] S1056. Determine the third candidate defect region based on the second candidate defect mask.

[0211] The second candidate defect mask is used to characterize the image range corresponding to the candidate abnormal column in the target barcode image.

[0212] Specifically, a third candidate defect region can be determined based on the candidate range represented in the second candidate defect mask. The third candidate defect region is the candidate defect image region in the target barcode image that corresponds to the abnormal pixel distribution in each column of the barcode.

[0213] In this embodiment of the application, by performing a second binarization process on the target barcode image and statistically analyzing and filtering the number of black pixels in each column of the statistical binary image, it is possible to identify potential defect columns with abnormal numbers of black pixels from the perspective of changes in pixel distribution in each column of the barcode, and further construct a corresponding second candidate defect mask, thereby effectively characterizing column distribution anomalies caused by factors such as dirt, dark scratches, and partial occlusion, improving the detection capability of this type of defect, and providing a basis for the accurate determination of the third candidate defect region.

[0214] In one possible embodiment, the method steps shown in S1056 can be implemented by Sd1 to Sd3, which are described in detail below.

[0215] Sd1. Perform connected component analysis within the candidate range defined by the second candidate defect mask to obtain multiple second candidate connected regions.

[0216] The candidate range defined by the second candidate defect mask is the range of the image region corresponding to the candidate anomaly column within the second candidate defect mask. Connected component analysis is used to aggregate spatially connected candidate pixels within the candidate range to form multiple distinct connected regions.

[0217] Specifically, based on a preset connectivity rule, candidate pixels within the candidate range defined by the second candidate defect mask can be traversed, and interconnected candidate pixels can be divided into the same connected region, thereby obtaining multiple second candidate connected regions. The preset connectivity rule can be a 4-neighborhood connectivity rule, an 8-neighborhood connectivity rule, or other pixel connectivity determination rules, and this application does not limit it in this regard.

[0218] Optionally, each of the multiple second candidate connected regions can correspond to a set of continuously distributed candidate pixels.

[0219] For example, when there are multiple separate candidate pixel clusters within the candidate range defined by the second candidate defect mask, each candidate pixel cluster can be divided into different second candidate connected regions through connected component analysis.

[0220] Sd2. Based on the second preset area filtering condition, remove the second candidate connected regions that meet the second preset area filtering condition from multiple second candidate connected regions to obtain the second target connected region.

[0221] The second preset area filtering condition is that the area of ​​the second candidate connected region is less than the second minimum area threshold, or the area of ​​the second candidate connected region is greater than the second maximum area threshold.

[0222] It should be noted that the second preset area filtering condition is used to filter the area of ​​multiple second candidate connected regions. The second candidate connected regions that meet the second preset area filtering condition are the connected regions that need to be removed.

[0223] Specifically, the area of ​​each of the multiple second candidate connected regions can be calculated separately, and the area of ​​each second candidate connected region can be compared with the second minimum area threshold and the second maximum area threshold. When the area of ​​a certain second candidate connected region is less than the second minimum area threshold or greater than the second maximum area threshold, the second candidate connected region is removed. The second candidate connected regions that are not removed are retained as the second target connected regions.

[0224] Optionally, the area can be obtained by counting the number of pixels contained in the second candidate connected region, and this application does not limit this.

[0225] For example, if the area of ​​a second candidate connected region is too small, it may correspond to a local noise point, an isolated interference point, or a non-target small region; if the area of ​​a second candidate connected region is too large, it may correspond to a large area of ​​background interference or a non-target abnormal region. Both can be eliminated by the second preset area filtering condition.

[0226] Sd3. Determine the third candidate defect region based on the pixel coordinates of the second target connected region.

[0227] The pixel coordinates of the second target connected region are used to characterize the positional distribution of the second target connected region in the target barcode image. The third candidate defect region is the image region characterized by the pixel coordinates corresponding to the second target connected region.

[0228] Specifically, the coordinate information corresponding to each pixel in the second target connected region can be extracted, and the third candidate defect region can be determined based on the coordinate information. Optionally, the third candidate defect region can be determined by one second target connected region or by multiple second target connected regions, and this application does not limit this.

[0229] For example, when the second target connected region includes multiple separate preserved connected regions, the pixel coordinates corresponding to the multiple preserved connected regions can be used together as the coordinate range of the third candidate defect region.

[0230] In this embodiment, by performing connected component analysis within the candidate range defined by the second candidate defect mask, and by filtering the obtained multiple second candidate connected regions in combination with the second preset area filtering condition, the effective defect regions related to the abnormal pixel distribution of each column of the barcode can be distinguished from isolated noise, small invalid regions, and large non-target interference regions. Then, the third candidate defect region is determined based on the pixel coordinates of the retained second target connected regions, thereby improving the accuracy and stability of the third candidate defect region extraction results and further enhancing the detection effect on defects such as dirt, dark scratches, and partial occlusion with abnormal column distribution.

[0231] In one possible embodiment, the method steps shown in S107 can be implemented by S1071 to S1074, which are described in detail below.

[0232] S1071. The defect pixels in the comprehensive defect area are marked to obtain the defect annotation results.

[0233] The identification processing is used to distinguish defective pixels in the comprehensive defect area from non-defective pixels in the target barcode image, so as to form identification information that can intuitively represent the distribution of defect locations.

[0234] The defect annotation results may include color identifiers, grayscale identifiers, boundary identifiers, region filling identifiers, or other identifiers that can characterize the defect area corresponding to the defect pixels, and this application does not limit them.

[0235] Specifically, a preset labeling attribute can be assigned to the defect pixels in the comprehensive defect area to obtain the defect labeling result.

[0236] Optionally, the preset identification attribute can be a preset color value, a preset grayscale value, a preset border style, or other display attributes used to highlight the defective area; this application does not limit this.

[0237] For example, defect pixels in the comprehensive defect area can be marked with red, green, blue, or other colors that are clearly different from the original pixels in the target barcode image; or, for example, only the edge contour of the comprehensive defect area can be marked to form a boundary-style defect annotation result.

[0238] S1072. Overlay the defect annotation results onto the target barcode image to obtain the defect annotation image.

[0239] The overlay process combines the annotation information representing the distribution of defects with the target barcode image to form an output image that simultaneously contains the content of the original barcode image and the defect annotation content.

[0240] Defect-annotated images are used to visually display the location, extent, and distribution of defects in a target barcode image.

[0241] Specifically, the defect annotation results can be mapped to the corresponding positions in the target barcode image according to the pixel correspondence, and the pixel values ​​at the corresponding positions can be replaced, merged, or superimposed to obtain the defect annotation image.

[0242] Optionally, the overlay method can be direct overlay, transparent blending overlay, or other image overlay methods, and this application does not limit this.

[0243] For example, when the defect annotation result is in the form of color marking, the defect marking area with a preset color can be superimposed on the corresponding pixel position of the target barcode image; when the defect annotation result is in the form of boundary contour, the boundary contour line can be drawn on the corresponding area of ​​the target barcode image. This application does not limit this.

[0244] S1073. Count the number of defective pixels in the comprehensive defect area to obtain the defect statistics results.

[0245] Among them, the defect statistics results are used to characterize the size of defect pixels in the comprehensive defect region.

[0246] The number of defective pixels can reflect the degree of defects, the range of defects, or the abnormal distribution in the target barcode image.

[0247] Specifically, the defective pixels contained in the comprehensive defect area can be counted to obtain the number of defective pixels, and the number of defective pixels can be used as the defect statistics result. Optionally, the defect statistics result may also include the number of pixels in each independent defect area, the total area of ​​the comprehensive defect area, the proportion of the defect area to the area of ​​the target barcode image, or other statistical information based on the number of defective pixels. This application does not limit this.

[0248] For example, when the overall defect area contains multiple separate defect sub-regions, the number of defective pixels in each defective sub-region can be counted separately, or the number of defective pixels in all defective sub-regions can be summed to obtain the overall defect statistics.

[0249] S1074. Output the barcode defect detection results based on the defect annotation image and / or defect statistics.

[0250] Among them, the barcode defect detection results are used to characterize the output information of defect detection in the target barcode image.

[0251] The barcode defect detection results may include defect-annotated images, defect statistics, or a combination of both, and this application does not limit this.

[0252] Specifically, the defect-annotated image can be output as an image-based detection result, the defect statistics can be output as a data-based detection result, or both the defect-annotated image and the defect statistics can be output simultaneously to form a complete barcode defect detection result.

[0253] Optionally, the output method can be display output, storage output, transmission output, or other result output methods, which are not limited in this application.

[0254] For example, defect-marked images can be displayed on a display terminal so that operators can view the distribution of defects; defect statistics can be sent to a control terminal, quality management system, or storage device for subsequent quality assessment, defect recording, or production adjustments.

[0255] In this embodiment, defect pixels in the comprehensive defect area are marked and superimposed onto the target barcode image to obtain a defect-marked image. At the same time, the number of defect pixels in the comprehensive defect area is counted to obtain defect statistics. Then, the barcode defect detection result is output based on the defect-marked image and / or defect statistics. This provides both intuitive defect distribution information and quantified defect statistics, improving the visualization and analyzability of the detection results. It also facilitates operators to quickly locate defect positions and carry out subsequent quality assessment and processing.

[0256] Figure 2 A visual example of a barcode defect detection method provided in this application embodiment. (See attached image.) Figure 2 As shown in the figure, the left side is an example of the original barcode image to be detected, the right side is the corresponding defect detection result image, and the center is a schematic representation of the barcode defect detection processing procedure. After processing by the method of this application, the defective areas of the barcode can be marked and displayed in the detection result image. The abnormal areas detected in the figure are marked in red, thus intuitively reflecting the defect location, defect range, and defect distribution in the barcode image, which facilitates the visualization and analysis of the detection effect of the method of this application.

[0257] Figure 3 This is a schematic diagram of a barcode defect detection device provided in an embodiment of this application. Figure 3 As shown, the barcode defect detection device 300 includes an image acquisition module 301, a correction module 302, a first defect detection module 303, a second defect detection module 304, a third defect detection module 305, a merging module 306, and an output module 307.

[0258] The image acquisition module 301 is used to acquire an image to be detected, the image to be detected containing a barcode area.

[0259] The correction module 302 is used to locate and correct the posture of the barcode region in the image to be detected, so as to obtain the target barcode image.

[0260] The first defect detection module 303 is used to determine the first candidate defect region based on the grayscale anomaly information of the target barcode image.

[0261] The second defect detection module 304 is used to determine the second candidate defect region based on the barcode structure continuity information of the target barcode image.

[0262] The third defect detection module 305 is used to determine the third candidate defect region based on the pixel distribution information of each column of the target barcode image.

[0263] The merging module 306 is used to merge the first candidate defect region, the second candidate defect region and the third candidate defect region to obtain a comprehensive defect region.

[0264] The output module 307 is used to output the barcode defect detection result based on the comprehensive defect area.

[0265] It should be noted that the specific process of each module in the barcode defect detection device executing the above method has been described in detail in the above embodiments, and this embodiment does not make specific limitations on it.

[0266] Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 4 As shown, the electronic device 400 provided in this embodiment includes a memory 401 and a processor 402.

[0267] The memory 401 can be a separate physical unit, connected to the processor 402 via a bus 403. Alternatively, the memory 401 and processor 402 can be integrated and implemented in hardware. The memory 401 stores program instructions, which the processor 402 calls to execute the operations performed by the barcode defect detection device in any of the above method embodiments.

[0268] Optionally, when some or all of the methods in the above embodiments are implemented by software, the electronic device 400 may also include only the processor 402. A memory 401 for storing programs is located outside the electronic device 400, and the processor 402 is connected to the memory via circuits / wires to read and execute the programs stored in the memory. The processor 402 may be a central processing unit (CPU), a network processor (NP), or a combination of a CPU and an NP. The processor 402 may further include a hardware chip. The hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.

[0269] Memory 401 may include volatile memory, such as random-access memory (RAM); memory may also include non-volatile memory, such as flash memory, hard disk drive (HDD) or solid-state drive (SSD); memory may also include combinations of the above types of memory.

[0270] For example, this application provides a chip, including: an interface circuit and a logic circuit. The interface circuit is used to receive signals from other chips outside the chip and transmit them to the logic circuit, or to send signals from the logic circuit to other chips outside the chip. The logic circuit is used to perform the operations performed by the barcode defect detection device in the above method embodiments.

[0271] For example, this application provides a computer-readable storage medium storing computer program instructions thereon, which are executed by the processor of an electronic device to cause the electronic device to perform the operations performed by the barcode defect detection device in the above method embodiments.

[0272] For example, this application provides a computer program product that, when run on an electronic device, causes the electronic device to perform the operations performed by the barcode defect detection device in the above method embodiments.

[0273] The above description is merely a specific embodiment of this application, enabling those skilled in the art to understand or implement this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments described herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A method for detecting barcode defects, characterized in that, The method includes: Acquire an image to be detected, wherein the image to be detected contains a barcode area; The barcode region in the image to be detected is located and its orientation is corrected to obtain the target barcode image; Based on the grayscale anomaly information of the target barcode image, a first candidate defect region is determined; Based on the barcode structure continuity information of the target barcode image, a second candidate defect region is determined; Based on the pixel distribution information of each column of the target barcode image, a third candidate defect region is determined; The first candidate defect region, the second candidate defect region, and the third candidate defect region are merged to obtain a comprehensive defect region. Based on the comprehensive defect area, output the barcode defect detection result; The step of determining the second candidate defect region based on the barcode structure continuity information of the target barcode image includes: The target barcode image is converted to grayscale to obtain a grayscale image; The grayscale image is subjected to a first binarization and inversion process to obtain an inverted binary image; Based on the vertical structural elements, the inverted binary image is morphologically processed to obtain the main structure image of the barcode. Based on the vertical projection result of the barcode main structure image, potential barcode column regions are divided to obtain multiple potential barcode column regions; For each of the plurality of potential barcode column regions, determine the vertical missing region of the potential barcode column region; The second candidate defect region is determined based on the pixel coordinates of the vertical missing regions of the plurality of potential barcode column regions; The step of determining the vertical missing region of each of the plurality of potential barcode column regions includes: For each potential barcode column region, determine the effective vertical range of the potential barcode column region within the barcode region to obtain the target vertical interval; Based on the target vertical interval, construct the theoretical complete mask corresponding to the potential barcode column region; Based on the actual foreground pixel regions within the potential barcode column region, the actual existing mask is obtained; Based on the difference between the theoretically complete mask and the actual existing mask, the vertical missing area of ​​the potential barcode column region is determined.

2. The method according to claim 1, characterized in that, The step of locating and correcting the posture of the barcode region in the image to be detected to obtain the target barcode image includes: Based on a preset minimum bounding rectangle size threshold, regions in the image to be detected are filtered to obtain candidate barcode regions; Determine the minimum bounding rectangle corresponding to the candidate barcode region; The attitude offset angle is determined based on the deflection angle of the minimum circumscribed rectangle relative to the horizontal reference direction. Based on the posture offset angle, the candidate barcode region is rotated and corrected to obtain the target barcode image.

3. The method according to claim 1, characterized in that, The step of determining the first candidate defect region based on the grayscale anomaly information of the target barcode image includes: The target barcode image is converted to grayscale to obtain a grayscale image; The grayscale image is subjected to a cap transform to obtain an abnormally enhanced image; Based on structural elements adapted to the barcode orientation, the abnormal enhancement image is subjected to orientation enhancement processing to obtain an orientation-enhanced image; The enhanced image is subjected to threshold segmentation to obtain a first candidate defect mask; The first candidate defect region is determined based on the first candidate defect mask.

4. The method according to claim 3, characterized in that, The step of determining the first candidate defect region based on the first candidate defect mask includes: Connectivity analysis is performed within the candidate range defined by the first candidate defect mask to obtain multiple first candidate connected regions; Based on the first preset area filtering condition, the first candidate connected regions that satisfy the first preset area filtering condition are removed from the plurality of first candidate connected regions to obtain the first target connected region; wherein, the first preset area filtering condition is that the area of ​​the first candidate connected region is less than the first minimum area threshold, or the area of ​​the first candidate connected region is greater than the first maximum area threshold. The first candidate defect region is determined based on the pixel coordinates of the first target connected region.

5. The method according to claim 1, characterized in that, The step of determining the third candidate defect region based on the pixel distribution information of each column of the target barcode image includes: The target barcode image is converted to grayscale to obtain a grayscale image; The grayscale image is subjected to a second binarization process to obtain a statistical binary image; The number of black pixels in each column of the statistical binary image is counted to obtain the column pixel distribution statistics. Based on a preset threshold for the number of black pixels, the statistical results of the pixel distribution in the column are filtered to obtain potential defect columns; Based on the potential defect list, a second candidate defect mask is constructed; The third candidate defect region is determined based on the second candidate defect mask.

6. The method according to claim 5, characterized in that, Determining the third candidate defect region based on the second candidate defect mask includes: Connectivity analysis is performed within the candidate range defined by the second candidate defect mask to obtain multiple second candidate connected regions; Based on the second preset area filtering condition, the second candidate connected regions that satisfy the second preset area filtering condition are removed from the plurality of second candidate connected regions to obtain the second target connected region; wherein, the second preset area filtering condition is that the area of ​​the second candidate connected region is less than the second minimum area threshold, or the area of ​​the second candidate connected region is greater than the second maximum area threshold; The third candidate defect region is determined based on the pixel coordinates of the second target connected region.

7. The method according to claim 1, characterized in that, The step of outputting barcode defect detection results based on the comprehensive defect area includes: The defect pixels in the comprehensive defect region are marked to obtain the defect annotation result; The defect annotation results are superimposed onto the target barcode image to obtain a defect-annotated image; The number of defective pixels in the comprehensive defect area is counted to obtain the defect statistics results; Based on the defect-annotated image and / or the defect statistics, the barcode defect detection result is output.

8. A barcode defect detection device, characterized in that, The device includes: An image acquisition module is used to acquire an image to be detected, wherein the image to be detected includes a barcode area; The correction module is used to locate and correct the posture of the barcode region in the image to be detected, so as to obtain the target barcode image; The first defect detection module is used to determine the first candidate defect region based on the grayscale anomaly information of the target barcode image; The second defect detection module is used to determine the second candidate defect region based on the barcode structure continuity information of the target barcode image; The third defect detection module is used to determine the third candidate defect region based on the pixel distribution information of each column of the target barcode image. The merging module is used to merge the first candidate defect region, the second candidate defect region and the third candidate defect region to obtain a comprehensive defect region. The output module is used to output the barcode defect detection results based on the comprehensive defect area; Specifically, the second defect detection module is used for: performing grayscale processing on the target barcode image to obtain a grayscale image; performing first binarization and inversion processing on the grayscale image to obtain an inverted binary image; performing morphological processing on the inverted binary image based on vertical structuring elements to obtain a barcode main structure image; dividing potential barcode column regions based on the vertical projection result of the barcode main structure image to obtain multiple potential barcode column regions; determining the effective vertical range of each potential barcode column region within the barcode region to obtain a target vertical interval; constructing a theoretical complete mask corresponding to the potential barcode column region based on the target vertical interval; obtaining an actual existing mask based on the actual existing foreground pixel region within the potential barcode column region; determining the vertical missing region of the potential barcode column region based on the difference between the theoretical complete mask and the actual existing mask; and determining the second candidate defect region based on the pixel coordinates of the vertical missing regions of the multiple potential barcode column regions.

Citation Information

Patent Citations

  • Milk powder food packaging bar code defect detection method based on image processing

    CN118364839A

  • Bar code label identification method and device applied to AOI and storage medium

    CN118520893A