Barcode Recognition and Conversion in Matlab

0
3229
matlab logo

The first step in Recognition and Conversion of barcode, can be easily achieved by several edge detection techniques. We have employed the fact that the barcode is an intensity image. It contains vertical strips of black and white lines. So we use the gray thresh value of the image to detect the start and the end of the barcode strips. This can be achieved by the wavelet transform method [3] or by morphological operations [10] but we want a method that is both quick and efficient so we use the scanline based approach. This method uses the fact that a horizontal scanline is at the center of the barcode. If the image of the barcode is not centered or the center region contains distortions then the algorithm will simply shift the scanline above or below the center of the image until we get a non distorted region.

1. Binarization

The next step is the Binarization of the image. We have to distinguish the barcode i.e., black strips from the white background i.e., spaces. First the start and the end of the barcode is detected i.e., first black strip and last black strip. Then scanline scans the pixels from the center. Then the gray thresh values are used to assign 1s to spaces and 0 s to strips. If the gray thresh is other than that of black or white. We have defined two ranges for gray thresh values, one range is allotted 1 for white and another is assigned a binary 0 for black. If the gray thresh value of a pixel lies within the white range it is assigned binary 1 i.e., we say that this pixel is white or space but has been discolored, if it lies in other range then it is assigned the opposite binary number i.e., 0 we say that the pixel was black but has been decolored. In this way the decolored pixels that may arise in the barcode image are accounted for and thus a type of noise is taken care of. This method is derived from the luminous method described in [1]. In this method without searching for the edges the scanline binarizes all the pixels. The pixel selected first is the middle one. To cater for the noises first the scanline pixels are smoothed and then the luminance value Y(x) Ԑ [0, 1] is calculated for each pixel, whose position is given by:

.

Then the local minima and maxima are computed

Fig. 2 Block Diagram of our Algorithm, Input is an image of barcode. Which is detected and binatized by scanline approach, then error check and decimal conversion are applied.

2. Error Checking

Once the image has been binarized then all we have to do is to use our knowledge about the construction of the barcode, this in our case is achieved by the knowledge of the UPC’s encoding process. In nearly all the Barcode standards we have a total of 13 decimal digits. The last is the parity bit. This digit is used to provide the second step of the double error check.

A.     The UPC standard [12]

An UPC-A barcode consists of four areas:

  1. The number system
  2. The manufacturer code
  3. The product code
  4. The check digit

Number Systemis for the class or type to which the product belongs.

The manufacturer codeis given to each manufacturer by UCC council for companies.

The product codeis a 5-digit number that the manufacturer gives to his products.

The check digit verifies the validity of the product. It tells whether the barcode has been scanned properly of not.

The barcode begins & ends with a ‘101’ or “black-white-black” pattern, these are also termed as guard bars. Between these guard bars, we have two blocks of black and white strips, separated by a central bar which is a ‘01010’or“white-black-white-black-white” pattern. The two blocks on either side of the central bar contain the decimal code, which is different for each product. Each decimal digit is encoded by using 7 bits. Even and Odd alphabets are used for the encoding. First digit is called the meta-number or induced digit[1].

To ensure that the scanline does not go outside the boundary of the image, we set our starting position in the middle of the image, to the value where luminance is 0 or we start with a bar (black). Then we scan left and right from this point by using pre-determined steps or blocks of pixels. The choice of the steps helps us in finding the guard bars, which are used for the first step of the error detection.

The error detection is as follows:

If

  • The first 3 bits of the binarized image are equal to 101.
  • The bit 46 is 0, 47 is 1, 48 is 0, 49 is 1 and 50 is 0.
  • The last three bits i.e., 93, 94 and 95 are equal to 101.

Then

The binarized image is a valid UPC code. If any one of the above mentioned conditions is not satisfied then the code is not a valid UPC code.

3.  Conversion

For the conversion of the binarized image to the decimal we again use our knowledge of the UPC construction. There is a lookup table [14] with which we compare our bits.

We ignore first and last 3 bits as they are the guard bits. Also we do not consider the central bits. As these bits are same for all the barcodes. Starting from the 4th bit (first three bits are guard bits) and selecting seven bits at a time. These seven bits are compared with the lookup table of Figure 2 and by comparison we allocate decimal values to the block of seven bits.  We can see from the table that if we are looking at bits to the left of the central bits then we use the “Left Bit Pattern”column to compare, while if we are looking at bits on the right of the central bits we use the “Right Bit Pattern”.

