Frage

Given different values of RGB triplet (ex. [255, 255, 255] or [1,2,3]), I want to map it to a fixed set of colors (Red,Blue,Green,Black,White,Yellow,Pink,Purple,Beige,Brown,Orange,Grey).

As an example, when I give "0,0,0", BLACK should be the mapped output. Simialrly, Grey for "190,190,190". Values of the map will be enumerated colors defined above (12 in number).

Maintaining a reverse mapping with fixed set of colors and calculating square difference of every new RGB triplet with all the elements of map is the one way but results are not that good with the approach. Reason of bad results could be that 12 colors we have chosen are not uniformly distributed in the color space (not sure though :)).

This data will further be used for clustering.

Is there any library (preferably Java/C++ or OpenCV) or website which does the similar task and I could leverage it?

Thanks in advance!

War es hilfreich?

Lösung

First get to hashmaps, one with

Map1: colourname->Colourvalue
example: "black" -> (255,255,255), "blue" -> (0,0,255)

Map2: Colourvalue->colourname
example: (255,255,255) -> "black", (0,0,255) -> "blue"

If you get a colour for a value(0,10,240) you want to check which of the known entrys in map2 it is closest too.

So if your colours a approximately equidistant (else see bottom) you compute the euclidean distance and get something like

dist((0,10,240), (0,0,255)) ~ 20 (number guessed)
dist((0,10,240), (255,255,255)) ~ 20000 (number guessed)

And you know that your colour value (0,10,240) should be mapped to "blue".

A few things to keep in mind:

  1. You might want to check out different colour spaces. I would suggest the LAB colour space, it was designed, so that distances are similar to how humans experience the world.

  2. Checkout the XKCD Colour Survey. If you are only working with RGB you could even store a static mapping of all colours to their correct names. If oyu have the space you would have an instant lookup. (To save space use an threedimensional array from [r,g,b] -> char, and lookup the colourname depending on you char)

enter image description here

edit: You have to check if your colours are approximately equidistant (same distance) from each other. Otherwise some colours might get too much exposure. (for example in the xkcd colour chart some colours like Dark Brown have only tiny regions and while others are very large.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top