Problem Regarding Color Recognition

Hello,
I am currently trying to make a program that recognizes three colors and then can detect the shape of the color. However, I have hit a problem with recognizing the colors. The third color in the threshold array is never detected. I have moved the variables around in different positions in the array, but it always doesn’t recognize the third. Can anyone tell me what my problem is? And then how I could manage to recognize the shape of the object that is being detected? I will only need to recognize a sphere, cube, and cylinder. theoretically, I could just look for rectangle and circle for the cube and sphere, then if neither of those are recognized, it would be a cylinder. I just do not know how to check within the area around the colored shape.

Here is what I have so far:

import sensor, image, time, math, pyb
from pyb import Pin



#Red == 1
#Blue == 2
#Yellow == 3


thresholds = [(41, 70, 46, 81, 29, 63),(75, 95, -19, 8, 53, 96),(53, 76, -21, -3, -56, -19)]



sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) 
sensor.set_auto_whitebal(False)
clock = time.clock()



while(True):
    clock.tick()
    img = sensor.snapshot()
    for blob in img.find_blobs(thresholds,
    pixels_threshold=100, area_threshold=100, merge=True):

        if blob.code() == 1:
            print("Red Found")

        if blob.code() == 2:
           print("Yellow Found")

        if blob.code() == 3:
            print("Blue Found")

Codes are powers of 2, so the 3 should be a 4. It’s done this way so if you’d see a non power of two that’s actually two or more codes that were merged.

As for the shapes, please use the blob methods that return information about elongation and etc.

Okay! The first part of what you said about calling the third threshold as four makes sense. However, since I am new to OpenMV I am slightly confused about your second answer, can you elaborate a bit?

The blob object has a bunch of methods that return information about it. Like the perimeter and etc. You should observe how these changes for the different shapes and then set filters on this.

Thank you so much, I will definitely look into that.

Another quick question. When I save my code it turns it to a single color and gets rid of the method list when typing. Is there a way to get this back?

Not sure I understand what you mean? Undo?

When I save the code it gets rid of the method list that happens when you press .
And it also gets rid of the colors of the code.
The first image is what it changes to.

I also have a question regarding detecting shapes from within a set spot on the image. I have the corners I want to search within, but I do not know the method that does it.

Hi, is the file end with .py? I don’t see this issue with the IDE.

As for detecting shapes. There is no method for this. find_blobs() can be used to do this however using the method attributes of blobs. E.g. the perimeter + elongation of a blob will be very different per shape type.

Here is the usage:

print(Shapes[checkForShapes(blob.rect()[3],blob.rect()[2],blob.roundness())])

And here is the shape array:

Shapes =["Not established","Cube","Barrel","Sphere"]

So, I changed the function a bit, and I am running into the problem of the sphere being detected as a crate, and the crate being detected as a sphere. Since the thresholds for the sphere are within the crate. How would you recommend I get around this problem?

def checkForShapes(Height, Width, CircleRoundness, Elongation):

    isShape = 0

    if (.53<Elongation<.70) and (.30<CircleRoundness<.40):
        #Barrel
        isShape = 2
    elif (Elongation<.15) and (50<CircleRoundness<1):
        #Sphere
        isShape = 3
    elif(.07<Elongation<.43) and (.53<CircleRoundness<100):
        #Cube
        isShape = 1
    else:
        isShape = 0

    return isShape

Here are the objects:


Use the perimeter. the elongation is for stick like objects. it’s not really accurate for circles and squares. roundess is just 1 - elongation.

Okay! that makes so much more sense than I was thinking.

I am also having some problems with the hue. whenever the camera turns on, the hue is always different depending on what is in the image when it starts. how to I keep this more consistent so the thresholds can stay the same. because the color changes enough that my objects are no longer detected under those color thresholds

You can make the color ranges larger, or just turn off auto white balance and gain immediately.