# This code takes a picture, finds all of the "redish" pixels, # calculates their center (average X & Y) and draws a red box on the # center of the "redish" pixels. from myro import * initialize("/dev/rfcomm1") #This function will find all red pixels and turn them white, and turn #all other pixels black. def findRed(picture): for pixel in getPixels(picture): if ( getRed(pixel) > 175 and getBlue(pixel) < 75 and getGreen(pixel) < 75) : setRed(pixel,255) setGreen(pixel,255) setBlue(pixel,255) print "it's a red pixel!" else: setRed(pixel,0) setGreen(pixel,0) setBlue(pixel,0) #This function will calculate the average X and Y values of #all white pixels in the picture, returning it as a tuple. def findAverageWhite(picture): counter = 0 accumulatorX = 0.0 accumulatorY = 0.0 for pixel in getPixels(picture): if ( getRed(pixel) == 255): accumulatorX = accumulatorX + getX(pixel) accumulatorY = accumulatorY + getY(pixel) counter = counter+1 if (counter == 0): print "No White Pixels!" return (-1,-1) averageX = accumulatorX / counter averageY = accumulatorY / counter return (averageX,averageY) #This function draws a red box centered on the specified X,Y position. # def makeDot(picture,x,y): intX = int(x) intY = int(y) for pixel_x in range(intX-3,intX+4): for pixel_y in range(intY-3, intY+4): pixel = getPixel(picture,pixel_x,pixel_y) setRed(pixel,255) setBlue(pixel,0) setGreen(pixel,0) p = takePicture() show(p) findRed(p) x,y = findAverageWhite(p) print "X,Y is:", x, y if (x != -1): makeDot(p,x,y) show(p)