Disclosure of Invention
The technical problem of the invention is to provide a decoding method of a stained one-dimensional bar code, which can correctly identify the stained one-dimensional bar code to a certain extent.
In order to achieve the above purpose, the technical scheme adopted by the invention is as follows:
the decoding method of the stained one-dimensional bar code is characterized by comprising the following steps of: s1, acquiring a bar code image, and preprocessing the bar code image; s2, screening and positioning bar code areas of the bar code images; s3, scanning bar space information along the bar code direction by using a scanning line to obtain the width of each bar space of the bar code; s4, decoding according to the bar space width by utilizing a width measurement method and comparing a bar code character set coding table, and ending the flow if the decoding is successful; if the decoding fails, S5 is executed; s5, scanning the bar code area line by line, and calculating the maximum confidence coefficient of all the code words to obtain all the code word values, wherein the code word values comprise: and decoding the codeword true value and the codeword missing value according to the number of the codeword missing values.
S5 comprises the following steps: s51, searching out a start symbol, a stop symbol and a middle separator according to the space width, and determining the space data of the code word area; s52, forward and reverse decoding is carried out on the code words in the code word area row by row to obtain a code word data matrix; s53, calculating the confidence coefficient of each column of code word in the code word data matrix, obtaining the true value and the missing value of the code word, and decoding according to the number of the missing values of the code word.
S52 includes: s521, searching a character set coding table to perform forward decoding and reverse decoding on the strip-space data, and recording the numerical value of the codeword corresponding to the strip-space data error as Null; s532 repeatedly performs S52, scans line by line until the entire barcode region is traversed, generating a left codeword data matrix and a right codeword data matrix.
S53 includes: s531 counts the frequency of the occurrence of the numerical value of the code word represented by each column in the left code word data matrix and the right code word data matrix respectively, calculates the confidence coefficient of each numerical value, obtains the confidence coefficient maximum value of the code word represented by each column, judges whether the column confidence coefficient maximum value is larger than a threshold value, if so, judges that the numerical value corresponding to the confidence coefficient maximum value is the true value of the code word of the column, and continues to calculate the next column; otherwise, marking the code word represented by the column as a code word missing value, marking as Null, and continuing to calculate the next column until the last column of data is calculated, so as to obtain all code word values of the bar code; s532, counting the number of the codeword missing values obtained in S531, and if the number of the codeword missing values is greater than 1, failing decoding; if the number of the codeword missing values is equal to 1, calculating the codeword missing values according to the obtained codeword true values, and decoding successfully; if the number of the codeword missing values is equal to 0, calculating a correct check code according to all codeword values, and if the correct check code is matched with the codeword value of the check bit obtained in the step S531, decoding is successful; otherwise, executing S533; s533 compares the maximum confidence of all codewords obtained in S531, selects the minimum value, marks the codeword corresponding to the minimum value as a codeword missing value, and marks the codeword missing value as Null; and calculating a codeword missing value according to the obtained codeword true value, and decoding successfully.
In S532, if the number of codeword missing values is equal to 1 and the codeword missing values are check codes, the method for calculating codeword missing values according to the obtained codeword true values is as follows: s01, judging the parity of each codeword value at the left side of the bar code, and calculating the numerical value of the front bit of the bar code according to a parity coding table; s02, defining rightmost codeword bits as odd bits, defining rightmost codeword bits as even bits and sequentially and alternately defining parity of each codeword including the prefix bits from right to left; s03, summing the codeword values on the odd digits and multiplying the codeword values by 3 to obtain A, summing the codeword values on the even digits to obtain B, and calculating to obtain a result of A+B; s04, dividing the result of A+B in S03 by 10, if the result can be divided completely, the check code is 0, otherwise, the remainder is x, and the calculated value of (10-x) is the calculated check code, so that the decoding is successful.
In S532, if the number of codeword missing values is equal to 1 and the codeword missing values are not check codes, the method for calculating codeword missing values according to the obtained codeword true values is as follows: and (3) setting the initial value of the codeword missing value as 0, repeating the steps S01 to S04, comparing the obtained check code with the codeword value of the check bit obtained in the step S531, and if the result is inconsistent, sequentially adjusting the numerical value of the codeword missing value from 0 to 9 to calculate until the obtained check code is consistent with the codeword value comparison result of the check bit obtained in the step S531, and decoding is successful.
Detailed Description
The invention will now be further described with reference to the accompanying drawings and specific examples, which are not intended to limit the invention.
A complete one-dimensional bar code is composed of two side static areas, a starter, a data symbol, a verifier and a terminator, and the structure diagram is shown in figure 2. A plurality of black bars and blanks with different widths are arranged according to a certain coding rule to express a group of information.
Common barcode scanning devices are typically composed of components such as a light source (e.g., laser), an optical lens, a photosensitive device (e.g., CCD, CMOS, etc.), decoding logic, and an I/O interface. In brief, the light source projects light onto the bar code, the light reflected from the bar code enters the surface of the photosensitive device through the optical lens, a series of treatments such as photoelectric conversion and analog-to-digital conversion are carried out to form a digital image of the bar code, then the decoding logic circuit analyzes and decodes the captured image, in the prior art, the method for decoding the one-dimensional bar code by acquiring the width of the bar space through the scanning line has larger requirements on the definition degree of the bar code, and the decoding success rate of the method is lower under the condition that the bar code is seriously stained.
The invention provides a decoding method of a stained one-dimensional bar code, which comprises the steps of firstly, collecting bar code images, preprocessing the bar code images, including denoising, binarization and the like; the bar code region screening and positioning are carried out on the bar code image after pretreatment; and scanning the successfully positioned bar code area line by line from top to bottom according to the bar code direction to obtain the bar space information of each line. As can be seen from the EAN13 coding rule, each codeword consists of two black bars and two white spaces, as shown in FIG. 3, C1 、C2 、C3 、C4 The width of the corresponding bar or void, respectively. After the decoding scanning line sweeps a row, acquiring the space-stripe width of the whole row, and normalizing the space-stripe width value to obtain space-stripe ratio data of each character of the row; according to the obtained space widthThe degree ratio information decodes the codeword. Referring to EAN13 bar code standard, searching character set coding table, each codeword value with numbers 0-9 has its corresponding bar space width ratio KLA (left-side odd code), KLB (left-side even code), KR And (right side code), comparing the obtained space-space width ratio with data in a coding table, determining a codeword value according to the similarity, storing codeword character data, and verifying whether a decoding result is correct by using a check bit codeword value.
If the decoding fails in the above process, as shown in fig. 1, scanning the bar code area line by line, searching the initiator, terminator and middle separator according to the bar space width, and determining the bar space data of the code word area; let the left codeword area stripe width (between the initiator and the middle separator) of the row be { C1 ,C2 ,...,Cn Sequentially forward and backward decoding by searching a character set coding table, and respectively recording codeword data D obtained by decoding twice in different directionsforward ={Df1 ,Df2 ,...,Df6 Sum Dinverse ={Di1 ,Di2 ,...,Di6 }. If the corresponding stripe data errors in the k code word are solved and the stripe data errors can not be successfully solved, the current code word value D is recordedk =null, and then continue to solve the subsequent code words, and ensure that the left side and the right side of the bar code are both 6 code words according to the coding rule. Repeating the steps, traversing and scanning the whole bar code area image, counting the code word value of each row, and setting the left code word data matrix as XL The number of scans was n, at which time it was possible to obtain:
wherein each data is an integer codeword value or Null with a value of 0-9, the number of rows of the set X is 2n, and the number of columns is 6. The right codeword data matrix X can be obtained by the same wayR . Then start counting XL And XR Data of each column, XL Data { D of column 11f1 ,D1i1 ,...,Dni1 All measured values representing the first code word of the bar code except the preamble code, counting each row of data from column 1The frequency of occurrence of each value in (a) is used to obtain the frequency f of occurrence of 0,1, 9 in the values of the code words in the column respectively0 ,f1 ,...,f9 (e.g., the number of codewords in the column is 1, k times, f1 =k), and the number of codewords is the frequency f of occurrences of NullNull . From this, the confidence Conf of each possible value i of the code word character of the bar code can be obtainedi The calculation formula is as follows:
calculate the column confidence Confi And (3) the maximum value, the corresponding i value of which is the most frequently occurring in the list, is considered to be the codeword true value of the codeword in the row if the maximum confidence is greater than a threshold value T (taking T=0.3), and the codeword character corresponding to the row is successfully decoded. If the maximum confidence of the column is not greater than the threshold T, the decoding of the current codeword character fails, the current codeword data is marked as a codeword missing value, and the codeword missing value is marked as Null to continue to calculate the next column.
For XL And XR After all 12 codeword values of the bar code are calculated, counting the number of codeword missing values in the 12 codeword values, setting the number of codeword missing values as N, if N>And 1, indicating that the bar code information is seriously lost, and failing to decode and exiting. If n=0, the barcode decoding correctness is verified. According to the bar code coding rule, the 11-bit data symbol code word can calculate the correct check code, compare the code word values of the check bits obtained in the decoding process, if the comparison values are consistent, the decoding is successful, and the correct decoding result is output. When the check values are inconsistent, comparing the maximum confidence of each codeword in the decoding result, selecting the minimum value, marking the corresponding codeword as a codeword missing value, marking as Null, calculating the codeword missing value according to the obtained codeword true value, and decoding successfully.
When n=1, the remaining 11-bit codeword values are considered to be true and reliable, and according to the coding rule, the true value of the codeword missing value can be calculated through the 11-bit codeword values, the calculation method refers to the commodity barcode national standard GB12904-2003, and when the codeword missing value is a check code, the specific calculation method is as follows:
s01, judging the parity of each codeword value at the left side of the bar code, and calculating the numerical value of the front bit of the bar code according to a parity coding table;
s02, defining rightmost codeword bits as odd bits, defining rightmost codeword bits as even bits and sequentially and alternately defining parity of each codeword including the prefix bits from right to left;
s03, summing the codeword values on the odd digits and multiplying the codeword values by 3 to obtain A, summing the codeword values on the even digits to obtain B, and calculating to obtain a result of A+B;
s04, dividing the result of A+B in S03 by 10, if the result can be divided completely, the check code is 0, otherwise, the remainder is x, and the calculated value of (10-x) is the calculated check code, so that the decoding is successful.
If the number of the codeword missing values is n=1 and the codeword missing values are not check codes, the method for calculating the codeword missing values according to the obtained codeword true values is as follows: and (3) setting the initial value of the codeword missing value as 0, repeating the steps S01 to S04, comparing the obtained check code with the codeword value of the check bit obtained in the step S531, and if the result is inconsistent, sequentially adjusting the numerical value of the codeword missing value from 0 to 9 to calculate until the obtained check code is consistent with the codeword value comparison result of the check bit obtained in the step S531, and decoding is successful.
In summary, the method for counting the codeword value with the maximum confidence and the method for calculating the codeword value by using the multi-row scanning information effectively improve the decoding success rate of the dirty code, and the algorithm process is not complex, has smaller calculated amount and is suitable for practical application scenes.
The foregoing describes preferred embodiments of the present invention; it is to be understood that the invention is not limited to the specific embodiments described above, wherein devices and structures not described in detail are to be understood as being implemented in a manner common in the art; any person skilled in the art will make many possible variations and modifications, or adaptations to equivalent embodiments without departing from the technical solution of the present invention, which do not affect the essential content of the present invention; therefore, any simple modification, equivalent variation and modification of the above embodiments according to the technical substance of the present invention still fall within the scope of the technical solution of the present invention.