Assignment 2 – Colour 2D barcodes
2D barcodes are becoming very popular. A well-known one is the QR code, which is common in
mobile apps. There is a new type of barcode that uses colours to increase the depth of the encoding.
Your task is to write a program in C or C++ using OpenCV that reads the 2D barcodes below. The 2D
barcodes may be rotated, scaled and slightly distorted.
This new 2D barcode uses a simple encoding with 8 colours. The colours are black, blue, green, cyan,
red, magenta, yellow, and white. These colours can represent three bit words. Notice that there is a
relationship with the RGB colour space (e.g., black is 0 (0,0,0), blue is 1 (0,0,255), and green is 2
There are 64 characters that can be encoded: the digits 0~9, the lower case a~z, the upper case A~Z, a
full stop ‘.’ and space. The encoding table can be found on Stream. The encoding of text is done
through the 2D bar using the order Top-left towards Right-bottom (just like scanning an image). Every
two squares of colours represents one of the 64 characters (3bits + 3bits). The maximum number of
characters in one barcode is 1050. The last square (bottom right) is left unused.
An example of encoding: char ‘a’ is 1. Therefore, its bit representation is 000001. Dividing this 6 bit
group into two groups, we have 000 and 001. The first square should be black (0,0,0) for R=0, G=0
and B=0 and the second square should be blue (0,0,1) for R=0, G=0 and B=255. Each square block of
colour represents 3 bits, and a pair of squares represent a single character.
An example of decoding: two squares red and yellow. Red is represented by (255,0,0) and yellow is
(255,255,0) in RGB. So red is 100 and yellow is 110. Putting the two together, 100110 (38 in decimal)
is ‘L’ in the table. Therefore, red/yellow is decoded as ‘L’.
Figure 1. a) an empty 2D bar showing the concentric circles. b) an example of encoding all possible characters
in order (see table 1 on the next page).
In figure 1 two examples of the 2D bar are shown. The concentric circles can be used for aligning the
2D bar when the image is rotated. The centres of the concentric circles can also be used to locate the
squares properly when the image is scaled and rotated. The lines between the squares can also be used
to rotate the 2D bar to an horizontal position. Both the lines and the concentric circles can be used
concurrently to get a better accuracy, so the centre of each squares where the colours will be
determined can be scanned properly.
1) Initially, work with an upright static version of the barcodes. It is easier to debug decoding
2) Use a combination of Hough transforms for straight lines and circles to re-orient the image in case
the image is rotated or scaled.
3) Once you have the image properly positioned, start reading the colours from upper-left to down-
right, reading two squares at a time. For example, A is encoded as 27 (011011) and G as 33 (100001).
Therefore A is encoded with 011 (cyan) 011 (cyan) and G is encoded as 100 (red) 001 (blue).
4) Figure 1-b shows the following pattern: <space>abcd…zABCD…Z12..9<stop>. This is the same
order in which the characters are encoded (‘space’ is 000000, ‘.’ is 111111, ‘a’ is 000001 etc).
5) Remember that the last square (bottom-right of the 2D bar) does not encode anything, as it cannot
pair up with any other square.
6) The encoding/decoding table is: (use the array available on Stream)
7) the colours present in the barcode are:
black (0,0,0), red (255,0,0), green (0,255,0), blue (0,0,255), white (255,255,255), cyan (0,255,255),
magenta (255,0,255) and yellow (255,255,0). Notice that the eight colours give all the possible states
for a 3 bit word.
8) There are many ways to re-orient, scale and align the image, so each colour can be extracted. For
example, one could start with the Hough for lines and just rotate the image, so the lines are
horizontal/vertical. However, this could leave the image rotated to 90 degrees, or even upside-down.
One could then use the concentric circles to rotate the image again, this time by a multiple of 90
degrees. The image could be out of centre and in an arbitrary scale, so limits of the 2D bar can be used
to determine a standard scale and centralise the image. The concentric circles can also be used for the