There are three different things going on here.
- Reduce the number of colors of an image
- Get the different colors of an image
- Get the color name
Reduce the number of colors
There are many techniques to reduce the number of colors. Here you can see how to use color quantization and kmeans.
Another approach could use the median cut algorithm (not shown here).
OpenCV provides the Non-Photorealistic Rendering module. Here you can see some examples of how to use it.
Get the different colors of an image
This is pretty easy. Just iterate over the whole image. If you see a new color, store its value, with counter equal to 1. If you see a color already seen, increment its counter. A std::map
could be useful here.
Get the color name
I won't show it here. But online there are some useful resources. You need a list of all named colors. Keep in mind that not every color has a name. In fact, all possible colors for RGB values would be 256*256*256
. So find the closest color in your list, and assign its name to your current color.
For example, with this input image,
using kmeans approach, I get the reduced color image:
And its palette is:
Color: [14, 134, 225] - Area: 5.28457%
Color: [16, 172, 251] - Area: 27.3851%
Color: [22, 68, 101] - Area: 3.41029%
Color: [28, 154, 161] - Area: 3.89029%
Color: [40, 191, 252] - Area: 22.3429%
Color: [87, 204, 251] - Area: 8.704%
Color: [161, 222, 251] - Area: 3.47429%
Color: [253, 255, 255] - Area: 25.5086%
You can now search for the closest color name in your list, and you'll get what you need. How to make up the GUI to show these information is up to you: the data is all there.
Code:
#include
#include
#include
#include