How to calculate the percentage of an image?

If I have an image, How can I use openframeworks to calculate the percentage of each color of the image?
Looking forward your reply!

I’ve seen OF addons to quantize the colors and get a palette out of an image, but I didn’t find one that gives you the frequencies of the colors. Someone knows?

ofxHistogram for instance does almost what you want, but I believe it only gives you the frequencies in each R, G and B channel separately, not combined as RGB colors.

ToxicLibs’ histogram does give you (bucket) color counts. You could port it from Processing. Here an example of how to use it.

The algorithm you are looking for probably works by creating buckets to cluster colors together, and then count how many colors are in each bucket. You do that because pixel colors may be slightly different across the image and it may actually happen that not a single color is repeated in an image because of those subtle variations. See for example this data: https://en.wikipedia.org/wiki/Color_histogram#Example_1 Color 0,0,0 appears 7414 times, and color 0,0,1 appears 230 times, but you probably want to count both as the same color (black) by using an algorithm that groups colors in clusters or buckets.

1 Like