def greenOnly(value): return (value.hueDistance(GREEN, minsaturation=db.t(0), minvalue=db.t(1))).threshold(db.t(2)).invert()
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