2012-11-01 59 views
0

我的代码很长,我需要帮助冷凝它以使其更加方便。我所拥有的代码是假设我执行了一组指令,让它在侥幸机器人上执行这些指令。我正在使用python。机器人假设使用它的传感器执行以下代码。我需要帮助凝结它。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() 
+1

你可以实现'getRGB'和'setRGB'而不是'getRed','setRed',...?这样,你可以做'r,g,b = getRGB(pix)'和'setRGB(pix,r,g,b)'。 – Blender

+0

你的代码不是很长。事实上,我会说这很短。 – aib

+0

除非这是一个关于您认为可能被python内建函数或某事压缩的特定代码的非常集中的问题,否则我认为像这样的问题(带有工作代码)可能更适合codereview.stackexchange。我会投票结束,我们会看看是否有其他人同意... – mgilson

回答

4
# 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) 
+0

非常感谢你的帮助!我将回顾这段代码,看看它是否有效。 –

+0

我很抱歉打扰你,但我无法使用代码和我的。我用我的整个代码编辑了我的帖子。你认为你可以看看它,因为当我尝试使用你的代码时,它不能正常工作。我假设自从我遗漏了我的代码的其他部分。 –

+0

其实我没有想到它的工作!再次感谢你! –

1

此实现适用于你有一个ARGB像素的想法,如果你有一个RGB像素替换:

0xFFFFFFFF0xFFFFFF

我在做什么是这样的:

pixel = A R G B 
A = 2 bytes 
R = 2 bytes 
G = 2 bytes 
B = 2 bytes 

所以在十六进制是:

pixel = 0xFF000000; # black 
pixel = 0xFFFFFFFF; # white 

A必须是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功能正在使像素变成白色而不是黄色。

+0

如果'pix'是一个十六进制值,则无法从发布的代码中知道。它可能是另一个带有getter和setter的类实例。 – kevintodisco

+1

@ktodisco,也许他们有相同的功课 –

+0

是否有可能直接使用'len()'获得totalPixels或者将宽度乘以高度或某物? –