Python Condensing Code

时间:2012-11-01 01:59:36

标签: 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()

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

如果您有ARGB像素替换,则此实现适用于您拥有RGB像素的想法:

带有0xFFFFFFFF

0xFFFFFF

我正在做的是:

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功能会使像素变为白色而非黄色。

相关问题