Digit

Left Bit Pattern Right Bit Pattern
0 0001101 1110010
1 0011001 1100110
2 0010011 1101100
3 0111101 1000010
4 0100011 1011100
5 0110001 1001110
6 0101111 1010000
7 0111011 1000100
8 0110111 1001000
9 0001011 1110100

Once the 12 digits have been calculated the check sum bit is calculated, it is calculated by the following method [14]

  1. The digits that are in the odd numbered positions are added and multiplied by 3.
  2. The digits in the even numbered positions are added.
  3. The sum of even numbered is added to the sum of odd numbered.
  4. The result is modulo 10.
  5. If the result is zero then check bit is zero. Otherwise subtract it from 10 and the answer is the result.
[code]clc
all clear;
input = imread('4.jpg');
imshow(input);
g=graythresh(input);
r=max(max(input))-min(min(input));
t=round(g*r);
[r c]=size(input);
for i=1:r
for j=1:c
if input(i,j)<t
input(i,j)=0;
end
if input(i,j)>t
input(i,j)=255;
end
end
end

%---------CROP Scanline--------------
sizeimg = size(input);
for i=1:sizeimg(2)
binimg1(i)=input(round(sizeimg(1)/2), i);
end
% figure
% subplot(1,2,1)
% imshow(binimg1)
inv=not(binimg1);
% subplot(1,2,2)
% imshow(inv)
ind1 = find(inv, 1, 'first');
ind2 = find(inv, 1, 'last');
new_binimg=imcrop(binimg1,[ind1 1 ind2-ind1 0]);
scanline=not(new_binimg);
for i=0:numel(scanline)
if scanline(i+1)==0
break;
end
end
h=scanline(1:i:end);
%----------------binary to decimal conversion---------
if (h(1)==1 && h(2)==0 && h(3)==1 && h(93)==1
&& h(94)==0 && h(95)==1 && h(46)==0 && h(47)==1
&& h(48)==0 && h(49)==1 && h(50)==0)
check1=1;
else
check1=0;
end

if(check1==1)
bit1=h(4:92);
cflag=1;
else
bit1=0;
cflag=0;
end

check=bit1;

if cflag==1
for i=1:6
if bit1(1+7*(i-1):7+7*(i-1)) == [0 0 0 1 1 0 1];
dec(i)=0;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 0 1 1 0 0 1];
dec(i)=1;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 0 1 0 0 1 1];
dec(i)=2;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 1 1 0 1];
dec(i)=3;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 0 0 0 1 1];
dec(i)=4;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 0 0 0 1];
dec(i)=5;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 0 1 1 1 1];
dec(i)=6;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 1 0 1 1];
dec(i)=7;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 1 1 0 1 1 1];
dec(i)=8;
elseif bit1(1+7*(i-1):7+7*(i-1)) == [0 0 0 1 0 1 1];
dec(i)=9;
end
end

for i=7:12
if bit1(48+7*(i-7):54+7*(i-7)) == [1 1 1 0 0 1 0];
dec(i)=0;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 1 0 0 1 1 0];
dec(i)=1;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 1 0 1 1 0 0];
dec(i)=2;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 0 0 1 0];
dec(i)=3;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 1 1 1 0 0];
dec(i)=4;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 1 1 1 0];
dec(i)=5;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 1 0 0 0 0];
dec(i)=6;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 0 1 0 0];
dec(i)=7;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 0 0 1 0 0 0];
dec(i)=8;
elseif bit1(48+7*(i-7):54+7*(i-7)) == [1 1 1 0 1 0 0];
dec(i)=9;
end
end
ODD = 3*(dec(1) + dec(3) + dec(5) + dec(7) + dec(9) + dec(11));
EVEN = dec(2) + dec(4) + dec(6) + dec(8) + dec(10);
SUM = ODD + EVEN
check = 0;
i = 0;
while (check == 0)
i=i+1;
if((i*10) > SUM)
CS = (i*10) - SUM
if ( CS == dec(12))
check = 1;
end
break
end
end

if check==1
barcode=dec;
else barcode=0;
end
if barcode==0
barcode='Error in Barcode';
end
else
barcode='Error in bar';
end
barcode
[/code]

LEAVE A REPLY

Please enter your comment!
Please enter your name here