pythonpython-2.7computer-visionsimplecv

Filter blobs by meanColor close to a specified colour SimpleCV


SimpleCV has this nifty function to filter blobs based on certain criteria.

blobs.filter(numpytrutharray)

Where numpytrutharray is generated by blobs.[property] [operator] [value].

I need to filter out blobs that are close to a certain colour, SimpleCV uses tuples to store the RGB colour value. Any ideas on how to do that?


Solution

  • If you wanted to do something like

    blobs.filter((rmin, gmin, bmin) < blobs.color() < (rmax, gmax, bmax))
    

    then you can immediately stop what were you doing. That's not how Numpy truth array generation works, if you want to filter blobs using that method you need to do this:

    red, green, blue = [[],[],[]]
    color_array = blobs.color()
    
    # Split the color array into separate lists (channels)
    for i in color_array:
        red.append(i[0])
        green.append(i[1])
        blue.append(i[2])
    
    # Convert lists to arrays
    red_a = np.array(red)
    green_a = np.array(green)
    blue_a = np.array(blue)
    
    # Generate truth arrays per channel
    red_t = rmin < red_a < rmax
    green_t = gmin < green_a < gmax
    blue_t = bmin < blue_a < bmax
    
    # Combine truth arrays (* in boolean algebra is the & operator, and that's what numpy uses)
    rgb_t = red_t * green_t * blue_t
    
    # Run the filter with our freshly baked truth array
    blobs.filter(rgb_t)
    

    Admittedly, a lengthy way to generate the array, but it's probably faster than filtering the color blob by blob manually.