2
votes

I am processing an image pixel by pixel and I need to get name of the color for each pixel. I have these main color names: yellow, magenta, cyan, red, green, blue, white, black. I also have RGB and CMYK numbers of each pixel. How would I approximate the color of the pixel to one of the above based on those numbers? It does not need to be precise, just a very general approximation. Is there any maths that I can do with RGB or CMYK to determine that? I would rather prefer a simple solution than a precise one.

2
You're asking how to tell whether a color is mainly yellow given the cyan, magenta, yellow and black components of the color? Have you at least tried to do this?beaker
Well, for example i have a pixel with RGB(70,200,220). How do i determine whether it is considered yellow or magenta or cyan, etc..Army
What are the CMYK values for that color?beaker
it would be (68%, 9%, 0%, 14%) for this particular oneArmy
I can't tell if you're looking at an image of the color or the CMYK values. So let me suggest this: if you take the CMYK values and convert the maximum value to 1 and the minimum value to 0. Then, for the other two colors, assign them 1 or 0 based on whether their value is closer to the max or the min. Now you have a table of 16 possible values that you should be able to map to your 8 colors.beaker

2 Answers

2
votes

Let's take this as a starting image:

enter image description here

Now, make a map of all the colours that we want to look for, bearing in mind that ImageMagick uses X11 colornames where green is named lime:

magick xc:black xc:white xc:red xc:lime xc:blue xc:cyan xc:magenta xc:yellow +append map.png

That makes this - which I have magnified because it is only 8 pixels wide and 1 pixel tall:

enter image description here

Now, we just ask imageMagick to map all the pixels in Paddington to whatever colour is nearest in the colours in our map:

magick paddington.png +dither -remap map.png result.png

enter image description here

Now we look at the distribution of pixels in the colormap of the result:

magick identify -verbose result.png | grep -A9 Histogram

Output

Histogram:
 78839: (  0,  0,  0) #000000 black
 15057: (  0,  0,255) #0000FF blue
     1: (  0,255,  0) #00FF00 lime
 22422: (  0,255,255) #00FFFF cyan
 18103: (255,  0,  0) #FF0000 red
    11: (255,  0,255) #FF00FF magenta
  5809: (255,255,  0) #FFFF00 yellow
 19758: (255,255,255) #FFFFFF white

And we can see there are 78839 pixels out of 400x400 that are black, and 15057 pixels that are blue... and so on.

Note that you can do all this with wand which is a Python binding to ImageMagick.

0
votes

While the other solution posted will work for KNOWN names, take a look at models and papers like this one to actually generalize and understand how to translate (even) unknown colors to useful names.

Edit: Even though this isn't what OP wanted, I ended up spending a couple of hours to wire it all up to work easily within Docker/VSCode. See here for a way to quickly get up and running with this repository.

I hope this helps someone!