Professional Documents
Culture Documents
Intro to Robots
Processing Pictures:
Intro to Robots
Picture Functions:
newPic = pictureCopy(picture)
Intro to Robots
Robot Vision:
Intro to Robots
Making your own Picture:
newPic = makePicture(100,100)
show(newPic)
black by default
onePixel = getPixel(newPic,10,5)
print getRed(onePixel) #initial red value
setRed(onePixel,255)
.
print getRed(onePixel) #new red value
show(newPic)
Intro to Robots
Making your own Picture: height of picture
drawVerticalRedLine(newPic,10)
show(newPic)
Intro to Robots
Real Photograph
applePic = takePicture()
OR
applePic = loadPicture(apple.jpg)
drawVerticalRedLine(applePic,10)
show(applePic)
Intro to Robots
Scan Real Photograph
Intro to Robots
Robot/Program Vision:
The robot and our program dont see purple, they each
see a combination of red, green and blue.
r,g,b = getColors(pixel)
wait(5)
myCanvas.close()
Intro to Robots
Filtering Photographs:
Intro to Robots
Brightness Filter:
Intro to Robots
Redness Filter:
Intro to Robots
Redness Filter:
myPicture = takePicture()
show(myPicture)
def filterRed(picture):
for pixel in getPixels(picture):
rValue = getRed(pixel)
if rValue > 175:
#Turn the pixel white
setRed(pixel,255)
setGreen(pixel,255)
setBlue(pixel,255)
else:
#Otherwise, turn it black.
setRed(pixel,0)
setGreen(pixel,0)
setBlue(pixel,0)
filterRed(myPicture)
show(myPicture)
an awful lot of red
Intro to Robots
Greenness Filter:
myPicture = takePicture()
show(myPicture)
for pixel in getPixels(myPicture):
gValue = getGreen(pixel)
if gValue > 175:
#Turn the pixel white
setRed(pixel,255)
setGreen(pixel,255)
setBlue(pixel,255)
else:
#Otherwise, turn it black.
setRed(pixel,0)
setGreen(pixel,0)
setBlue(pixel,0)
show(myPicture)
Intro to Robots
Primarily Red Filter:
myPicture = takePicture()
show(myPicture)
Def findRedAreas(picture):
for pixel in getPixels(myPicture):
rValue = getRed(pixel)
gValue = getGreen(pixel)
bValue = getBlue(pixel)
if rValue > 175 and
gValue < 175 and bValue < 175:
#Turn the pixel white
setRed(pixel,255)
setGreen(pixel,255)
setBlue(pixel,255)
else:
#Otherwise, turn it black.
setRed(pixel,0)
setGreen(pixel,0)
setBlue(pixel,0)
more like it; just two red
show(myPicture) hot spots
Intro to Robots
Find the Center of Redness:
average x-coordinate
Intro to Robots
Center of Redness of our Photography
def AverageXofWhitePixels(picture):
sumX = 0.0 # We use floating point values.
counter = 0.0 # We use floating point values.
for xPos in range(0, getWidth(picture) ):
for yPos in range(0, getHeight(picture) ): Vertical line to one side because
pixel = getPixel(picture,xPos,yPos) redness of the apple shadow is
value = getGreen(pixel) pulling the red center to the right.
if value > 0 :
# add x-coord of every white pixel
sumX = sumX + xPos
counter = counter + 1
averageX = sumX / counter
return int(averageX) #Return an Integer
myPicture = takePicture()
picture = copyPicture(myPicture)
picture = findRedAreas(picture)
XposAvg = AverageXofWhitePixels(picture)
drawVerticalRedLine(myPicture,XposAvg)
show(myPicture)
Intro to Robots
Processing Red Areas Individually:
Intro to Robots
Red Blobs one-at-a-time:
Intro to Robots
BlobList Data Structure:
Intro to Robots
Complicated Blobs
Intro to Robots
total blob
How to Build a Blob: in picture
pixels added
to program
blob
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . 2.nd . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
1st
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
Which is the last pixel to be added to the program blob?
Intro to Robots
Blob Strategy
Intro to Robots
Blob Code: .....
.....
from myro import * .....
class Blob:
.....
xCoordinates 12 13 14
def incPixelCount(self,xCoordinate,cnt):
self.blobPixelSum = self.blobPixelSum + cnt
if self.verticalCounts.has_key(xCoordinate): ### xCoord already belongs to blob
self.verticalCounts[xCoordinate] = ### handles the donut case
self.verticalCounts[xCoordinate] + cnt
else:
self.verticalCounts[xCoordinate] = cnt
def inBlob(self,c):
return c in self.colors ### tests if a color is already in the blob; if the same color
### is found in two blobs, they are the same blob
Intro to Robots
Blob Code:
def mergeDictionaries(self,b):
newD = { }
d1 = self.verticalCounts ### {x1:c1, x2:c2, , xn:cn}
d2 = b.verticalCounts
for d in d1.keys():
newD[d] = d1[d]
if d2.has_key(d):
newD[d] = newD[d] + d2[d] ### add vertical counts if xCoord in both blobs
del d2[d]
for d in d2.keys(): ### get the remaining xCoords not gotten in first loop
newD[d] = d2[d]
return newD
def combine(self,b):
newB = Blob()
newB.colors = self.colors + b.colors ### combine disjoint color lists
newB.blobPixelSum = self.blobPixelSum +
b.blobPixelSum
newB.verticalCounts = self.mergeDictionaries(b) ### combine vertical counts
return newB
Intro to Robots
Blob Code:
def calculateMidPoint(self):
sum = 0
for x in self.verticalCounts.keys(): ### same calculation as the single blob problem
sum = sum + x*self.verticalCounts[x]
self.midPoint = sum/self.blobPixelSum
def getMidPoint(self):
if self.midPoint == -1:
self.calculateMidPoint()
return self.midPoint
def getSize(self):
return self.blobPixelSum
def writeOut(self):
print self.colors
print "sum of pixels: ", self.blobPixelSum
self.calculateMidPoint()
print 'midPoint ' , self.midPoint
print 'color list', self.colors
Intro to Robots
Generate New Colors:
class AvailableColors:
generates blue-green colors
def __init__(self):
on the fly in the ranges:
self.g = 100; self.b = 0
100 <= green <= 255
0 <= blue <= 255
def getNext(self):
approximately 37500 different
g = self.g; b = self.b
colors
self.b = self.b + 1
if self.b > 255:
self.g = self.g+1
self.b = 0
return Color(0,g,b)
Intro to Robots
Alternate AvailableColors Implementation:
class AlternativeAvailableColors:
def __init__(self): creates a complete lost of colors
self.contents = [ ] up front and then returns these
self.index = 0 colors one at a time as requested
for i in range (100,256):
for j in range(0,256):
self.contents.append(Color(0,i,j))
def getNext(self):
c = self.contents[self.index]
index = index + 1
return c
Intro to Robots
Data/Code Comparisons:
Intro to Robots
Main Program:
leftPixel
rightPixel
rightPixel = ==
ac= getPixel(pictureCopy,x-1,yNdx)
getPixel(pictureCopy,x-1,yNdx)
getPixel(pictureCopy,x+1,yNdx)
getPixel(pictureCopy,x+1,yNdx)
AvailableColors()
clr
yNdx y+1= ac.getNext()
=yCount = yCount
ififwhile
bl getRed(leftPixel)
=getRed(rightPixel)
if getRed(rightPixel)
BlobList()
x =yNdx
getX(pix)
>= 0 and 0+
==== yNdx
==and
00and
and- y -1
picHeight
yNdx
yNdx
while =
==yNdx getHeight(pictureCopy)
yNdx
yNdx +<- 1picHeight
1
b.incPixelCount(x,yCount) and> >0:>0:0:
getGreen(leftPixel)
getGreen(leftPixel)
getGreen(rightPixel)
getGreen(rightPixel)
y<=picHeight:
getY(pix)
getRed(pixel) > maxRed: > 0:
if pixel
yNdx
pixel = =getPixel(pictureCopy,x,yNdx)
getPixel(pictureCopy,x,yNdx)
getRed(pixel)
bl.add(b) > maxRed:
for pixleftColor
leftColor
rightColor
rightColor
bin == getColor(leftPixel)
getColor(leftPixel)
==getColor(rightPixel)
= Blob(clr,x) getColor(rightPixel)
getPixels(pictureCopy):
setColor(pixel,clr)
pixel
if
ifsetColor(pixel,clr)
not=
not getPixel(pictureCopy,x,yNdx)
ifpixelb.inBlob(leftColor):
b.inBlob(leftColor):
notb.inBlob(rightColor):
b.inBlob(rightColor):
redValue ==pix
getRed(pix)
oldB
if redValueoldB=>=bl.findBlob(leftColor)
bl.findBlob(rightColor)
bl.findBlob(rightColor)
maxRed:
for each pixel
bb==b.combine(oldB)
b.combine(oldB)
if pixel is red:
bl.dropBlob(oldB)
bl.dropBlob(oldB)
create a new empty blob and new color
while pixel is red:
add pixel to current blob (change color)
if pixel to the left is already in another blob:
combine the two blobs
if pixel to the right is already in another blob:
combine the two blobs
get the pixel immediately above the previous pixel
select the pixel immediately below the original red pixel
while pixel is red:
add pixel to current blob (change color)
if pixel to the left is already in another blob:
combine the two blobs
if pixel to the right is already in another blob:
combine the two blobs
get the pixel immediately below the previous pixel
add blob to blob list
Intro to Robots