As a school project we are assigned to find and decode barcodes in an image. For this project I will be teaming with Julien Marquegnies. This is the first article of a (perhaps) long serie.
As a first draft, this morning I quickly developed a program to decode UPC barcodes. I used OpenCV for loading and displaying but I don't use any advanced function from the library so the code can be quickly adapted to other use cases.
Universal Product Code
UPC is a standardized type of barcode used for scanning goods in stores. Here is an example of a UPC barcode:
First of all, we need to explain how UPC works.
To the left and to the right of the barcode we have a large area of white called the quiet zone. This zone is required to be at least 10 times the width of the smallest bar and is here to facilitate barcode detection, it has no interest for decoding.
We can notice that the same pattern at the left, at the middle and at the right of our barcode:
This patterns is called the guard. The three occurences of this pattern are called (from left to right) S, M, and E (Start, Middle and End). S and E are encoded with 3 bits and the pattern is bar-space-bar. M is encoded with 5 bits with the pattern space-bar-space-bar-space.
Left and right digits
Between S and M we have the left digits and between M and E we have the right digits. Each digit is encoded on 7 bits, giving a total of 84 bits encoding digits, and 11 bits encoding guards.
Left and right digits are encoded differently: the right encoding scheme is the bitwise complement of the left encoding scheme.
The concept of bits for a visual pattern might be unclear, so let me explain: by reading the guard we have determined the width of the thinnest bar, in order to read a digit we read approximatively 7 * width pixels, at each time, if we encountered a bar the current bit will be 1 and 0 otherwise. The encoding of each digit is presented in the following table:
Let's zoom on the beginning of our example code, at the left we have our guard pattern and just after we have the first encoded digit.
After reading the guard, we have the width of a single bit, we can now read the next 7 bits and get their value.
We obtain here space-space-space-bar-bar-space-bar, that is to say we have 0001101, and according to our table it is the value 0.
Here are two more barcode images I used:
I tried with other images as well but you can generate your own barcodes online if you want.
Now read part 2 for the first part of the implementation.