Decoding Barcodes: Institute For Personal Robots in Education (IPRE)
Decoding Barcodes: Institute For Personal Robots in Education (IPRE)
CS 1 with Robots
Aug 29 2007
Aug 29 2007
Aug 29 2007
Aug 29 2007
49 bars total!
6
It's an I
Aug 29 2007
Aug 29 2007
10
Aug 29 2007
11
Aug 29 2007
12
Threshold Code
Aug 29 2007
13
Threshold Code
def threshold(pic):
for i in getPixels(pic):
g = getGreen(i)
if( g < 127):
setRed(i,0)
setGreen(i,0)
setBlue(i,0)
else:
setRed(i,255)
setGreen(i,255)
setBlue(i,255)
return(pic)
Aug 29 2007
14
15
Now what?
We have a thresholded image, now we have to scan
across it to look for bars.
Lets start out with a simpler task, just scan across it and
save a list of the pixel values (white=255 or black=0) in a
list.
But where do we scan?
Aug 29 2007
16
Now what?
We have a thresholded image, now we have to scan
across it to look for bars.
Lets start out with a simpler task, just scan across it and
save a list of the pixel values (white=255 or black=0) in a
list.
But where do we scan?
How about the middle?
How do you find the middle of the image?
Aug 29 2007
17
Our Image:
X=255
X=0
Width = 256
Y=0
Height =
192
Y=191
Aug 29 2007
18
X=255
X=0
Width = 256
Y=0
Height =
192
Middle =
Height / 2
Y=191
Aug 29 2007
19
def makeScanLine(bwPic):
return(values)
Aug 29 2007
20
def makeScanLine(bwPic):
height = getHeight(bwPic)
mid = height / 2
width = getWidth(bwPic)
values = []
for x in range(0, width):
pix = getPixel(bwPic, x, mid)
val = getGreen(pix)
values.append(val)
return(values)
Aug 29 2007
21
Note the large runs of white at the beginning and end of the barcode!
Aug 29 2007
22
Aug 29 2007
23
Aug 29 2007
24
Aug 29 2007
25
return(barData)
Aug 29 2007
26
previous = scanLine[0]
length = 0
for element in scanLine:
if (element != previous): #a change has occured!
myTuple = (length, previous)
barData.append( myTuple ) #add run info to barData list
length = 1
previous = element
else:
#No change.
length = length + 1
Aug 29 2007
27
28
[ (2, 0), (26, 255), (3, 0), (8, 255), (3, 0), (3, 255), (8, 0),
(4, 255), (8, 0), (4, 255), (2, 0), (4, 255), (8, 0), (4, 255),
(4, 0), (8, 255), (2, 0), (4, 255), (4, 0), (4, 255), (8, 0),
(2, 255), (1, 0), (1, 255), (6, 0), (4, 255), (4, 0), (8, 255),
(8, 0), (4, 255), (2, 0), (4, 255), (4, 0), (4, 255), (2, 0),
(8, 255), (4, 0), (4, 255), (8, 0), (4, 255), (7, 0), (3, 255),
(4, 0), (39, 255) ]
Can you spot the narrow and wide bars?
Aug 29 2007
29
[ (2, 0), (26, 255), (3, 0), (8, 255), (3, 0), (3, 255), (8, 0),
(4, 255), (8, 0), (4, 255), (2, 0), (4, 255), (8, 0), (4, 255),
(4, 0), (8, 255), (2, 0), (4, 255), (4, 0), (4, 255), (8, 0),
(2, 255), (1, 0), (1, 255), (6, 0), (4, 255), (4, 0), (8, 255),
(8, 0), (4, 255), (2, 0), (4, 255), (4, 0), (4, 255), (2, 0),
(8, 255), (4, 0), (4, 255), (8, 0), (4, 255), (7, 0), (3, 255),
(4, 0), (39, 255) ]
Narrow bars look to be around 3-4 pixels in size, and
wide bars appear to be around 6-8 pixels in size!
Aug 29 2007
30
Aug 29 2007
31
Zoom in:
Aug 29 2007
32
Zoom in:
Aug 29 2007
33
Another problem!
Wait! What are those single pixel black and white bars
doing in the middle of our image?
Actual scanline data:
[ (2, 0), (26, 255), (3, 0), (8, 255), (3, 0), (3, 255), (8, 0),
(4, 255), (8, 0), (4, 255), (2, 0), (4, 255), (8, 0), (4, 255),
(4, 0), (8, 255), (2, 0), (4, 255), (4, 0), (4, 255), (8, 0),
(2, 255), (1, 0), (1, 255), (6, 0), (4, 255), (4, 0), (8, 255),
(8, 0), (4, 255), (2, 0), (4, 255), (4, 0), (4, 255), (2, 0),
(8, 255), (4, 0), (4, 255), (8, 0), (4, 255), (7, 0), (3, 255),
(4, 0), (39, 255) ]
Aug 29 2007
34
Another problem!
Wait! What are those single pixel black and white bars
doing in the middle of our image?
Actual scanline data:
[ (2, 0), (26, 255), (3, 0), (8, 255), (3, 0), (3, 255), (8, 0),
(4, 255), (8, 0), (4, 255), (2, 0), (4, 255), (8, 0), (4, 255),
(4, 0), (8, 255), (2, 0), (4, 255), (4, 0), (4, 255), (8, 0),
(2, 255), (1, 0), (1, 255), (6, 0), (4, 255), (4, 0), (8, 255),
(8, 0), (4, 255), (2, 0), (4, 255), (4, 0), (4, 255), (2, 0),
(8, 255), (4, 0), (4, 255), (8, 0), (4, 255), (7, 0), (3, 255),
(4, 0), (39, 255) ]
Zoom in:
Aug 29 2007
35
Another problem!
We need to remove those single pixel errors!
Zoom in:
Aug 29 2007
36
Aug 29 2007
37
def removeSingles(barData):
return(newBarData)
Aug 29 2007
38
def removeSingles(barData):
newBarData =[]
for item in barData:
length = item[0]
if (length != 1):
newBarData.append(item)
return(newBarData)
Aug 29 2007
39
Aug 29 2007
40
Aug 29 2007
41
42
43
44
Aug 29 2007
45
return( threshold )
Aug 29 2007
46
Aug 29 2007
47
Aug 29 2007
48
return(barString)
Aug 29 2007
49
50
bWbWbwBwBwbwBwbWbwbwBwBwbWBwbwbwbWbwB
wBwbW
Now we just have to parse this string to find our barcode!
All (valid) barcodes start with the pattern:
bWbwBwBwborthe*symbol
Lets go looking for it!
Aug 29 2007
51
bWbWbwBwBwbwBwbWbwbwBwBwbWBwbwbwb
WbwBwBwbW
Aug 29 2007
52
Aug 29 2007
53
Aug 29 2007
54
55
56
code39dict = {
'BwbWbwbwB': "1",
}
answer=code39dict[BwbWbwbwB]
print answer
1
Aug 29 2007
57
Aug 29 2007
58
Aug 29 2007
59
Aug 29 2007
60
startLoc = startLoc + 10
#Initialize a variable to store our code
codeData=
Aug 29 2007
61
62
Questions?
Aug 29 2007
63