def getDirection(img, color): width = img.width height = img.height if color == 'blue': filtered = blueOnly(img) elif color == 'red': filtered = redOnly(img) elif color == 'green': filtered = greenOnly(img) db.showImg(filtered) sumLeft = np.sum(filtered.regionSelect(0, 0, width/2, height).getGrayNumpy()) sumRight = np.sum(filtered.regionSelect(width/2, 0, width, height).getGrayNumpy()) sumMid = np.sum(filtered.regionSelect(width/5*2, 0, width/5*3, height).getGrayNumpy()) sumBoth = sumLeft + sumRight x = width*height #print("sumleft: " + `sumLeft` + ". sumMid: " + `sumMid` + ". sumRight: " + `sumRight` + ". sumBoth: " + `sumBoth`) if sumBoth > 40*x or sumMid > 1*x: return 'forward: ' + `sumBoth/x` elif sumLeft > 1*x: return 'left: ' + `sumLeft/x` elif sumRight > 1*x: return 'right: ' + `sumRight/x` else: return 'nothing'
def getDirection(img, color): #applies 2 different blurs to get rid of noise and "normalizes" the colors a bit img = img.copy() blurred = img.gaussianBlur((5, 5), 3, 3) blurred = blurred.medianFilter(11) width = img.width height = img.height if color == 'blue': filtered = blueOnly(blurred) elif color == 'red': filtered = redOnly(blurred) elif color == 'green': filtered = greenOnly(blurred) db.showImg(filtered) #finds all the blobs and check whether they are balloon-y by checking the hu-moments with known (cached) blobs balloons = filterBalloons(filtered) if balloons: #[b.drawOutline(layer=filtered.dl(), color=SimpleCV.Color.CRIMSON, width=3) for b in balloons] filtered = blobsToImage(balloons) else: return "idle" #sum of balloon-pixels in each section sumLeft = np.sum(filtered.regionSelect(0, 0, width/2, height).getGrayNumpy()) sumRight = np.sum(filtered.regionSelect(width/2, 0, width, height).getGrayNumpy()) sumMid = np.sum(filtered.regionSelect(width/5*2, 0, width/5*3, height).getGrayNumpy()) sumBoth = sumLeft + sumRight #the x factor is to make the sums independent of resolution x = width*height #the image is split up in 3 sections, each having a threshold sum #"forward" is a bit of an exception, since a balloon right in front of us can "fill" multiple sections if sumBoth > 40*x or sumMid > 0.7*x: ret = "forward"#: " + `sumBoth/x` elif sumLeft > 0.7*x: ret = "left"#: " + `sumLeft/x` elif sumRight > 0.7*x: ret = "right"#: " + `sumRight/x` else: ret = "idle" db.showImg(filtered) return ret
def findColorOrder(self, img): if COMPENSATEYELLOW == True: t1 = 100; t2 = 130; t3 = 35 t4 = 100; t5 = 40; t6 = 100 t7 = 5; t8 = 5; t9 = 180 t4 = db.t(0) t5 = db.t(1) t6 = db.t(2) else: t1 = 100; t2 = 130; t3 = 35 t4 = 30; t5 = 30; t6 = 60 t7 = 150; t8 = 5; t9 = 200 #basic blurs to remove noise and even out the colors img = img.copy() median = img.gaussianBlur(3, 3) median = median.medianFilter(7) #RED = (213, 78, 29) if COMPENSATEYELLOW else (132, 168, 44) GREEN = (213, 78, 29) if COMPENSATEYELLOW else (63, 57, 89) #BLUE = (213, 78, 29) if COMPENSATEYELLOW else (132, 168, 44) #apply (hueDistance . treshold . invert), multiplies by values which somehow improve detection.. #todo adaptive thresholding (binarize). perhaps to create a mask for more precise thresholding later on? red = (median.hueDistance(SimpleCV.Color.RED, minsaturation=t1, minvalue=t2) *2).threshold(t3).invert() green = (median.hueDistance(SimpleCV.Color.GREEN, minsaturation=t4, minvalue=t5)*1).threshold(t6).invert() blue = (median.hueDistance(SimpleCV.Color.BLUE, minsaturation=t7, minvalue=t8) ).threshold(t9).invert() #blue = blue.invert() if COMPENSATEYELLOW == False else blue #(erode . dilate) to remove "messy" blobs and make the card-blob more square-like redBlobs = red.erode(10).dilate(15).findBlobs(minsize=1000) greenBlobs = green.erode(10).dilate(10).findBlobs(minsize=1000) blueBlobs = blue.erode(10).dilate(10).findBlobs(minsize=1000) median.addDrawingLayer(red.dl()) if redBlobs and greenBlobs and blueBlobs: #filter all blobs with a square-y ratio #s1 = float(db.t(0))/100 #s2 = float(db.t(1))/100 s1 = 1.50 s2 = 0.20 redsquares = (redBlobs).filter([b.isSquare(s1, s2) for b in redBlobs]) greensquares = (greenBlobs).filter([b.isSquare(s1, s2) for b in greenBlobs]) bluesquares = (blueBlobs).filter([b.isSquare(s1, s2) for b in blueBlobs]) if redsquares and greensquares and bluesquares: #draw the blobs to our initial blurred image, for debug and spider-o-vision! redsquares[-1].draw(layer=median.dl()) greensquares[-1].draw(layer=median.dl()) bluesquares[-1].draw(layer=median.dl()) greensquares[-1].drawOutline() db.showImg(median) #find the 3 blobs, order by height, extract the ordered color strings colors = [('red', redsquares), ('green', greensquares), ('blue', bluesquares)] ordersTuple = sorted(colors, key=lambda color: (color[1][-1].y)) orders = [x[0] for x in ordersTuple] return orders #display failing blob-image here. db.showImg(red) db.showImg(green) db.showImg(blue) #didn't find all 3 RGB squares return None