sightmachine / SimpleCV

The Open Source Framework for Machine Vision
http://simplecv.org
BSD 3-Clause "New" or "Revised" License
2.7k stars 799 forks source link

Problem with filtering blobs by colour #697

Closed golyalpha closed 6 years ago

golyalpha commented 7 years ago

So, I have been at this for the past few days. No-one on stackoverflow apparently knows how to filter SimpleCV blobs by colour, and when it seems I have figured it out, this error comes up: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Code:

def calibrate():
    camera = Camera()
    display = Display()
    Rmin = 255
    Rmax = 0
    Gmin = 255
    Gmax = 0
    Bmin = 255
    Bmax = 0
    for i in range(1, 100):
        image = camera.getImage()
        image.applyGaussianFilter()
        blobs = image.findBlobs()
        try:
            blobs.sortColorDistance((253, 231, 184)).draw(autocolor=True)
            image.drawCircle(blobs[0].coordinates(), 25, (0, 255, 0), 2)
            image.save(display)
            if blobs[0].meanColor()[0] < Rmin:
                Rmin = blobs[0].meanColor()[0]
            if blobs[0].meanColor()[0] > Rmax:
                Rmax = blobs[0].meanColor()[0]
            if blobs[0].meanColor()[1] < Gmin:
                Gmin = blobs[0].meanColor()[1]
            if blobs[0].meanColor()[1] > Gmax:
                Gmax = blobs[0].meanColor()[1]
            if blobs[0].meanColor()[2] < Bmin:
                Bmin = blobs[0].meanColor()[2]
            if blobs[0].meanColor()[2] > Bmax:
                Bmax = blobs[0].meanColor()[2]
            print i
        except AttributeError:
            i -= 1
            pass

    while True:
        image = camera.getImage()
        image.applyGaussianFilter()
        blobs = image.findBlobs()
        try:
            blobs.sortColorDistance((253, 231, 184))
            print (
                Rmin < blobs[0].meanColor()[0] < Rmax and
                Gmin < blobs[0].meanColor()[1] < Gmax and
                Bmin < blobs[0].meanColor()[2] < Bmax
            )
            blobs.filter(
                Rmin < blobs.meanColor()[0] < Rmax and
                Gmin < blobs.meanColor()[1] < Gmax and
                Bmin < blobs.meanColor()[2] < Bmax
            )
            blobs.draw(autocolor=True)
            image.drawCircle(blobs[0].coordinates(), 25, (0, 255, 0), 2)
            image.save(display)
        except AttributeError:
            pass

It trips up on the Rmin < blobs.MeanColor()[0] < Rmax part. When I try to add .all() or .any() it trips up with "Boolean index array should have 1 dimension", in the return code of the filter method.

golyalpha commented 6 years ago

Nevermind. That's not how npArrays work.