Drawing and Image Processing in Python With Myro Graphics: Dr. Paige H. Meeker
Drawing and Image Processing in Python With Myro Graphics: Dr. Paige H. Meeker
Processing in Python
with Myro Graphics
Dr. Paige H. Meeker
drawings!
http://wiki.roboteducation.org/Myro_Reference_Ma
nual
Changing Colors
Python uses the RGB
Changing Colors
To create a new color value, assign an
Drawing
To draw things, we need a window. We create
it with:
myWin = GraphWin(title, width, height)
(remember, myWin is a variable name it
can be anything you want it to be.)
After creating the window, you can do several
things to it:
myWin.setBackground(color)
myWin.close()
myWin.isClosed()
myWin.getMouse()
Point(x,y)
myPoint = Point(x,y)
Actions you can use on points include:
getX()
getY()
These actions (also called methods) will
Oval(point1,point2)
myOval = Oval(point1,point2)
Actions you can use on ovals include:
getCenter()
getP1()
getP2()
These actions (also called methods) will
Rectangle(point1,point2)
myRectangle = Rectangle(point1,point2)
Actions you can use on rectangles include:
getCenter()
getP1()
getP2()
These actions (also called methods) will
Line(point1,point2)
myLine = Line(point1,point2)
Actions you can use on lines include:
getCenter()
setArrow("first" / "last" / "both" / "none")
getP1()
getP2()
Circle(centerPoint,radius)
myCircle = Circle(centerPoint,radius)
Actions you can use on circles include:
getCenter()
getRadius()
Polygon(point1,
point2, ...)
myPoly = Polygon(p1,p2,p3,) #list of
points
Actions you can use on polygons include:
getPoints() #Returns a list of points.
Text(anchorPoint, string)
myText = Text(anchorPoint,string)
Actions you can use on text include:
setText(string)
getText()
getAnchor()
setFace(family)
setSize(point)
setStyle('normal'/'bold'/'italic')
setTextColor(color)
Image(centerPoint,
imageFileName)
#to read in a file (jpg, gif) and then
#convert it to an image:
dis = makePicture(pickAFile())
disPixmap = makePixmap(dis)
center = Point(150,150)
pic = Image(center,disPixmap)
Cat.py
#Cat window created by Dr. Paige Meeker
#Robot Class
#September 5, 2008
#
#Collaboration Statement: I worked on this
program alone.
Cat.py
Setting up the Window
#Import the myro library
from myro import *
#Create a window
animalWindow = GraphWin("My Animal Drawing", 300, 300)
#Change the background color of the window
#For a list of several of the RGB colors, visit:
#http://www.tayloredmktg.com/rgb/#OR
#alternatively, you can use Google with "rgb color codes"
as a search parameter
lightPink = color_rgb(255,182,192)
animalWindow.setBackground(lightPink)
Cat.py
Making a face
#create the face of the cat:
centerCatFace = Point(150,150)
face = Circle(centerCatFace,50)
catColor = color_rgb(255,105,180)
face.setOutline(catColor)
face.setWidth(10)
face.draw(animalWindow)
Cat.py
Making ears
earPt1 = Point(125,110)
tip = Point(125,50)
earPt2 = Point(150,100)
tip2 = Point(175,50)
earPt3 = Point(175,110)
leftEar1 = Line(earPt1,tip)
leftEar2 = Line(tip,earPt2)
leftEar1.setWidth(10)
leftEar1.setOutline(catColor)
leftEar1.draw(animalWindow)
leftEar2.setWidth(10)
leftEar2.setOutline(catColor)
leftEar2.draw(animalWindow)
rightEar1 = Line(earPt2,tip2)
rightEar2 = Line(tip2,earPt3)
rightEar1.setWidth(10)
rightEar1.setOutline(catColor)
rightEar1.draw(animalWindow)
rightEar2.setWidth(10)
rightEar2.setOutline(catColor)
rightEar2.draw(animalWindow)
Cat.py
Making eyes
eyeColor = color_rgb(0,255,0)
eyePt1 = Point(135,135)
eyePt2 = Point(165,135)
leftEye = Circle(eyePt1,10)
leftEye.setFill(eyeColor)
leftEye.draw(animalWindow)
rightEye = Circle(eyePt2,10)
rightEye.setFill(eyeColor)
rightEye.draw(animalWindow)
Cat.py
Making the nose and
mouth
nose = Circle(centerCatFace,5)
nose.setFill(catColor)
nose.draw(animalWindow)
mouthPt = Point(150,175)
mouth = Text(mouthPt,"W")
mouth.setSize(30)
mouth.setTextColor(catColor)
mouth.draw(animalWindow)
Disney.py
To import images into your files, use the following example.
#Disney Image window created by
Dr. Paige Meeker
#Robot Class
#September 5, 2008
#
#Collaboration Statement: I
worked on this program alone.
#Import the myro library
from myro import *
#Create a window
disneyWin = GraphWin("My Disney
Images", 300, 300)
dis = makePicture(pickAFile())
#show(dis)
disPixmap = makePixmap(dis)
center = Point(150,150)
pic = Image(center,disPixmap)
pic.draw(disneyWin)
Disney.py
#Disney Image window created by
Dr. Paige Meeker
#Robot Class
#September 5, 2008
#
#Collaboration Statement: I
worked on this program alone.
#Import the myro library
from myro import *
#Create a window
disneyWin = GraphWin("My Disney
Images", 300, 300)
dis = makePicture(pickAFile())
#show(dis)
disPixmap = makePixmap(dis)
center = Point(150,150)
pic = Image(center,disPixmap)
pic.draw(disneyWin)
#Can also draw on the window
with the picture
circleTest = Circle(center,20)
ccolor = color_rgb(255,182,98)
circleTest.setFill(ccolor)
circleTest.draw(disneyWin)
LoopingCircles.py
#Circle window created by Dr.
Paige Meeker
#Robot Class
#September 5, 2008
#
#Collaboration Statement: I
worked on this program alone.
for i in range(10):
myCircle.move(5,5)
wait(1) #without this, you
cant see the movement!
While
To have your while command repeat forever,
type:
while True:
<do something>
<do something>
Image Processing
and Perception
Dr. Paige H. Meeker
Whats an Image?
Images are pictures, taken by a digital camera
Whats an Image?
Color images are made up of 255 shades of
Whats an Image?
Most digital cameras take photos of at least 6
Megapixels.
Refers to the largest size image the camera can
take
it in variable pic
pic = takePicture(gray) #Takes a grayscale photo with the
Scribbler
getWidth(pic) # gives the width of pic
getHeight(pic) # gives the height of pic
savePicture(pic, NewPicName.jpg) #saves a JPG
savePicture(pic, NewPicName.gif) #saves a GIF
makePicture(directory/nameOfPic.JPGor.GIF) #reads back
in a photo that has been stored
pickAFile() #opens navigational dialog box to allow user a
choice of files to load in.
show(pic) #shows the photo on the monitor
Robot Explorer
You can use the joyStick() function to guide your
Animated GIFs
In chapter 5 (Sensing the World), we made an
Animated GIFs
First, you use the robot to create a list of pictures
Making Photos
You can create photos in fact, you have
Color Values
Myro has some predefined colors:
black = makeColor( 0, 0, 0)
white = makeColor(255, 255, 255)
blue = makeColor( 0, 0, 255)
red = makeColor(255, 0, 0)
green = makeColor( 0, 255, 0)
gray = makeColor(128, 128, 128)
darkGray = makeColor( 64, 64, 64)
lightGray = makeColor(192, 192, 192)
yellow = makeColor(255, 255, 0)
pink = makeColor(255, 175, 175)
magenta = makeColor(255, 0, 255)
cyan = makeColor( 0, 255, 255)
Making Pictures
To create a picture filled with one color:
#define some size for the width and height of the image
Width = Height = 100
#fill in black with any color
newPic = makePicture(width,height,black)
show(newPic)
newPic2 = makePicture(width,height,makeColor(r,g,b))
Show(newPic2)
Picture Commands
getPixel(x,y)
Returns the pixel at location x,y
pickAColor()
Displays a color palette allowing you to chose a new color
repaint(pictureName)
Refreshes the displayed image to allow you to see any
Image Processing
By manipulating the pixels in an image, we
Image Processing
A range of gray? Use the previous function
Image Processing
Why does this work?
Think about the values: location 0,0 will be
Image Processing
Play with the other examples in your book
Image Processing
You dont have to start with a blank image to
factor.
Use the rule:
New pixel at x,y = old pixel at x*Factor, y*Factor
def shrink(pic):
show(pic,"Before")
x = getWidth(pic)
y = getHeight(pic)
F = int(ask("Enter shrink factor"))
newX = x/F
newY = y/F
newPic = makePicture(newX,newY)
for x in range(1,newX):
for y in range(1,newY):
setPixel(newPic, x, y, getPixel(pic, x*F,y*F))
show(newPic,"After")
return newPic
factor.
Use the rule:
New pixel at x,y = old pixel at x/Factor, y/Factor
def enlarge (pic):
show(pic,"Before")
x = getWidth(pic)
y = getHeight(pic)
F = int(ask("Enter enlarge factor"))
newX = x*F
newY = y*F
newPic = makePicture(newX,newY)
for x in range(1,newX):
for y in range(1,newY):
setPixel(newPic, x, y, getPixel(pic, x/F,y/F))
show(newPic,"After")
return newPic
one?
Calculate a blurring of a neighborhood of 8?
Calculate a blurring of a neighborhood of 15?
slide to:
Calculate a color sharpening instead of a grayscale
one?
Calculate a sharpening of a neighborhood of 8?
Calculate a sharpening of a neighborhood of 15?
Image Processing
Negative and Embossing
A negative of an image is obtained by
Negative
def negative(pic):
show(pic,"Before")
W = getWidth(pic)
H = getHeight(pic)
MAX = 255
newPic = makePicture(W,H)
for x in range(W):
for y in range(H):
pixel = getPixel(pic,x,y)
v = MAX - getRed(pixel)
setPixel(newPic, x, y, gray(v))
show(newPic,"After")
return newPic
Image Understanding
We can look at a picture and
photo?
How many are kneeling?
Could you draw a box around
each of their faces?
computationally? So the a
computer or a robot can
Image Understanding
For our robots to perceive their environment
visually:
First we need to take a picture
Then, we ask a question about the environment
We analyze the picture and answer our
question
Answering Questions
Assume we can recognize the ball. How do
we follow it?
while timeRemaining(T):
#take picture and locate ball
if <ball is to the left>
turnLeft(cruiseSpeed)
elif <ball is to the right>
turnRight(cruiseSpeed)
else:
forward(cruiseSpeed)
Answering Questions
So, how do we recognize the ball?
Start at the pixel level if we know what color
the ball is, we can look for pixels that are in that
color range.
The authors ball was pink with R=253 and G
= 66 so looking for a range within acceptable
values of these would help us find the ball!
for pixel in getPixels(p):
r, g, b = getRGB(pixel)
if r > 200 and g < 100:
setRGB(pixel, (255,255,255))
else:
setRGB(pixel, (0,0,0))
Answering Questions
So, how do we recognize the ball?
Using the code on the previous slide, we
effectively filter out the unwanted regions of
the image and can focus on the desired color.
Now, how do you turn left, right, or keep
center?
We know the image is 256 pixels wide; divide
Code illustrated
while timeTemaining(T):
# take picture and locate the ball
pic = takePicture()
ballLocation = locateBall(pic)
if ballLocation <= 85:
turnLeft(cruiseSpeed)
elif ballLocation <= 170:
forward(cruiseSpeed)
else:
turnRight(cruiseSpeed)
Blobs
This kind of threshold filtering is called blob
filtering
Our robots can define blobs
First, take a picture
Then, display the picture and using your mouse,
Object Recognition
Other computations on images include edge
Next Steps?
Type in these code segments and try them