View unanswered posts | View active topics It is currently Fri Jul 01, 2016 4:20 am

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Finding closest color (beyond basic colors)
Author Message
Rookie

Joined: Tue Nov 22, 2011 10:45 am
Posts: 12
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

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
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
Rookie

Joined: Tue Nov 22, 2011 10:45 am
Posts: 12
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 3 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

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