# # Jay Summet # CS 1301 - October 14th, 2011 # # Released to the public domain. # # Example code to find a green wall by marking all green pixels white and # all non-green pixels black. Then it calculates the average X position of # the white pixels. # from myro import * #This function will take in a color picture and look #for green pixels. If it finds a "green" pixel, it will #mark it white (255,255,255) and any other color pixel #it will mark black (0,0,0) #It does not reutrn anything, but the image that was #passed in gets modified. def markGreen(pic): for pix in getPixels(pic): r = getRed(pix) g = getGreen(pix) b = getBlue(pix) if g / 1.5 > r and r < 100 and b < 100: setRed(pix,255) setGreen(pix,255) setBlue(pix,255) else: setRed(pix,0) setGreen(pix,0) setBlue(pix,0) #This function counts the number of pixels that are #white (vs black) in the picture and returns the count #as a percentage of total pixels def pctMarked(pic): count = 0 totalPix = 0 for pix in getPixels(pic): r = getRed(pix) totalPix = totalPix + 1 if r == 255: count = count + 1 pct = 100 * (count / float(totalPix) ) return pct #This function will calculate the average X position of #all 'marked' pixels. (marked pixels are white) def findAvgX(pic): pixelCount = 0 totalXCount = 0 for pix in getPixels(pic): g = getGreen(pix) if g == 255: pixelCount = pixelCount + 1 x = getX(pix) totalXCount = totalXCount + x avgX = totalXCount / float(pixelCount) return(avgX) def drawVLine(pic, x): pHeight = getHeight(pic) for y in range(pHeight): pix = getPixel(pic,x,y) setRed(pix,0) setGreen(pix,255) setBlue(pix,0) init("/dev/rfcomm1") p = takePicture() #p = loadPicture("rca1.gif") p2 = copyPicture(p) show(p, "orig") markGreen(p2) numGreen = pctMarked(p2) print "percent marked is:", numGreen if numGreen > 1.0: print "Found a green wall!" avgX = findAvgX(p2) print "Avg X is:", avgX drawVLine(p2,int(avgX)) else: print "no wall in sight!" show(p2)