0

我的代码很长,我需要帮助压缩它以使其更方便。我拥有的代码是假设执行我命令它在侥幸机器人上执行的一组指令。我正在使用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()
4

2 回答 2

5
# 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-01T02:07:07.990 回答
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功能是使像素变为白色而不是黄色。

于 2012-11-01T02:09:32.960 回答