I would like to check what colors is present in a image. This will be stored in the database and used for a search form. (red=1, green=1, blue=0, yellow=1, black=1, white=1 etc.)
img = Magick::Image.read('phosto-file.jpg').first
img = img.quantize(10
h = img.color_histogram
pp h
{red=12815, green=18494, blue=15439, opacity=0=>13007,
red=44662, green=47670, blue=51967, opacity=0=>18254,
red=17608, green=43331, blue=48321, opacity=0=>11597,
red=21105, green=25865, blue=39467, opacity=0=>10604,
red=15125, green=36629, blue=22824, opacity=0=>10223,
red=52102, green=42405, blue=10063, opacity=0=>12928,
red=39043, green=28726, blue=40855, opacity=0=>7728,
red=10410, green=8880, blue=7826, opacity=0=>13795,
red=25484, green=25337, blue=24235, opacity=0=>7351,
red=44485, green=12617, blue=11169, opacity=0=>14513}
How do I convert the 10 values to color names? red, green, NOMATCH, yellow, black, white etc. Only need the rough color name – not LimeGreen but Green etc.
Best regards.
Asbjørn Morell
If you could represent all of the colors you are looking for in the same number format that ImageMagick returns. Then you could compare the RGB values to find which it is closest to.
So for instance if you have a histogram value of (assuming 255 values for each color, YMMV)
Then you could compare it to each of
So if you add up the difference between all of the elements you’ll get
So you can clearly see that the color is closest to green
UPDATE: Added a solution in Ruby