Barcode recognition method, electronic device, computer storage medium and program product
Through image inversion and adaptive binarization threshold processing, combined with image fusion and morphological operations, the problem of difficult recognition of barcodes under dense and dark backgrounds is solved, and the speed and accuracy of barcode recognition are significantly improved.
Patent Information
- Application Number
- CN202410421247.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-09
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2044-04-09
AI Technical Summary
Traditional barcode recognition tools are difficult to recognize quickly and accurately when barcodes are densely packed, and the recognition effect is particularly low on dark backgrounds.
Through image inversion and adaptive binarization threshold processing, the contrast and prominence of the barcode area are enhanced, and combined with image fusion and morphological operations, the speed and accuracy of barcode positioning and decoding are improved.
Effectively separate dense barcodes, improve barcode recognition speed and accuracy, and significantly improve the recognition effect especially on dark backgrounds.
Smart Images

Figure CN118171672B
Abstract
Description
Technical Field
[0001] Embodiments of the present application relate to the field of image processing technology, and in particular to a barcode recognition method, electronic device, computer storage medium, and program product. Background Art
[0002] A barcode is a graphic identifier used to express information related to an item. However, due to the uniqueness of some item-related information, such as electronic products, there may be multiple barcodes associated with them.
[0003] Therefore, because multiple barcodes are printed on the outer packaging of these items, the barcodes are relatively densely distributed. In this case, the traditional way of barcode recognition and decoding through open source decoding tools such as Zxing and Zbar will be difficult to quickly and accurately recognize each barcode because the barcodes are densely packed and difficult to distinguish from each other, which can easily reduce the speed and accuracy of barcode recognition. Summary of the invention
[0004] The embodiments of the present application provide a barcode recognition method, electronic device, computer storage medium and program product, which can improve the recognition speed and accuracy of barcodes.
[0005] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0006] In a first aspect, a barcode recognition method is provided, which is applied to an electronic device, and the electronic device includes a camera; the method includes: the electronic device collects a first image in response to a user's scanning operation. The first image includes one or more barcodes and is collected by the camera. Then, the electronic device obtains a second image and a third image based on the first image; the second image is a grayscale image of the first image, and the third image is obtained by inverting the second image. Next, the electronic device performs image fusion on the second image and the third image to obtain a fourth image, and performs binarization processing on the fused fourth image based on an adaptive binarization threshold to obtain a fifth image. The adaptive binarization threshold is determined based on a first pixel mean; the first pixel mean is an average value of multiple second pixel means; the second pixel mean is an average value of each pixel value in different pixel ranges in the fourth image, and the second pixel mean corresponds to the pixel range one by one. Finally, the electronic device locates and decodes the barcode based on the fifth image, obtains decoding information and displays it on the first interface.
[0007] It can be seen that in this method, before the electronic device locates and decodes the barcode, it will first invert the pixel values of the image by inverting the image, thereby enhancing the contrast of the image and visually enhancing the barcode area, making the barcode in the image more prominent and improving the barcode recognition speed and accuracy.
[0008] If there are multiple barcodes densely distributed in the image, each barcode can be highlighted by inverting the color to separate the different barcodes, thereby enhancing the positioning effect of each barcode. In the case of multiple barcodes densely distributed, the speed and accuracy of subsequent barcode positioning and decoding can also be improved.
[0009] Furthermore, since image inversion can change the image background color, and the adaptive binarization threshold is a binarization threshold obtained based on the average value of each pixel value in different pixel ranges, the situation where the barcode background area accounts for a relatively large proportion is taken into account. Therefore, in this method, for dark-colored barcodes, such as barcodes with black backgrounds, the black background can be converted to a white background by inverting the image, and binarization based on this adaptive binarization threshold can further highlight the barcode area, thereby reducing the impact of the black background. Even in the face of barcodes with black backgrounds, the barcode recognition effect can be ensured, and the speed and accuracy of barcode positioning and decoding can be improved.
[0010] That is to say, in the case of densely distributed barcodes, even if the background is dark such as black, this method can separate different barcodes by highlighting each barcode through inversion enhancement, thereby enhancing the positioning effect of each barcode; and, by inverting the image to convert the black background into a white background, binarization based on this adaptive binarization threshold can further highlight the barcode area, thereby reducing the impact of the black background, thereby quickly and accurately locating and decoding multiple densely distributed barcodes.
[0011] In a possible implementation manner of the first aspect, performing image fusion on the second image and the third image to obtain a fourth image may include: using a discrete differential operator to calculate the approximate gradient of the second image to obtain a first gradient image, and using a discrete differential operator to calculate the approximate gradient of the third image to obtain a second gradient image; and performing weighted summation on the second gradient image and the third gradient image to obtain the fourth image.
[0012] In this implementation, considering that the original background is light-colored, such as a white background, the recognition effect is not bad, but the background color will be affected by the image inversion. Therefore, the second image and the third image are fused to ensure that the fourth image is an image with white background image characteristics. At the same time, in order to further highlight the barcode area and enhance the barcode positioning effect, the barcode area can be highlighted by edge detection. In addition, based on the characteristic that the barcode is a straight line, in order to improve the effect of edge detection, the discrete differential operator is used to calculate the approximate gradient of the image to realize the edge detection of the barcode. After that, the obtained gradient image is weighted summed to complete the image fusion.
[0013] In another possible implementation manner of the first aspect, when weighted summing is performed on the first gradient image and the second gradient image, a first weight corresponding to the first gradient image may be smaller than a second weight corresponding to the second gradient image.
[0014] It is understandable that the weight is determined by repeated deduction based on actual needs and test results. After actual test determination, in most background colors, when the first weight is less than the second weight, a good barcode recognition effect can be obtained.
[0015] In another possible implementation of the first aspect, binarizing the fourth image according to an adaptive binarization threshold to obtain a fifth image may include: dividing the pixel values in the fourth image based on the candidate binarization threshold as a division basis to obtain a first pixel value range and a second pixel value range; respectively calculating the mean of the first pixel value range and the mean of the second pixel value range to obtain multiple second pixel means; calculating the average of multiple second pixel means to obtain the first pixel mean; when the error between the candidate binarization threshold and the first pixel mean is less than the error threshold, binarizing the fourth image using the candidate binarization threshold as an adaptive binarization threshold to obtain a fifth image. Thus, binarization processing according to the adaptive binarization threshold can further eliminate the influence of the background color, highlight the barcode area, and improve the positioning effect of the barcode area.
[0016] In another possible implementation of the first aspect, when the error between the candidate binarization threshold and the first pixel mean is greater than the error threshold, the first pixel mean is used as a new candidate binarization threshold, and the adaptive binarization threshold is re-determined based on the new candidate binarization threshold. That is, when the error between the candidate binarization threshold and the first pixel mean is greater than the error threshold, the electronic device uses the current first pixel mean as a new candidate binarization threshold. Then, the electronic device uses the new candidate binarization threshold (i.e., the previous first pixel mean) to divide the pixel values in the fourth image and re-obtain the first pixel value range and the second pixel value range. Then, multiple second pixel means are continued to be calculated to obtain a new first pixel mean, and then the new candidate binarization threshold is determined based on the error whether it can be used as an adaptive binarization threshold.
[0017] In another possible implementation manner of the first aspect, performing barcode positioning and decoding based on the fifth image to obtain decoding information may include:
[0018] Determine the adaptive window size; perform morphological operation on the fifth image based on the adaptive window size to obtain a sixth image; wherein the adaptive window size is determined according to the proportion of the barcode in the image; and perform barcode positioning and decoding on the sixth image to obtain decoding information. The morphological operation includes dilation, erosion, and opening and closing operations.
[0019] In this implementation, by performing morphological operations such as dilation and erosion on the fifth image, the barcode area can be further enhanced, thereby enhancing the positioning effect of the barcode area to improve the speed and accuracy of barcode recognition and decoding. By performing morphological operations such as opening and closing operations on the fifth image, the influence of characters in the image can be further removed, further improving the accuracy of barcode recognition and decoding.
[0020] At the same time, in this implementation, taking into account the sizes of different types of barcodes and the distribution of different types of barcodes, the window size required for the morphological operation is further determined based on the actual proportion of the barcode, so that the morphological operation can be accurately adapted to the distribution of different types of barcodes based on the adaptive window size, thereby further improving the effect of the morphological operation, to assist in improving the effect of subsequent barcode positioning and recognition.
[0021] In another possible implementation of the first aspect, determining the adaptive window size may include: dividing the fifth image into n image blocks according to pixel rows; wherein n≥2, and n is an integer; different image blocks include an equal number of pixel columns; for each image block, respectively adding the pixel values in each pixel column in the image block to obtain a one-dimensional array corresponding to the image block; wherein the values in the one-dimensional array correspond one-to-one to the pixel columns; using an adaptive binarization threshold, binarizing the one-dimensional array to obtain a one-dimensional binary array; in each one-dimensional binary array, determining the continuous length of values that are identical and continuous to obtain a reference shortest continuous length corresponding to each image block; taking the minimum value of the reference shortest continuous lengths corresponding to the n image blocks as the target shortest continuous length, and taking the target shortest continuous length as the adaptive window size.
[0022] In another possible implementation of the first aspect, determining the adaptive window size may include: dividing the fifth image into n image blocks according to pixel rows; wherein n≥2, and n is an integer; different image blocks include an equal number of pixel columns; for each image block, respectively adding the pixel values in each pixel column in the image block to obtain a one-dimensional array corresponding to the image block; wherein the values in the one-dimensional array correspond one-to-one to the pixel columns; using an adaptive binarization threshold, binarizing the one-dimensional array to obtain a one-dimensional binary array; in each one-dimensional binary array, determining the continuous length of values that are identical and continuous to obtain a reference shortest continuous length corresponding to each image block; taking the product of the minimum value of the reference shortest continuous lengths corresponding to the n image blocks and a preset coefficient as the target shortest continuous length, and taking the target shortest continuous length as the adaptive window size.
[0023] In another possible implementation of the first aspect, based on the vertical and straight characteristics of the barcode, in order to improve the effect of morphological operations on the barcode, the dilation operation and erosion operation in the morphological operation can include a horizontal dilation operation and a vertical erosion operation for the barcode.
[0024] That is, when the fifth image is subjected to morphological operation based on the adaptive window size to obtain the sixth image, the expansion operation and the erosion operation in the morphological operation may be a horizontal expansion operation and a vertical erosion operation. That is, when the fifth image is subjected to morphological operation based on the adaptive window size to obtain the sixth image, the method may include: performing morphological operation on the fifth image based on the adaptive window size to obtain the sixth image; wherein the morphological operation includes a horizontal expansion operation and a vertical erosion operation. Thus, the morphological operation effect can be improved for the barcode, and the subsequent barcode positioning and decoding effect can be further improved.
[0025] In another possible implementation of the first aspect, the sixth image is barcode positioned and decoded to obtain decoding information, including: edge detection is performed on the sixth image to locate the barcode area to obtain the seventh image; and the seventh image is barcode decoded by a barcode decoding tool to obtain decoding information. Thus, the decoding tool can receive a high-quality barcode image with enhanced positioning and prominent barcodes, and the decoding tool can directly achieve fast decoding without spending a lot of time on locating the barcode, thereby improving the speed and accuracy of barcode recognition.
[0026] In a second aspect, the present application provides an electronic device, including: a camera, one or more processors and a memory, wherein the camera and the memory are respectively coupled to the processor; the camera is used to capture images; the memory stores one or more computer program codes, and the computer program codes include computer instructions; when the processor executes the computer instructions, the electronic device performs the following steps:
[0027] In response to a barcode scanning operation by a user, a first image is captured; wherein the first image includes one or more barcodes, and the first image is captured by a camera;
[0028] A second image and a third image are obtained based on the first image; wherein the second image is a grayscale image of the first image, and the third image is obtained by inverting the second image;
[0029] Performing image fusion on the second image and the third image to obtain a fourth image;
[0030] Binarizing the fused fourth image based on an adaptive binarization threshold to obtain a fifth image; wherein the adaptive binarization threshold is determined based on a first pixel mean; the first pixel mean is an average of a plurality of second pixel means; the second pixel mean is an average of pixel values in different pixel ranges in the fourth image, and the second pixel mean corresponds to the pixel range one by one;
[0031] Positioning and decoding the barcode based on the fifth image to obtain decoding information;
[0032] The first interface is displayed, and the first interface includes decoding information, and the decoding information corresponds to the barcode.
[0033] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0034] Calculating the approximate gradient of the second image using a discrete differential operator to obtain a first gradient image, and calculating the approximate gradient of the third image using a discrete differential operator to obtain a second gradient image;
[0035] A weighted sum is performed on the first gradient image and the second gradient image to obtain a fourth image.
[0036] In a possible implementation manner of the second aspect, when the above-mentioned computer instructions are executed by the processor, the electronic device further performs the following steps: performing weighted summation on the second gradient image and the second gradient image to obtain a fourth image; wherein the first weight corresponding to the first gradient image is less than the second weight corresponding to the second gradient image.
[0037] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0038] Using the candidate binary threshold as a division basis, dividing the pixel values in the fourth image to obtain a first pixel value range and a second pixel value range;
[0039] Calculating the mean of the first pixel value range and the mean of the second pixel value range respectively to obtain a plurality of second pixel mean values;
[0040] Calculate the average of multiple second pixel means to obtain the first pixel mean;
[0041] When the error between the candidate binarization threshold and the first pixel mean is less than the error threshold, the candidate binarization threshold is used as an adaptive binarization threshold to binarize the fourth image to obtain a fifth image.
[0042] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0043] When the error between the candidate binarization threshold and the first pixel mean is greater than the error threshold, the first pixel mean is used as a new candidate binarization threshold, and the adaptive binarization threshold is re-determined according to the new candidate binarization threshold.
[0044] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0045] Determine the adaptive window size;
[0046] Based on the adaptive window size, a morphological operation is performed on the fifth image to obtain a sixth image; wherein the adaptive window size is determined according to the proportion of the barcode in the image;
[0047] The barcode is located and decoded on the sixth image to obtain decoding information.
[0048] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0049] Dividing the fifth image into n image blocks according to pixel rows; wherein n≥2, n is an integer; and different image blocks include an equal number of pixel columns;
[0050] For each image block, the pixel values in each pixel column in the image block are added to obtain a one-dimensional array corresponding to the image block; wherein the values in the one-dimensional array correspond to the pixel columns one by one;
[0051] Using the adaptive binarization threshold, the one-dimensional array is binarized to obtain a one-dimensional binary array;
[0052] In each one-dimensional binary array, determine the continuous length of values that are identical and continuous, and obtain a reference shortest continuous length corresponding to each image block;
[0053] The minimum value of the reference shortest continuous lengths of the n image blocks is used as the target shortest continuous length, and the target shortest continuous length is used as the adaptive window size.
[0054] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0055] Dividing the fifth image into n image blocks according to pixel rows; wherein n≥2, n is an integer; and different image blocks include an equal number of pixel columns;
[0056] For each image block, the pixel values in each pixel column in the image block are added to obtain a one-dimensional array corresponding to the image block; wherein the values in the one-dimensional array correspond to the pixel columns one by one;
[0057] Using the adaptive binarization threshold, the one-dimensional array is binarized to obtain a one-dimensional binary array;
[0058] In each one-dimensional binary array, determine the continuous length of values that are identical and continuous, and obtain a reference shortest continuous length corresponding to each image block;
[0059] The product of the minimum value of the reference shortest continuous lengths of the n image blocks and the preset coefficient is used as the target shortest continuous length, and the target shortest continuous length is used as the adaptive window size.
[0060] In a possible implementation of the second aspect, when the above-mentioned computer instructions are executed by the processor, the electronic device further performs the following steps: based on the adaptive window size, performing a morphological operation on the fifth image to obtain a sixth image; wherein the morphological operation includes a horizontal dilation operation and a vertical erosion operation.
[0061] In a possible implementation manner of the second aspect, when the computer instructions are executed by a processor, the electronic device further performs the following steps:
[0062] Performing edge detection on the sixth image to locate the barcode area, thereby obtaining a seventh image;
[0063] The barcode decoding tool is used to perform barcode decoding on the seventh image to obtain decoding information.
[0064] In a third aspect, the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor in an electronic device, the electronic device executes the barcode recognition method of the first aspect and any possible implementation thereof.
[0065] In a fourth aspect, the present application provides a computer program product, which, when executed on a computer, enables the computer to execute the method of the first aspect and any possible implementation thereof. The computer may be the electronic device described above.
[0066] It can be understood that the beneficial effects that can be achieved by the electronic device of any possible implementation of the second aspect, the computer-readable storage medium of the third aspect, and the computer program product of the fourth aspect can be referred to as the beneficial effects in the first aspect and any possible implementation thereof, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] Figure 1A schematic diagram of a barcode provided in an embodiment of the present application Figure 1 ;
[0068] Figure 2 A schematic diagram of a barcode provided in an embodiment of the present application Figure 2 ;
[0069] Figure 3 A schematic diagram of a first interface provided in an embodiment of the present application;
[0070] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0071] Figure 5 A software structure block diagram of an electronic device provided in an embodiment of the present application;
[0072] Figure 6 A flowchart of a barcode recognition method provided in an embodiment of the present application;
[0073] Figure 7 A schematic diagram of a grayscale process provided in an embodiment of the present application;
[0074] Figure 8 A schematic diagram of an image inversion provided in an embodiment of the present application;
[0075] Fig. 9 A schematic diagram after a morphological operation provided in an embodiment of the present application;
[0076] Fig.10 A flowchart for determining an adaptive window size provided in an embodiment of the present application;
[0077] Fig.11 A schematic diagram of a positioning barcode area provided in an embodiment of the present application;
[0078] Fig.12 A timing interaction diagram of a barcode recognition method provided in an embodiment of the present application;
[0079] Fig.13 A structural block diagram of a chip system provided in an embodiment of the present application. DETAILED DESCRIPTION
[0080] The technical scheme of the embodiment of the present application will be clearly and completely described below in conjunction with the drawings in the embodiment of the present application. Among them, in the description of the embodiment of the present application, the terms used in the following embodiments are only for the purpose of describing a specific embodiment, and are not intended to limit the present application. In addition, in order to facilitate the clear description of the technical scheme of the embodiment of the present application, in the embodiment of the present application, if the words "first", "second" and the like are used to distinguish the same items or similar items with basically the same functions and effects. It can be understood by those skilled in the art that the words "first", "second" and the like do not limit the quantity and execution order, and the words "first", "second" and the like do not limit necessarily different. And, in the description of the embodiment of the present application, unless otherwise specified, the meaning of "multiple" means two or more.
[0081] A barcode is a graphic identifier that arranges multiple black bars and spaces of varying widths according to certain coding rules to express information related to an item. Common barcodes are parallel lines formed by black bars (referred to as bars) and white bars (referred to as spaces) with very different reflectivity. Barcodes can mark a lot of information such as the country of production, manufacturer, product name, production date, book classification number, mail start and end locations, categories, dates, etc., and are therefore widely used in many fields such as commodity circulation, book management, postal management, and banking systems.
[0082] For example, in offline retail scenarios, store clerks can scan product barcodes to enter product information, so that they can sell products and better manage product inventory, prices, and promotional information. At the same time, scanning barcodes can also track the source of products, which helps to ensure product quality.
[0083] However, due to the uniqueness of some item-related information, such as mobile phones, tablet computers and other electronic products, in addition to the product barcode (commonly known as 69 code), there will also be product serial numbers (SN), mobile equipment identifiers (MEID), international mobile equipment identities (IMEI) and other barcodes. Therefore, the outer packaging of these items usually carries a combination of multiple barcode information. For example, the outer packaging of electronic products such as mobile phones and tablet computers usually includes SN codes, MEID codes, IMEI codes, and 69 codes and other barcodes.
[0084] Figure 1 and Figure 2 , taking the outer packaging box of a mobile phone as an example, a schematic diagram of a barcode is shown. Figure 1 The white outer box is shown. Figure 2 Shown is the black outer box.
[0085] like Figure 1 and Figure 2 As shown, the outer packaging box of this mobile phone includes IMEI code, MEID code, SN code and 69 code. At the same time, the IMEI code also includes IMEI1 code and IME2 code, a total of five barcodes.
[0086] It should be noted that Figure 1 and Figure 2 The barcodes shown in the figure are only used for illustration of the embodiments of the present application. These barcodes are invalid barcodes drawn by themselves and do not involve any item information.
[0087] It can be seen that when there are multiple barcode combinations, the distribution of these multiple barcodes on the outer packaging will be relatively dense. In this case, the traditional barcode recognition method of directly scanning and decoding through barcode decoding tools (such as open source decoding tools such as Zxing and Zbar) will be difficult to quickly and accurately identify each barcode because the barcodes are densely packed and difficult to distinguish from each other, resulting in slow scanning speed and possible recognition failures such as barcodes being unable to read, resulting in reduced efficiency and accuracy of barcode scanning.
[0088] At the same time, the outer packaging boxes of some items are darker and deeper in color, such as black outer packaging boxes. In this case, because the barcode is mainly a graphic identifier composed of black bars, the dark background, especially the black background, is easily mistakenly recognized as being in the same color gamut as the barcode, thereby affecting the recognition speed and accuracy. In addition, according to the actual recognition results and experimental findings of outer packaging boxes of various colors, in the barcode recognition process of black outer packaging boxes, that is, barcodes with black backgrounds, the decoding tool is indeed prone to mistakenly recognize the black bars of the barcode and the black background as one. In other words, compared with barcodes with other background colors (such as light backgrounds such as white), barcodes with black backgrounds are more likely to be mistaken as being integrated with the barcode in terms of color gamut, which will affect the positioning effect of the barcode area, resulting in lower recognition effect and accuracy of barcodes in the black background scenario.
[0089] Based on this, an embodiment of the present application provides a barcode recognition method, which is applied to an electronic device, wherein the electronic device includes a camera. The camera in the electronic device is used to capture images, such as images including barcodes.
[0090] In short, in the barcode recognition method provided in the embodiment of the present application, after the electronic device receives the user's scanning operation, the electronic device will use the camera to capture an image including the barcode.
[0091] Afterwards, before the electronic device uses the decoding tool to locate and decode the barcode, the electronic device will first enhance the barcode by graying the image, inverting the image, fusing the original image and the inverted image, and binarizing the image based on an adaptive binarization threshold. Among them, the electronic device can enhance the barcode area and highlight the barcode by inverting the image, and separate different barcodes as much as possible, thereby enhancing the positioning effect of the barcode area and improving the speed and accuracy of barcode recognition.
[0092] At the same time, the electronic device can further reduce the influence of the black background on the barcode area through image inversion, image fusion and adaptive binarization threshold. Therefore, even if the barcodes are densely distributed and / or have a dark color, such as a black background, it can ensure a relatively good barcode positioning and decoding effect, thereby improving the speed and accuracy of barcode recognition in scenes with dense barcodes and / or black backgrounds.
[0093] In addition, the decoded information finally obtained by the electronic device can be displayed and fed back to the user. For example, after the electronic device responds to the user's scanning operation, it displays a first interface. This first interface includes the decoded information corresponding to the barcode.
[0094] It is understandable that the decoded information may include different contents according to different scenarios, for example, basic information of the item, inventory of the item, selling price, promotion information and sales promotion information related to the item, etc., which is not limited in any way in the embodiments of the present application. Figure 3 A schematic diagram of an interface of a first interface is shown.
[0095] Need to explain, Figure 3 The interface shown is only used for illustration of the embodiment of the present application. The specific interface design of the first interface depends on the actual interface design of the application. Figure 3 There is no limitation on the first interface and the decoding information.
[0096] The electronic device may be at least one of a mobile phone including a camera, a foldable electronic device, a tablet computer, a desktop computer, a laptop computer, a handheld computer, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a cellular phone, a personal digital assistant (PDA), an augmented reality (AR) device, a virtual reality (VR) device, an artificial intelligence (AI) device, a wearable device, an in-vehicle device, a smart home device, or a smart city device. The embodiment of the present application does not impose any special restrictions on the specific type of the electronic device.
[0097] Figure 4 A structural schematic diagram of an electronic device is shown.
[0098] like Figure 4 As shown, the electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) connector 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera module 193 (including a camera), a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. Among them, the camera module 193 includes a camera. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, and the like.
[0099] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0100] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.
[0101] The processor can generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions. In the embodiment of the present application, the barcode recognition method described in the embodiment of the present application can be implemented by the processor 110.
[0102] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 may be a cache memory. The memory may store instructions or data that have been used or are frequently used by the processor 110. If the processor 110 needs to use the instruction or data, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0103] In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc. The processor 110 may be connected to a touch sensor, an audio module, a wireless communication module, a display screen, a camera module, and the like through at least one of the above interfaces.
[0104] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present application is only a schematic illustration and does not constitute a structural limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.
[0105] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function. For example, files such as music and videos are stored in the external memory card. Or files such as music and videos are transferred from the electronic device to the external memory card.
[0106] The internal memory 121 can be used to store computer executable program codes, which include instructions. For example, executable program codes for implementing barcode recognition are stored. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as an image capture function, a sound playback function, an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 100 (such as image data, audio data, a phone book, etc.), etc. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc. The processor 110 executes various functional methods or data processing of the electronic device 100 by running instructions stored in the internal memory 121, and / or instructions stored in a memory provided in the processor.
[0107] The USB connector 130 is an interface that complies with the USB standard specification and can be used to connect the electronic device 100 and peripheral devices. The charging management module 140 is used to receive the charging input of the charger. The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. The power management module 141 receives the input of the battery 142 and / or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the display screen 194, the camera module 193, and the wireless communication module 160.
[0108] The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.
[0109] The electronic device 100 can realize the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which is connected to the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs, which execute program instructions to generate or change display information. Among them, the display screen 194 is used to display text, images, videos, etc.
[0110] For example, the electronic device 100 may perform image processing on the image captured by the camera, including the barcode, through the GPU, and then locate and decode the barcode, and then display the decoded information corresponding to the barcode on the display screen 194.
[0111] The electronic device 100 can realize the camera function through the camera module 193, ISP, video codec, GPU, display screen 194, application processor AP, neural network processor NPU, etc. For example, the camera function collects an image including a barcode.
[0112] The camera module 193 can be used to collect color image data and depth data of the photographed object (such as a barcode). The ISP can be used to process the color image data collected by the camera module 193. For example, when scanning a barcode, light is transmitted to the camera photosensitive element through the lens, and the light signal is converted into an electrical signal. The camera photosensitive element transmits the electrical signal to the ISP for processing and converts it into an image visible to the naked eye. The ISP can also perform algorithm optimization on the noise and brightness of the image. The ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, the ISP can be set in the camera module 193.
[0113] In some embodiments, the camera module 193 may be composed of a color camera module and a 3D sensing module.
[0114] In some embodiments, the photosensitive element of the camera of the color camera module can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, and then transmits the electrical signal to the ISP to be converted into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format.
[0115] In some embodiments, the 3D sensing module may be a time of flight (TOF) 3D sensing module or a structured light 3D sensing module. Among them, structured light 3D sensing is an active depth sensing technology, and the basic components of the structured light 3D sensing module may include an infrared emitter, an IR camera module, etc. The working principle of the structured light 3D sensing module is to first emit a light spot of a specific pattern to the object being photographed, and then receive the light spot pattern coding (light coding) on the surface of the object, and then compare the similarities and differences with the original projected light spot, and use the triangulation principle to calculate the three-dimensional coordinates of the object. The three-dimensional coordinates include the distance between the electronic device 100 and the object being photographed. Among them, TOF 3D sensing may be an active depth sensing technology, and the basic components of the TOF 3D sensing module may include an infrared emitter, an IR camera module, etc. The working principle of the TOF 3D sensing module is to calculate the distance (i.e., depth) between the TOF 3D sensing module and the object being photographed by the time when the infrared rays return to obtain a 3D depth map.
[0116] Structured light 3D sensing modules can also be used in face recognition, somatosensory game consoles, industrial machine vision inspection and other fields. TOF 3D sensing modules can also be used in game consoles, augmented reality (AR) / virtual reality (VR) and other fields.
[0117] In other embodiments, the camera module 193 may also be composed of two or more cameras. The two or more cameras may include a color camera, which can be used to collect color image data of the photographed object (such as a barcode). The two or more cameras may use stereo vision technology to collect depth data of the photographed object (such as a barcode). Stereo vision technology is based on the principle of human eye parallax. Under natural light, two or more cameras are used to capture images of the same object from different angles, and then triangulation and other operations are performed to obtain the distance information between the electronic device 100 and the photographed object, that is, the depth information.
[0118] In some embodiments, the electronic device 100 may include one or more camera modules 193. Specifically, the electronic device 100 may include one front camera module 193 and one rear camera module 193. The front camera module 193 may generally be used to collect color image data and depth data of the photographer facing the display screen 194, and the rear camera module may be used to collect color image data and depth data of the subject (such as a barcode, a person or scenery, etc.) facing the photographer.
[0119] In some embodiments, the CPU or GPU or NPU in the processor 110 can process the color image data and depth data collected by the camera module 193.
[0120] The electronic device 100 can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.
[0121] The buttons 190 may include a power button, a volume button, etc. The SIM card interface 195 is used to connect a SIM card. The motor 191 may generate a vibration prompt. The indicator 192 may be an indicator light, which may be used to indicate the charging status, the change in power, or may be used to indicate messages, missed calls, notifications, etc.
[0122] Specifically in the embodiment of the present application, when performing barcode recognition, the indicator light and / or the motor 191 can vibrate to indicate the stage of barcode recognition. For example, the indicator light or the motor 191 vibration can be used to indicate that the barcode scanning is completed, the barcode recognition is completed, etc. In some embodiments, the speaker 170A can also output a prompt sound, such as a "beep" sound, to indicate that the barcode scanning is completed, the barcode recognition is completed, etc.
[0123] In some embodiments, the software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. TM Taking the system as an example, the software structure of the electronic device 100 is exemplified.
[0124] Figure 5 A software structure block diagram of an electronic device is shown.
[0125] like Figure 5 As shown in FIG. 1 , the layered architecture divides the software into several layers, each with a clear role and division of labor. The layers communicate with each other through software interfaces. In some embodiments, Android TM The system is divided into five layers, from top to bottom: application layer, application framework layer, Android runtime (ART) and native C / C++ library, hardware abstraction layer (HAL) and kernel layer.
[0126] The application layer may include a series of application packages. In the embodiment of the present application, the application layer includes a code scanning application.
[0127] It can be understood that the code scanning application refers to an application with a code scanning function. In the embodiment of the present application, the code scanning application includes an image processing module, a color inversion module, a binarization module and a window calculation module.
[0128] Among them, the image processing module is used to perform image preprocessing, grayscale processing, morphological operations, etc. on the image. The inversion module is used to invert the image. The binarization module is used to calculate the adaptive binary threshold required for binarization, and binarize the image using the adaptive binary threshold. The window calculation module is used to calculate the adaptive window size required for morphological operations, and send the determined adaptive window size to the image processing module for morphological operations.
[0129] In some embodiments, the application package may also include applications such as gallery, calendar, map, WLAN, music, SMS, call, navigation, Bluetooth, video, etc. (not shown in the drawings).
[0130] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.
[0131] The application framework layer may include a window manager, an activity manager, an input manager, a resource manager, a notification manager, a view system, a content provider, and the like.
[0132] The window manager provides window management services (WMS). WMS can be used for window management, window animation management, surface management, and as a transfer station for the input system.
[0133] Content providers are used to store and retrieve data and make it accessible to applications. This data can include video, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
[0134] The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. A display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying images.
[0135] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
[0136] The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, a text message is displayed in the status bar, a prompt sound is emitted, an electronic device vibrates, an indicator light flashes, etc.
[0137] The Activity Manager can provide Activity Manager Service (AMS), which can be used to start, switch, and schedule system components (such as activities, services, content providers, and broadcast receivers) as well as manage and schedule application processes.
[0138] The input manager can provide input management services (IMS), which can be used to manage system input, such as touch screen input, key input, sensor input, etc. IMS takes events from input device nodes and distributes them to appropriate windows through interaction with WMS.
[0139] The Android runtime includes the core library and the Android runtime. The Android runtime is responsible for converting source code into machine code. The Android runtime mainly uses the ahead of time (AOT) compilation technology and the just in time (JIT) compilation technology.
[0140] The core library is mainly used to provide basic Java class library functions, such as basic data structures, mathematics, IO, tools, databases, networks, etc. The core library provides an API for users to develop Android applications.
[0141] The native C / C++ library may include multiple functional modules. For example: surface manager, media framework, function library (such as libc), graphics library (such as OpenGL ES), database (such as SQLite), browser engine (webkit), etc. Among them, the surface manager is used to manage the display subsystem and provides fusion of 2D and 3D layers for multiple applications. The media framework supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc. OpenGL ES provides drawing and operation of 2D graphics and 3D graphics in applications. SQLite provides a lightweight relational database for applications of electronic device 100.
[0142] The hardware abstraction layer runs in user space, encapsulates kernel layer drivers, and provides a calling interface to the upper layer. The kernel layer is the layer between hardware and software.
[0143] like Figure 5 As shown, the hardware abstraction layer may include display HAL, audio HAL, camera HAL, sensor HAL, etc. The kernel layer at least includes display driver, camera driver, audio driver, and sensor driver.
[0144] The following is an illustrative description of the workflow of the software and hardware of the electronic device 100 in conjunction with a barcode recognition scenario.
[0145] When the sensor receives the user's operation (such as the touch sensor 180K receives the touch operation), the corresponding hardware interrupt is sent to the kernel layer. The kernel layer processes the user's operation into a raw input event (such as information including touch coordinates, timestamp of the touch operation, etc.). The raw input event is stored in the kernel layer. The application framework layer obtains the raw input event from the kernel layer and identifies the control corresponding to the input event. Assuming that the touch operation is a touch click operation, and the control corresponding to the click operation is a code scanning control in the code scanning application, the code scanning application will call the interface of the application framework layer, and then start the camera driver by calling the kernel layer, capture the image with the barcode through the camera, and feed back the image collected by the camera to the upper layer code scanning application. Then, the code scanning application locates and decodes the barcode through a series of image operations such as grayscale processing, inversion processing, fusion of the original image and the inverted image, binarization of the image based on an adaptive binarization threshold, and morphological operation of the image based on an adaptive window size, thereby obtaining the decoding information of the barcode. After the code scanning application obtains the decoded information, it displays the decoded information on the display screen 194 through the display driver.
[0146] The barcode recognition method proposed in the embodiment of the present application will be described in detail below with reference to the accompanying drawings. It should be noted that the barcode recognition method in the following embodiments can be implemented in the electronic device 100 having the above hardware structure.
[0147] Figure 6 A flow chart of a barcode recognition method is shown, which is applied to an electronic device including a camera, and includes steps S601-S608. Figure 6 The barcode recognition method provided in the embodiment of the present application is described.
[0148] S601, the electronic device captures a first image in response to a code scanning operation by a user.
[0149] In the embodiment of the present application, the first image can be understood as an image including a barcode to be identified. The first image can include one or more barcodes. Moreover, the first image is acquired by a camera in an electronic device. The barcode scanning operation is an operation that triggers the electronic device to scan a barcode.
[0150] For example, the code scanning operation may be an operation in which a user opens a code scanning application and clicks a code scanning control in the application interface of the code scanning application. The code scanning control is a functional control used to trigger the code scanning application to call the underlying camera to capture a barcode image.
[0151] In the embodiment of the present application, the code scanning application can be an application with a code scanning function dedicated to managing item information, such as a retail application. Specifically, the retail code scanning application can be an application used in offline retail stores of electronic products such as mobile phones and tablet computers to sell, cash register, query and manage inventory of these electronic products.
[0152] The electronic device pre-installs the code scanning application, and after the electronic device receives a code scanning operation of the user clicking a code scanning control in the code scanning application, the electronic device responds to the code scanning operation, captures the first image and starts decoding the barcode. From the user's perspective, after the clerk operates the electronic device to scan the barcode on the electronic product packaging box, the electronic device can quickly display the decoding information of the barcode, so that the clerk can quickly understand and manage the relevant information of the electronic product.
[0153] It should be noted that the code scanning application is a retail application only for illustrative purposes of the embodiments of the present application, and does not constitute any limitation on the application and recognition scenario of barcode recognition in the embodiments of the present application. For example, the code scanning application can also be other third-party applications with a code scanning function. For example, social applications and video applications with a "scan" function.
[0154] S602: The electronic device performs grayscale processing on the first image to obtain a grayscale image (second image).
[0155] Grayscale processing of images usually refers to converting color images into grayscale images. In grayscale images, each pixel in the image contains only one grayscale value, rather than the three channels of red, green and blue in color images. By grayscale processing of images, image processing and analysis can be simplified. At the same time, it can provide an image basis for subsequent binarization.
[0156] Therefore, in the embodiment of the present application, when the captured first image is a color image, the electronic device can obtain a grayscale image corresponding to the first image by grayscale processing the first image, that is, the grayscale image (second image) in the embodiment of the present application.
[0157] It can be understood that if the first image captured by the electronic device is itself a grayscale image, then the second image is the first image, and the electronic device does not need to perform additional grayscale processing on the first image.
[0158] like Figure 7 As shown, the embodiment of the present application shows a schematic diagram of grayscale processing. It should be noted that the grayscale of the image in the embodiment of the present application can adopt any existing grayscale processing method. For example, the image grayscale can adopt any of the maximum value method, the average value method, and the weighted average method, and the embodiment of the present application does not make any limitation on this.
[0159] In some embodiments, while the electronic device performs grayscale processing on the first image, it can also perform other image preprocessing on the first image based on actual needs to enhance image quality.
[0160] In a specific embodiment, the electronic device may also perform image scaling and image noise filtering on the first image. Image scaling may adjust the size of the image to suit different application scenarios and requirements. In the embodiment of the present application, since barcode recognition is applied to electronic devices equipped with a barcode scanning function, and the resolutions of different electronic devices may be inconsistent due to factors such as the model and manufacturer of the electronic device. Therefore, in order to ensure the effect of subsequent barcode recognition, the electronic device may pre-scale the first image to a specified size through image scaling.
[0161] It is understandable that the specific scaling size can be set according to actual needs, and the present application embodiment does not impose any limitation on this. It should be noted that in the present application embodiment, image scaling can adopt any existing image scaling algorithm, such as an interpolation algorithm and a transformation-based algorithm, and the present application embodiment does not impose any limitation on this.
[0162] Image noise filtering can also be called image denoising or image noise reduction. Generally speaking, because digital images are often disturbed by external environmental noise during the digitization and transmission process, the effect of image noise on the image can be filtered out through image noise filtering. Specific to the embodiment of the present application, because the outer packaging box of the product is the area that is in direct contact with the outside world, the outer packaging box will inevitably have some stains. If the position of the stain coincides with the area where the barcode is located, it will easily affect the positioning and decoding recognition of the barcode by the electronic device. Therefore, the embodiment of the present application can reduce the image noise impact caused by the stain by performing image noise filtering on the first image, thereby avoiding affecting the positioning and recognition of subsequent barcodes, so as to improve the accuracy of subsequent barcode positioning and recognition.
[0163] In the embodiment of the present application, the image noise filtering may adopt any existing image filtering algorithm. For example, the electronic device may adopt any filtering algorithm such as Gaussian filtering, mean filtering, median filtering, wavelet denoising, and adaptive Wiener filtering to filter the image noise of the first image, and the embodiment of the present application does not impose any limitation on this.
[0164] In a specific embodiment, the electronic device can realize the processing such as image scaling, image grayscale processing, image noise filtering, etc. through an open source computer vision library, such as OpenCV. It should be noted that the image preprocessing in the embodiment of the present application may not be limited to the above-mentioned image scaling and image noise filtering. For example, based on the actual quality requirements for the first image, the image preprocessing may also include feature extraction, image segmentation, image matching, etc., which can be specifically configured according to actual needs, and the embodiment of the present application does not impose any limitation on this.
[0165] S603, the electronic device performs image inversion on the grayscale image (second image) to obtain an inverted image (third image).
[0166] Image inversion can also be called image negation, image inversion, etc. Simply put, if it is a black and white pixel, image inversion is to convert white pixels to black pixels, and black pixels to white pixels. From the perspective of pixel value, after the image is pixel normalized, the pixel value of each pixel in the image is 0 to 1 (still between 0 and 255). Among them, the pixel value of black pixels is 0, and the pixel value of white pixels is 1. Then, the inversion of black and white pixels is 0 to 1, and 1 to 0. If it is a gray pixel, the pixel normalization is 1 minus the original pixel value, which is the inversion.
[0167] In general, inverting the grayscale image (second image) after pixel normalization means converting the pixel value 0 in the grayscale image (second image) to the pixel value 1, and converting the pixel value 1 to the pixel value 0. The gray pixel value between 0 and 1 is 1 minus the original pixel value. For example, if the original pixel value is 0.5, the pixel value after inversion is still 0.5. For another example, if the original pixel value is 0.6, the pixel value after inversion is 0.4. Similarly, the pixel value of a pixel with a pixel value of 0.3 is 0.7 after inversion.
[0168] It can be understood that if the image is an image that has not been pixel-normalized, the image inversion is to subtract the original pixel value from the pixel value 255. For the convenience of describing the scheme, the grayscale images (second images) mentioned below in the embodiments of the present application are all pixel-normalized images.
[0169] like Figure 8 As shown, the embodiment of the present application shows a schematic diagram of image inversion. It can be understood that the image on the left is a grayscale image (second image) and the image on the right is an inverted image (third image). Hereinafter, the embodiment of the present application will use G(x, y) to represent the grayscale image (second image) and G′(x, y) to represent the inverted image (third image). Where x and y are the coordinates of the pixel points of the image.
[0170] In the embodiment of the present application, since the black pixels will be transformed into white pixels after the image is inverted, for a barcode with a black background, the electronic device can transform the black background of the barcode into a white background by inverting the image, thereby reducing the influence of the black background and enhancing the positioning effect of the barcode area.
[0171] S604: The electronic device performs image fusion on the grayscale image (second image) and the inverted color image (third image) to obtain a fourth image.
[0172] In actual situations, the colors of outer packaging boxes include dark-colored packaging boxes such as black and light-colored packaging boxes such as white. Figure 1 and Figure 2 shown.
[0173] Therefore, if the barcode to be identified is light-colored, such as a barcode on a white packaging box, that is, if a barcode with a white background is encountered. The electronic device will convert the original white background into a black background through the above-mentioned image inversion operation. Therefore, the black background caused by the image inversion may affect the recognition efficiency and accuracy of the barcode. Therefore, the electronic device of the embodiment of the present application further performs image fusion on the grayscale image (second image) and the inverted image (third image) to obtain a fused fourth image. Subsequently, the electronic device directly performs adaptive binarization processing, barcode positioning and decoding and other operations on the fourth image obtained by image fusion.
[0174] In this way, no matter whether the barcode in the original image has a white background or a black background, as long as the image is inverted and fused, the fourth image can have the image features of the barcode image with a white background, thereby improving the barcode recognition effect.
[0175] That is to say, for the barcode on the black outer packaging box, because the image is inverted to obtain a barcode image with a white background, the image features of the barcode with a white background can be added after the image fusion, thereby reducing the impact of the black background.
[0176] As for the barcode on the white outer packaging box, due to further image fusion, even if the original image is inverted, the image features of the original barcode with a white background can be retained, so the effect and accuracy of barcode recognition on the white packaging box will not be affected by the image inversion.
[0177] In some embodiments, in order to further enhance the positioning effect of the barcode, the edge detection of the barcode in the image can be performed to highlight the barcode before image fusion. At the same time, based on the characteristic that the barcode is a straight line rather than an arbitrary shape contour, the edge detection performed by the electronic device for image fusion in the embodiment of the present application can mainly adopt the edge detection method of performing horizontal edge detection and vertical edge detection on the image.
[0178] In a specific embodiment, the electronic device may implement horizontal edge detection and vertical edge detection of the barcode by calculating an approximate gradient of the image using a discrete differentiation operator.
[0179] Based on this, S604, performing image fusion on the grayscale image (second image) and the inverted image (third image) to obtain a fourth image may include: using a discrete differential operator to calculate the approximate gradient of the second image to obtain a first gradient image, and using a discrete differential operator to calculate the approximate gradient of the third image to obtain a second gradient image; performing weighted summation on the first gradient image and the second gradient image to obtain the fourth image.
[0180] Specifically, after the electronic device obtains the grayscale image (second image) and the inverted image (third image), it first uses a discrete differential operator to calculate the approximate gradients corresponding to the grayscale image (second image) and the inverted image (third image) to obtain the corresponding gradient image.
[0181] It can be understood that, from the perspective of data, the image processing in the electronic device is actually a standard matrix. Therefore, the approximate gradient of the second image and the third image calculated by the electronic device is a gradient matrix, and the gradient matrix is actually a gradient image. In the embodiment of the present application, for the sake of distinction, the gradient image of the grayscale image (second image) is called the first gradient image. The gradient image of the inverted image (third image) is called the second gradient image.
[0182] It should be noted that there are many types of discrete differential operators, and the embodiments of the present application may use any existing discrete differential operator. For example, the Sobel operator, Robert operator, or Laplace operator may be used to calculate the approximate gradient, and the embodiments of the present application do not impose any limitation on this.
[0183] After the electronic device obtains the first gradient image and the second gradient image by calculating the approximate gradient, it can perform image fusion on the first gradient image and the second gradient image.
[0184] For example, the electronic device image fusion may adopt a fusion method of feature addition or feature merging to achieve image fusion. In a specific embodiment, the electronic device may complete image fusion by weighted summing the first gradient image and the second gradient image to obtain a fourth image.
[0185] The fusion formula for performing image fusion by weighted summation in the embodiment of the present application is: DG(x, y)=αD(G)+βD(G′).
[0186] Where D(G) is the approximate gradient of the grayscale image (second image) G(x,y), i.e., the first gradient image. D(G′) is the approximate gradient of the inverse image (third image) G′(x,y), i.e., the second gradient image. α is the first weight of the first gradient image D(G), and β is the second weight of the second gradient image D(G′).
[0187] It should be noted that the first weight and the second weight used in the embodiment of the present application are determined based on the image effect actually required for the barcode recognition business through experiments and repeated deductions. Therefore, the specific values of the first weight α and the second weight β can be set according to the actual test and deduction results, and the embodiment of the present application does not impose any limitation on this.
[0188] For example, the first weight α and the second weight β in the embodiment of the present application may specifically be weights determined in a scenario of identifying a barcode on an outer packaging box of an electronic product.
[0189] Specifically, since the black background will affect the barcode recognition, when the outer packaging box is white, that is, when the original background color of the barcode is a white background, the weight of the original image can be higher than the weight of the inverted image. That is, in this case, the first weight α can be set to be greater than the second weight β. If the outer packaging box is black, then the background color of the barcode after inverting is the white background with a better recognition effect, and the weight of the inverted image can be higher than the weight of the original image. That is, in this case, the second weight β can be set to be greater than the first weight α.
[0190] In a specific embodiment, for barcode recognition on the outer packaging of electronic products such as mobile phones and tablet computers, after repeated tests using the outer packaging, it is found that in this scenario, whether it is a white packaging box or a black packaging box, when the first weight α is less than the second weight β, a good barcode recognition effect can be obtained. Therefore, in this scenario, a set of weights in which the first weight α is less than the second weight β can be fixedly set in the electronic device.
[0191] In other embodiments, according to the actual barcode recognition business scenario, if it is impossible to determine only one set of weights through experiments, multiple sets of weights can be configured at the same time, where each set of weights includes a pair of first weights α and second weights β. Then, the electronic device can perform image fusion in parallel based on each set of weights to obtain a fourth image corresponding to each set of weights. Subsequently, subsequent binarization, morphology, positioning and decoding operations are performed in parallel based on multiple fourth images to obtain the decoding information of the barcode.
[0192] S605: The electronic device performs binarization processing on the fourth image according to an adaptive binarization threshold value to obtain a binarized image (fifth image).
[0193] After the electronic device obtains the fourth image through image fusion, since the barcode decoding tool needs to input a binary image, the electronic device binarizes the fourth image to obtain a binary image (fifth image) having only black and white colors.
[0194] In the embodiment of the present application, in order to further eliminate the influence of the black background, the barcode area is highlighted to facilitate the positioning of the barcode area. The binarization threshold used by the electronic device to binarize the fourth image is an adaptive binarization threshold obtained based on the actual situation of each pixel value in the fourth image.
[0195] Specifically, the adaptive binarization threshold in the embodiment of the present application is mainly determined based on the first pixel mean. The first pixel mean is the average of multiple second pixel means, and the second pixel mean is the average of each pixel value in different pixel ranges in the fourth image. It can be understood that each pixel range corresponds to a second pixel mean, so the second pixel mean corresponds to the pixel range one by one.
[0196] In the binarization process of an image, although the existing binarization threshold value also obtains an adaptive binarization threshold value based on the value of each pixel in the image, the adaptive binarization threshold value obtained by the existing binarization is usually the average value of all pixel values in the image.
[0197] However, the traditional method of directly taking the average value of all pixel values as the adaptive binarization threshold can only work well in the scenario where the pixel colors of the image are divided into half. In other words, if it is directly applied to the barcode recognition scenario in the embodiment of the present application, a relatively good barcode recognition effect can only be achieved when the black and white pixels in the barcode image are evenly distributed, such as when the black and white pixels are divided into half.
[0198] However, based on the actual situation, no matter what kind of item, the proportion of the barcode on the existing outer packaging box is usually relatively small. That is, the area occupied by the barcode is relatively small, and most of the area will be occupied by the background color. Therefore, the scene of existing barcode recognition rarely has the situation where black and white pixels are divided in half. Therefore, if the adaptive binarization threshold is directly obtained in the traditional way to binarize the fourth image, the recognition effect of the barcode will still be affected by the dark background, especially the black background color.
[0199] Therefore, the embodiment of the present application is mainly based on dividing the pixel values of the fourth image into different pixel ranges. Then, the average value of each pixel value in each pixel range is calculated respectively, so as to obtain multiple second pixel means. Then, the electronic device calculates the average value of the multiple second pixel means to obtain the first pixel mean. Then, the electronic device determines whether the candidate binarization threshold can be used as the adaptive binarization threshold based on the first pixel mean.
[0200] Thus, compared to the simple pixel mean, since the binarization threshold is determined by dividing the pixel range to determine the mean, compared to simply using the mean of all pixel values as the binarization threshold, this method in the embodiment of the present application can better take into account the situation where the pixel color distribution is uneven. That is, this method can better take into account the situation where the barcode background color occupies too much, specifically, the situation where the black background occupies most of the area, thereby obtaining a more reasonable and more suitable adaptive binarization threshold in the barcode recognition scenario, thereby improving the barcode recognition effect.
[0201] In a specific embodiment, S605, binarizing the fourth image according to the adaptive binarization threshold to obtain a binarized image (fifth image) may include:
[0202] The pixel values in the fourth image are divided based on the candidate binarization threshold as the division basis to obtain a first pixel value range and a second pixel value range; the mean of the first pixel value range and the mean of the second pixel value range are respectively calculated to obtain multiple second pixel means; the average of the multiple second pixel means is calculated to obtain the first pixel mean; when the error between the candidate binarization threshold and the first pixel mean is less than the error threshold, the candidate binarization threshold is used as an adaptive binarization threshold to binarize the fourth image to obtain a fifth image.
[0203] The initial candidate binarization threshold t1 is a preset binarization threshold, which can be set based on experience and actual needs, and the present embodiment does not impose any limitation on this. For example, in the present embodiment, the initial candidate binarization threshold t1=0.5.
[0204] After the electronic device obtains the fourth image, the pixel values of the fourth image are divided into two parts based on the initial candidate binary threshold value t1 as the division basis. That is, the pixel values in the fourth image are divided based on the candidate binary threshold value t1 as the division basis to obtain the first pixel value range A and the second pixel value range B. In other words, the pixel values in the fourth image that are greater than the candidate binary threshold value t1 can be divided into one set, and the pixel values that are less than the candidate binary threshold value t1 are divided into another set.
[0205] For example, the pixel values in the first pixel value range A may be pixel values greater than the candidate binarization threshold t1, and the pixel values in the second pixel value range B may be pixel values less than the candidate binarization threshold t1.
[0206] Alternatively, the pixel values in the first pixel value range A may be pixel values less than the candidate binarization threshold t1, and the pixel values in the second pixel value range B may be pixel values greater than the candidate binarization threshold t1. Pixel values equal to the candidate binarization threshold t1 may be classified into the first pixel value range A or the second pixel value range B.
[0207] Then, the electronic device calculates the average value of all pixel values in the first pixel value range A to obtain the second pixel average PA. At the same time, the electronic device calculates the average value of all pixel values in the second pixel value range B to obtain the second pixel average PB. The electronic device then calculates the average value of the second pixel average PA and the second pixel average PB to obtain the first pixel average t2.
[0208] Therefore, the calculation formula of the first pixel average t2 can be expressed as: t2=(PA+PB) / 2.
[0209] Then, if the error between the candidate binarization threshold t1 and the first pixel mean t2 is less than or equal to the error threshold △t, then the candidate binarization threshold t1 can be used as the adaptive binarization threshold t, and then the adaptive binarization threshold t, that is, the candidate binarization threshold t1, is used to binarize the fourth image to obtain a binarized image (fifth image).
[0210] The error threshold Δt is preset based on experience and actual needs, and the present embodiment does not impose any limitation on this. For example, in the present embodiment, the error threshold Δt=0.1.
[0211] However, if the error between the candidate binarization threshold t1 and the first pixel mean t2 is greater than the error threshold △t, it means that the current candidate binarization threshold t1 does not meet the requirements. The electronic device uses the current first pixel mean t2 as the new candidate binarization threshold t1. That is, the electronic device assigns t2 to t1, and t2=t1 at this time. Then, the electronic device uses the new candidate binarization threshold t1 (i.e., the previous first pixel mean t2) to re-determine the next first pixel mean t2 in the same processing method as above, and then determines whether the new candidate binarization threshold t1 (i.e., the previous first pixel mean t2) can be used as the adaptive binarization threshold t through the error threshold.
[0212] That is to say, the electronic device re-divides the pixel range of the pixel values in the fourth image with the new candidate binarization threshold t1 (i.e., the previous first pixel mean t2) as the division basis to obtain two new pixel value ranges. Then, the average values corresponding to the two new pixel value ranges are calculated to obtain multiple new second pixel means, and the first pixel mean t2 is obtained by taking the average value of the new multiple second pixel means. Then, based on the error between the new candidate binarization threshold t1 (i.e., the previous first pixel mean t2) and the current new first pixel mean t2, it is determined whether the new candidate binarization threshold t1 (i.e., the previous first pixel mean t2) can be used as the adaptive binarization threshold t. The principle is the same and will not be repeated here.
[0213] In general, when the electronic device determines that the candidate binarization threshold t1 does not meet the error requirement, it reassigns the candidate binarization threshold t1, and then iterates in the same way to determine a new candidate binarization threshold t1 until the candidate binarization threshold t1 that meets the error requirement is obtained as the adaptive binarization threshold t.
[0214] For example, assuming that the initial t1=0.5, t2=0.7, and the error threshold △t=0.1. At this time, the error between t1 and t2 is 0.2, which is greater than △t=0.1. Therefore, the electronic device reassigns the value, and t1=0.7 is obtained. Furthermore, the electronic device recalculates t2=0.8 or t2=0.6 through t1=0.7. At this time, the error between t1 and t2 is 0.1, which is equal to △t=0.1, meeting the error requirement. Then, t1=0.7 can be used as the adaptive binarization threshold t.
[0215] For another example, assuming that the initial t1=0.5, t2=0.3, and the error threshold △t=0.1, the error between t1 and t2 is 0.2, which is greater than △t=0.1. Therefore, the electronic device reassigns the value, and t1=0.3 is calculated by the electronic device through t1=0.3 to obtain t2=0.2 or t2=0.4. At this time, the error between t1 and t2 is 0.1, which is equal to △t=0.1, and the error requirement is met. Then, t1=0.3 can be used as the adaptive binarization threshold t.
[0216] The process of the electronic device binarizing the fourth image according to the determined adaptive binarization threshold t is as follows: the pixel values in the fourth image are compared one by one with the adaptive binarization threshold t. If a certain pixel value is less than the adaptive binarization threshold t, then the pixel value is assigned 0. If a certain pixel value is greater than the adaptive binarization threshold t, then the pixel value is assigned 1. After all pixels are assigned binary values, a binary image (fifth image) is obtained. It can be understood that the pixel value of each pixel in the binary image (fifth image) is 1 or 0.
[0217] S606: The electronic device performs a morphological operation on the binary image (fifth image) to obtain a sixth image.
[0218] After the electronic device obtains the binary image (fifth image), it performs morphological operations on the binary image (fifth image). The morphological operations include expansion, erosion, and opening and closing operations. The barcode area can be further enhanced by expansion and erosion, thereby improving the effect of barcode positioning, thereby improving the decoding speed and accuracy. The opening and closing operations can remove the influence of characters, thereby removing the influence of characters carried in the barcode image, thereby further improving the accuracy of barcode recognition. Fig. 9A schematic diagram showing a barcode image after morphological operation is performed.
[0219] In some embodiments, in view of the straight line characteristic of the barcode, when the electronic device performs morphological operations on the binary image (fifth image), it can perform targeted lateral expansion and longitudinal erosion, thereby enhancing the positioning effect of the barcode area.
[0220] In the embodiment of the present application, when the electronic device performs morphological operations on an image, the window size required for the morphological operation can be adaptively determined based on the proportion of the barcode in the image. Thus, by configuring windows of different sizes according to the different sizes of the barcodes in the image, morphological operations can be performed to adapt to the distribution of different types of barcodes, so as to improve the effect of subsequent barcode positioning and recognition.
[0221] In a specific embodiment, the electronic device determines the adaptive window size, which may include:
[0222] According to pixel rows, the binary image (fifth image) is divided into n image blocks; wherein n≥2, and n is an integer; different image blocks include an equal number of pixel columns; for each image block, the pixel values in each pixel column in the image block are added respectively to obtain a one-dimensional array corresponding to the image block; the one-dimensional array is binarized using an adaptive binarization threshold to obtain a one-dimensional binary array; in each one-dimensional binary array, the continuous length of values that are identical and continuous is determined to obtain a reference shortest continuous length corresponding to each image block; the minimum value of the reference shortest continuous lengths corresponding to the n image blocks is used as a target shortest continuous length; or, the product of the minimum value of the reference shortest continuous lengths corresponding to the n image blocks and a preset coefficient is used as the target shortest continuous length; and the determined target shortest continuous length is used as the adaptive window size.
[0223] Fig.10 A flow chart for determining the adaptive window size is shown. Fig.10 The process of determining the adaptive window size by the electronic device in the embodiment of the present application is described in detail.
[0224] When the electronic device determines the adaptive window size, it first divides the binary image into n blocks in the vertical direction. Fig.10 As shown, taking the corresponding image matrix as an example, in the vertical direction, it can be understood that the image is divided into n image blocks according to pixel rows. Fig.10 As shown, the image is divided into n=5 image blocks in total.
[0225] It is understandable that, because the electronic device is divided vertically according to pixel rows, the number of pixel columns included in each image block is equal. In other words, the number of pixel columns included in the binary image is the number of pixel columns included in each image block. The number of pixel columns included in each image block is equal. As for whether the number of pixel rows included in each image block is equal, it needs to be determined based on the total number of pixel rows in the image and the number of image blocks to be divided. Among them, n is determined according to actual needs and experimental results, and the embodiments of the present application do not impose any restrictions on this. For example, in a specific embodiment, n=4, that is, the binary image can be divided into 4 image blocks.
[0226] That is, only when the number of pixel rows is a multiple of the number of image blocks to be divided, can the number of pixel rows included in each image block be evenly divided so that the number is equal. When the number of pixel rows in an image block cannot be completely evenly divided, the electronic device can divide it by rounding up or rounding down.
[0227] For example, suppose the binary image has 100 pixel rows in total, and needs to be divided into n=5 image blocks. Then, in this case, it can be divided evenly, that is, each divided image block includes 20 pixel rows. If the binary image has 100 pixel rows in total, but can only be divided into n=3 image blocks. Then, in this case, the number of pixel rows in each image block cannot be divided evenly. At this time, the division can be performed by rounding up or rounding down.
[0228] If rounded up, the first image block will have one more pixel row than the other image blocks. That is, the first image block includes 1 to 34 rows, the second image block includes 35 to 67 rows, and the third image block includes 68 to 100 rows. If rounded down, the last image block will have one more pixel row than the other image blocks. That is, the first image block includes 1 to 33 rows, the second image block includes 34 to 66 rows, and the third image block includes 67 to 100 rows.
[0229] After the image blocks are divided, the electronic device adds the pixel values in each image block in columns, thereby obtaining a one-dimensional array corresponding to the image block. It can be understood that since the pixel values are added in columns, that is, the pixel values in each column are added, the number of values in the one-dimensional array is equal to the number of pixel columns.
[0230] like Fig.10As shown, the embodiment of the present application takes the fifth image block as an example to show a part of pixel values. Thus, the electronic device sums the pixel values in the fifth image block according to the pixel columns to obtain a one-dimensional array E5A. Similarly, the electronic device can obtain a one-bit array corresponding to each image block in the same way. For example, E1A, E2A, E3A and E4A can be obtained in the same way as E5A, and the principles are the same, which will not be repeated in the embodiment of the present application.
[0231] The electronic device then performs binarization processing on each one-dimensional array based on the above determined adaptive binarization threshold t to obtain a corresponding one-dimensional binary array. Specifically, the electronic device first calculates the quotient of each value in the one-dimensional array E5A and the total number of pixels in the corresponding pixel column.
[0232] For example, the first value 2 in the one-dimensional array E5A corresponds to a total number of pixels in a pixel column of 4, so the quotient is 2 / 4=0.5. For another example, the second value 3 in the one-dimensional array E5A corresponds to a total number of pixels in a pixel column of 4, so the quotient is 3 / 4=0.75. For another example, the third value 4 in the one-dimensional array E5A corresponds to a total number of pixels in a pixel column of 4, so the quotient is 4 / 4=1.
[0233] Finally, the electronic device compares the quotient corresponding to each value with the adaptive binarization threshold t to obtain a value. If the average value is less than or equal to the adaptive binarization threshold t, the value is 0; if the average value is greater than the adaptive binarization threshold t, the value is 1. Fig.10 Similarly, the electronic device can calculate E1B, E2B, E3B and E4B corresponding to E1A, E2A, E3A and E4A in the same way, the principle is the same, and the embodiment of the present application will not be repeated here.
[0234] It should be noted that, because the adaptive binarization threshold t is unclear, Fig.10 The two arrays E5A and E5B are only used for illustration in the present application, and E5A and E5B may not be corresponding correct calculation results. It can be understood that E5A and E5B may not constitute a corresponding relationship.
[0235] After the electronic device determines the one-dimensional binary array corresponding to each image block, that is, for each one-dimensional binary array, the continuous length of the values with the same and continuous values in the one-dimensional binary array is determined, thereby obtaining the shortest continuous length of each one-dimensional binary array. For the convenience of distinction, this shortest continuous length in the one-dimensional binary array is referred to as the reference shortest continuous length in the embodiment of the present application.
[0236] like Fig.10As shown, taking E5B as an example, the continuous lengths determined by the electronic device from left to right include: L51=2, L52=1, L53=3...L59=6,... Then, the reference shortest continuous length corresponding to E5B is 1. In the same way, the electronic device calculates the reference shortest continuous length corresponding to each one-dimensional binary array. It can be understood that the one-dimensional binary array corresponds to the image block one-to-one, so each image block has a corresponding reference shortest continuous length.
[0237] Then, the electronic device determines a minimum value from the reference shortest continuous lengths corresponding to all image blocks, and the minimum value is referred to as the target shortest continuous length in the embodiment of the present application. The electronic device uses the target shortest continuous length as the determined adaptive window size.
[0238] The calculation formula for the reference shortest continuous length gap (EiB) is as follows:
[0239] gap(EiB)=(Li1,Li2,…Lim).
[0240] Where i=1,2…n. L11,L12,…L1m represent the continuous lengths corresponding to the one-dimensional binary array, and m is the total number of continuous lengths. For example, Fig.10 As shown, gap(E5B)=L52=1.
[0241] The target shortest continuous length, that is, the calculation formula of the adaptive window size w is as follows:
[0242] W=δmin(gap(E1B), gap(E2B)…gap(EiB)).
[0243] Among them, δ is a preset coefficient, which can be determined according to actual needs and test results, and the embodiment of the present application does not impose any limitation on this. For example, in the embodiment of the present application, δ=1. It can be understood that when δ=1, the target shortest continuous length is directly the minimum value of the reference shortest continuous lengths corresponding to the n image blocks.
[0244] In the embodiment of the present application, compared with the traditional manual setting of the corresponding window size based on different images, the embodiment of the present application can calculate the continuous length of pixels through the electronic device to reflect the size and distribution of the object (such as a barcode) and then automatically determine the window size, thereby obtaining an adaptive window size. As a result, the electronic device can improve efficiency while ensuring the morphological effect, thereby improving the speed and accuracy of barcode recognition.
[0245] S607, the electronic device performs barcode positioning and decoding on the sixth image to obtain decoding information.
[0246] After the electronic device obtains the sixth image through morphological operation, it can perform edge detection on the sixth image to locate the barcode area to obtain the seventh image. Fig.11 A schematic diagram of locating a barcode area is shown.
[0247] like Fig.11 As shown, the white dotted frame is the barcode positioning frame. It should be noted that in the embodiment of the present application, this step is mainly to locate the barcode area, so the edge detection used in this processing step may not be limited to discrete differential operators. For example, the electronic device may also use the candy edge detection algorithm to locate the barcode on the sixth image.
[0248] Then, the electronic device inputs the seventh image into a barcode decoding tool. For example, the electronic device can input the seventh image into an open source decoding tool such as Zxing or Zbar. The barcode decoding tool such as Zxing or Zbar decodes the seventh image, thereby parsing and obtaining the decoding information of the barcode.
[0249] In some embodiments, if the image includes multiple barcodes at the same time, such as Figure 7 As shown, five barcodes may be included at the same time. Then, barcode decoding tools such as Zxing and Zbar can perform parallel decoding of multiple barcodes on the seventh image.
[0250] In the embodiment of the present application, after the sixth image is obtained by enhancing the barcode area and reducing the influence of the black background on recognition through the above series of image processing, the sixth image is subjected to edge detection to locate the barcode area and input to the decoding tool. As a result, the decoding tool can receive a high-quality barcode image in which the barcode has been located, the barcode is prominent, and the decoding tool can directly and quickly decode without having to locate the barcode itself, thereby improving the speed and accuracy of barcode recognition.
[0251] S608, the electronic device displays a first interface, where the first interface includes decoding information.
[0252] After the electronic device obtains the decoding information corresponding to the barcode through the decoding tool, it can display the first interface on the display screen. The decoding information is displayed on the first interface to facilitate the user who triggers the code scanning to quickly view the information related to the barcode.
[0253] Fig.12 Taking the interaction of each module as an example, a sequential interaction diagram of a barcode recognition method is shown. Fig.12 The barcode recognition method provided in the embodiment of the present application is described.
[0254] like Fig.12As shown, after the image processing module receives the first image captured by the camera, it first performs image preprocessing on the first image. For example, the image preprocessing may include image scaling and image noise filtering. At the same time, if the first image is a color image, the image processing module needs to grayscale the first image to obtain a grayscale image (second image).
[0255] Then, the image processing module transmits the grayscale image (second image) to the color inversion module, and the color inversion module performs image inversion and image fusion on the grayscale image (second image).
[0256] The color inversion module can obtain the inverted image (third image) corresponding to the grayscale image (second image) by inverting the image. Further, the color inversion module fuses the inverted image (third image) with the grayscale image (second image) to obtain a fused image (fourth image).
[0257] In the embodiment of the present application, the inversion module can perform image fusion by respectively calculating the approximate gradients of the grayscale image (the second image) and the inversion image (the third image), obtaining the corresponding first gradient image and the second gradient image, and then performing weighted summation of the first gradient image and the second gradient image. The specific implementation method of image fusion can refer to the description of the above embodiment, the principle is the same, and the embodiment of the present application will not be repeated here.
[0258] Next, after the inversion module completes the image inversion and image fusion, the final fused image (fourth image) is sent to the binarization module. After receiving the fused image (fourth image), the binarization module determines the adaptive binarization threshold t according to the fused image (fourth image). Then, the binarization module uses the determined adaptive binarization threshold t to perform binarization processing on the fused image (fourth image), thereby obtaining a binarized image (fifth image).
[0259] Among them, the determination of the adaptive binarization threshold t, the binarization module can divide the fused image (fourth image) into two pixel value ranges through the initial candidate binarization threshold, and then calculate the average value of each pixel value in the two pixel value ranges to obtain the corresponding two pixel means as the second pixel means. The binarization module takes the average value of the multiple second pixel means as the first pixel mean, thereby determining the adaptive binarization threshold through the error between the candidate binarization threshold and the first pixel mean. For specific implementation, please refer to the records of the above embodiment, the principle is the same, and the embodiments of this application will not be repeated here.
[0260] After that, after the binarization module obtains the adaptive binarization threshold t and the binarized image (fifth image), because the window size required for the morphological operation in the embodiment of the present application needs to be adaptively obtained according to the adaptive binarization threshold t and the binarized image (fifth image), the binarization module sends both the adaptive binarization threshold t and the binarized image (fifth image) obtained to the window calculation module, and the window calculation module calculates the adaptive window size.
[0261] At the same time, the binarization module sends the obtained binarized image (fifth image) to the image processing module, so that the image processing module can perform subsequent morphological operations on the binarized image (fifth image). The specific implementation of determining the adaptive window size can refer to the records of the above embodiment, the principle is the same, and the embodiment of the present application will not be repeated here.
[0262] Finally, since the processing of morphological operations requires a given window size, after the window calculation module calculates the adaptive window size, it feeds the calculated adaptive window size back to the image processing module.
[0263] After receiving the adaptive window size given by the window calculation module, the image processing module performs morphological operation on the binary image (fifth image) based on the adaptive window size to obtain the sixth image. That is, the sixth image is the binary image (fifth image) after the morphological operation. The morphological operation includes expansion, erosion, and opening and closing operations. In some embodiments, the image processing module can perform a horizontal expansion operation and a vertical erosion operation on the barcode image, that is, on the binary image (fifth image).
[0264] After the image processing module completes the processing of the morphological operation, edge detection is performed on the sixth image to locate the barcode area, and the image after the barcode area is located, that is, the seventh image, is sent to the decoding tool.
[0265] Therefore, the decoding tool can perform barcode decoding on the seventh image on which the barcode area positioning and barcode enhancement have been completed to obtain decoding information, thereby improving the speed and accuracy of barcode scanning and decoding by the decoding tool.
[0266] Referring to Table 1 below, the embodiment of the present application takes the mobile phone as an electronic product and the decoding tool Zxing as an example, and tests the barcode recognition success rate for the outer packaging boxes corresponding to different models using the barcode recognition method provided by the embodiment of the present application and the traditional method of directly using Zxing for barcode recognition.
[0267] Table 1
[0268]
[0269] Among them, "barcode adaptive enhancement + Zxing" in Table 1 above is the barcode recognition method provided by the embodiment of the present application. It can be seen from the data in Table 1 above that the barcode recognition method provided by the embodiment of the present application has a much higher recognition success rate than the traditional recognition method, whether it is a mixture of outer packaging boxes of different models or an outer packaging box of a single model.
[0270] Another embodiment of the present application provides an electronic device, including: a camera, one or more processors and a memory. The camera and the memory are respectively coupled to the processor; the camera is used to collect images; the memory stores one or more computer program codes, and the computer program codes include computer instructions; when the processor executes the computer instructions, the electronic device implements the barcode recognition method described in any of the above embodiments.
[0271] Another embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor in an electronic device, the electronic device implements the barcode recognition method described in any of the above embodiments.
[0272] The embodiment of the present application also provides a computer program product. When the computer program product is run on a computer, the computer is enabled to execute each function or step in the above method embodiment.
[0273] The present application also provides a chip system, such as Fig.13 As shown, the chip system 130 includes at least one processor 1301 and at least one interface circuit 1302. The processor 1301 and the interface circuit 1302 can be interconnected through a line. For example, the interface circuit 1302 can be used to receive signals from other devices (such as a computer memory). For another example, the interface circuit 1302 can be used to send signals to other devices (such as the processor 1301).
[0274] Exemplarily, the interface circuit 1302 can read the instruction stored in the memory and send the instruction to the processor 1301. When the instruction is executed by the processor 1301, the computer can execute the various steps in the above embodiment. Of course, the chip system can also include other discrete devices, which is not specifically limited in the embodiment of the present application.
[0275] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0276] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0277] The units described as separate components may or may not be physically separated, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0278] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0279] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to execute all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.
[0280] The above contents are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.
Claims
1. A barcode recognition method, It is characterized in that Applied to an electronic device, the electronic device includes a camera, and the method includes: In response to a barcode scanning operation by a user, a first image is captured; the first image includes one or more barcodes, and the first image is captured by the camera; Based on the first image, a second image and a third image are obtained; wherein the second image is a grayscale image corresponding to the first image, and the third image is obtained by inverting the second image; performing image fusion on a first gradient image corresponding to the second image and a second gradient image corresponding to the third image to obtain a fourth image; Binarization is performed on the fourth image according to an adaptive binarization threshold to obtain a fifth image; wherein the adaptive binarization threshold is determined based on a first pixel mean; the first pixel mean is an average of a plurality of second pixel means; the second pixel mean is an average of pixel values in different pixel ranges in the fourth image, and the second pixel mean corresponds to the pixel ranges one by one; Performing barcode positioning and decoding based on the fifth image to obtain decoding information; A first interface is displayed, wherein the first interface includes the decoding information, and the decoding information corresponds to the barcode.
2. The method according to claim 1, It is characterized in that The step of fusing the first gradient image corresponding to the second image and the second gradient image corresponding to the third image to obtain a fourth image includes: Calculating the approximate gradient of the second image using a discrete differential operator to obtain a first gradient image, and calculating the approximate gradient of the third image using a discrete differential operator to obtain a second gradient image; A fourth image is obtained by performing weighted summation on the first gradient image and the second gradient image.
3. The method according to claim 2, It is characterized in that A first weight corresponding to the first gradient image is smaller than a second weight corresponding to the second gradient image.
4. The method according to claim 1, It is characterized in that The step of binarizing the fourth image according to the adaptive binarization threshold to obtain the fifth image includes: Using the candidate binarization threshold as a division basis, dividing the pixel values in the fourth image to obtain a first pixel value range and a second pixel value range; respectively calculating the mean of the first pixel value range and the mean of the second pixel value range to obtain a plurality of second pixel mean values; Calculating an average of the plurality of second pixel means to obtain the first pixel mean; When the error between the candidate binarization threshold and the first pixel mean is less than an error threshold, the candidate binarization threshold is used as an adaptive binarization threshold to binarize the fourth image to obtain a fifth image.
5. The method according to claim 4, It is characterized in that The method further comprises: When the error between the candidate binarization threshold and the first pixel mean is greater than the error threshold, the first pixel mean is used as a new candidate binarization threshold, and the adaptive binarization threshold is re-determined according to the new candidate binarization threshold.
6. The method according to any one of claims 1 to 5, It is characterized in that The performing barcode positioning and decoding based on the fifth image to obtain decoding information includes: Determine the adaptive window size; Based on the adaptive window size, a morphological operation is performed on the fifth image to obtain a sixth image; wherein the adaptive window size is determined according to the proportion of the barcode in the image; The barcode is located and decoded on the sixth image to obtain decoding information.
7. The method according to claim 6, It is characterized in that The step of determining the adaptive window size comprises: Dividing the fifth image into n image blocks according to pixel rows; wherein n≥2, n is an integer; and different image blocks include an equal number of pixel columns; For each of the image blocks, pixel values in each pixel column in the image block are added respectively to obtain a one-dimensional array corresponding to the image block; wherein the values in the one-dimensional array correspond to the pixel columns one by one; Using the adaptive binarization threshold, binarize the one-dimensional array to obtain a one-dimensional binary array; In each of the one-dimensional binary arrays, determine the continuous length of values that are identical and continuous, and obtain a reference shortest continuous length corresponding to each of the image blocks; The minimum value of the reference shortest continuous lengths corresponding to the n image blocks is used as the target shortest continuous length, or the product of the minimum value and a preset coefficient is used as the target shortest continuous length, and the target shortest continuous length is used as the adaptive window size.
8. The method according to claim 6, It is characterized in that The step of performing a morphological operation on the fifth image based on the adaptive window size to obtain a sixth image comprises: Based on the adaptive window size, a morphological operation is performed on the fifth image to obtain the sixth image; wherein the morphological operation includes a transverse expansion operation and a longitudinal erosion operation.
9. The method according to claim 6, It is characterized in that The performing barcode positioning and decoding on the sixth image to obtain decoding information includes: Performing edge detection on the sixth image to locate the barcode area to obtain a seventh image; The barcode decoding tool is used to perform barcode decoding on the seventh image to obtain decoding information.
10. An electronic device, It is characterized in that include: A camera, one or more processors and a memory, wherein the camera and the memory are respectively coupled to the processor; The camera is used to collect images; One or more computer program codes are stored in the memory, and the computer program codes include computer instructions; when the processor executes the computer instructions, the electronic device executes the barcode recognition method as described in any one of claims 1 to 9.
11. A computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor of an electronic device, the electronic device executes the barcode recognition method according to any one of claims 1 to 9.
12. A computer program product comprising a computer program, It is characterized in that When the computer program is executed by a processor in an electronic device, the electronic device executes the barcode recognition method according to any one of claims 1 to 9.
