4
votes

I'm working on my Final Bachelor Project in Computer Science and for now I'm in a dead end. Here is what I got stuck on:

I'm trying to classify any color (rgb code) in any of 8 (eight) simple colors. In short terms I need to find 8 intervals where any colour can be placed and be considered a basic color (red, blue, green, black, yellow, purple, grey, brown ).

example:
(18,218,23) to be classified as "green"
(81,,214,85) also "green"
but
(15,52,16) needs to be "black"
(110,117,110) needs to be "grey"

So there are 256 x 256 x 256 possible colors and I need to divide them in 8 (intervals) basic colors.

I'm waiting for some suggestions.

Cheers !


To be clear (as I've seen in comments) I'm looking for a particular set of 8 colors (red, black, green, brown, blue, purple, grey, yellow). Sorry for the orange above !

5
Um... no it doesn't. You can specify ranges in your if-else blocks... For example, black could mean something like <60, <60, <60, etc.Tanvir Ahmed
What about calculating the distance of the colour that is to be classified to each of the eight "primary" colours? If you do this, you should keep in mind that short distances in the RGB space do not necessarily correspond to human perception. For that, you should convert to LUV or a similar space.Gnosophilon
How you can do this depends on exactly which colours you want to classify as what. Then again, once you determine that, you pretty much already have your algorithm right there.harold
Whichever way you end up choosing will for some colours be subjectively wrong to some people anyway. People don't really agree on how to label colours that are "in between" proper colours.harold

5 Answers

3
votes

Based on your example, I'd start with determining whether all components are approximately the same, or does on stand out. If they are about the same, then decide if the values are small enough to be black or not, then it's grey. If one value is different from the other two, then it is easy to check which is different and pick one of six possible colors accordingly.

Alternatively, set each component to either 0 or 1 according to a threshold, then you have 8 combinations to map to 8 colors.

threshold = 100:
(18,218,23)   -> (0, 1, 0) - to be classified as "green"
(81,214,85)   -> (0, 1, 0) - also "green"
(15,52,16)    -> (0, 0, 0) - to be "black"
(110,117,110) -> (1, 1, 1) - to be "grey"
8
votes

Don't do this in RGB, convert to a more convenient color space HSV is probably easiest - then the 8 "colors" are simply 8 intervals along the Hue axis.

3
votes

A simple solution is to do the distance from a defined points that you have labeled as a specific color. This is not a full proof solution, however it is an easy solution, and should work decently.

Long answer: Color spaces are annoying and don't translate well to labels.

1
votes

RGB is horrible for this kind of thing, so your first step should be converting the color to a Lab color space. You can find many open implementations of this conversion online. Once you have your Lab color, everything becomes very simple. The three values become coordinates in three dimensions which you can easily section according to color - check out the images in the wikipedia link.

0
votes

Don't know too much about Color scheme but calculating Euclidean distance would be a good solution.