我的代码相当长,我需要帮助来压缩它,使它更方便。我所拥有的代码应该是执行我命令它在fluke机器人上执行的指令集。我使用的是python。机器人应该使用它的传感器执行以下代码。我需要你帮我整理一下。
编辑:
我的代码:
from Myro import *
from Graphics import *
init('/dev/tty.IPRE6-366079-DevB')
def markYellow(pic):
for pix in getPixels(pic):
r = getRed(pix)
g = getGreen(pix)
b = getBlue(pix)
if r > 200 and b < 90 and g > 150:
setRed(pix,255)
setGreen(pix,255)
setBlue(pix,255)
else:
setRed(pix,0)
setGreen(pix,0)
setBlue(pix,0)
def pctMarked(pic):
totalPixels = 0
whitePixels = 0
for pix in getPixels(pic):
if getRed(pix) == 255:
whitePixels = whitePixels + 1
totalPixels = totalPixels + 1
result = whitePixels / float(totalPixels)
return result
def findAvgX(pic):
pixelCount = 0
totalXCount = 0
for pix in getPixels(pic):
if getRed(pix) == 255:
x = getX(pix)
totalXCount = totalXCount + x
pixelCount = pixelCount + 1
avgX = totalXCount / float( pixelCount)
return avgX
def turn():
findAvgX(pic)
if wallLocation <= 85:
turnLeft(1,0.25)
elif ballLocation >= 170:
turnRight(1,0.25)
def celebrate():
move(0.25,1)
beep(1,800)
beep(1,1600)
beep(1,800)
stop()
def main():
p = takePicture()
markYellow(p)
pctMarked(p)
while pctMarked(pic) < 0.2:
rotate(1,1)
p = takePicture()
markYellow(p)
pctMarked(p)
turn()
while getObstacle('center')> 1000: # I'm not sure about the number. We can test it tomorrow
forward(1,1)
celebrate()发布于 2012-11-01 10:07:08
# helper functions
def getRGB(pix):
return getRed(pix), getGreen(pix), getBlue(pix)
def setRGB(pix, r, g, b):
setRed(pix,r)
setGreen(pix,g)
setBlue(pix,b)
def markYellow(pic):
for pix in getPixels(pic):
r, g, b = getRGB(pix)
if r > 200 and b < 90 and g > 150:
setRGB(pix, 255, 255, 255)
else:
setRGB(pix, 0, 0, 0)
def pctMarked(pic):
# is there a more direct way to get the totalPixels?
# totalPixels = len(pic) # perhaps?
totalPixels = sum(1 for pix in getPixels(pic))
whitePixels = sum(getRGB(pix) == (255, 255, 255) for pix in getPixels(pic))
return whitePixels / float(totalPixels)发布于 2012-11-01 10:09:33
如果你有一个RGB像素替换,这个实现基于你有一个ARGB像素的想法:
使用0xFFFFFF的0xFFFFFFFF
我正在做的是:
pixel = A R G B
A = 2 bytes
R = 2 bytes
G = 2 bytes
B = 2 bytes所以在十六进制中就是:
pixel = 0xFF000000; # black
pixel = 0xFFFFFFFF; # whiteA必须为FF (255)才能没有透明度。
值得一提的是,我在这段代码中做了一个假设,即一个像素的形式是一个32位整数。
def markYellow(pic):
for pix in getPixels(pic):
if getRed(pix) > 200 and getBlue(pix) < 90 and getGreen(pix) > 150:
pix = 0xFFFFFFFF;
else:
pix = 0xFF000000;
def pctMarked(pic):
totalPixels = 0
whitePixels = 0
for pix in getPixels(pic):
if pix == 0xFFFFFFFF:
whitePixels += 1
totalPixels += 1
return whitePixels / float(totalPixels)下面是几条评论:
你的pcMarked只会看红色来寻找白色,这意味着它会拾取所有的全红色。
您的markyellow函数正在将像素变为白色而不是黄色。
https://stackoverflow.com/questions/13169971
复制相似问题