View unanswered posts | View active topics It is currently Wed Oct 01, 2014 7:32 pm






Reply to topic  [ 3 posts ] 
Finding closest color (beyond basic colors) 
Author Message
Rookie

Joined: Tue Nov 22, 2011 10:45 am
Posts: 12
Post Finding closest color (beyond basic colors)
I have 12 Duplo bricks in different colors and would like for the ColorSensor to tell which is which with good probability. Finding base colors like Blue, Red or Yellow is easy enough, but I would like to know the best way to find out other colors, like "light blue" or "black" (I even have a transparent Cyan one, but let's disregard this for now).

My current algorithm notes all brick colors (as recorded with the color sensor), then during program run converts the current RGB into Hue Saturation Value, and then does a closest match as below:

Code:
int getClosestIndex(int red, int green, int blue) {
    int closestIndex = 0;
    int closestDistance = 10000;
    int i;

    float hue, saturation, value;
    rgbToHsv(red, green, blue, hue, saturation, value);
    for (i = 0; i < 12; i++) {
        int thisDistance =
                abs(brick[i].hue - hue) +
                abs(brick[i].saturation - saturation) * .8 +
                abs(brick[i].value - value) * .2;
        if (thisDistance < closestDistance) {
            closestDistance = thisDistance;
            closestIndex = i;
        }
    }

    return closestIndex;
}


This works well for some bricks, and not so well for others (e.g. it can't really differentiate betwen red and brown).

Is there a better way?

Thanks!


Wed Nov 23, 2011 6:37 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: Finding closest color (beyond basic colors)
Which sensor are you using? The LEGO one or the one from HT?

The trouble with red and brown is that they're way too similar, depending on the light that is being reflected on them. Maybe you need to tighten up your distance function a bit or have more control over the light that is being reflected on the bricks.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Thu Nov 24, 2011 2:35 am
Profile WWW
Rookie

Joined: Tue Nov 22, 2011 10:45 am
Posts: 12
Post Re: Finding closest color (beyond basic colors)
Thanks Xander. I'm using the Lego NXT 2.0 pack's color sensor. I'm also using a bit of your HSV conversion code, I noticed your work and tutorials in a lot of places and it's very helpful!

Beyond just Brown vs Red, other colors also cause me trouble. For instance I got dark gray, light gray, and black, yet the white table is also sometimes triggering one of those. I got a light green and light blue, which is also hard to differentiate. I'll try fine tune my approach and also see if I can find other, less reflective material. If all else fails, I might look into using two-color codes, e.g. "blue then yellow" would mean one thing, "red then green" another thing, just using the base colors red, blue, yellow and green, which (while not quite what I had in mind) would give me the 12 possibilities.


Thu Nov 24, 2011 3:18 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.