Get average HSB from an image

How can i get the average values of HSB from an ofImage? Somebody have experience doing it?some advices would be helpfull.

maybe openCV has handy methods for doing this, if it’s the case it will be more optimized.

however, here is a simple approach:

image.load("image.jpg");

int width = image.getWidth();
int height = image.getHeight();

ofColor color;
float hue = 0.0;
float saturation = 0.0;
float brightness = 0.0;
float avgHue = 0.0;
float avgSaturation = 0.0;
float avgBrightness = 0.0;
int numPixels =  width * height;

for (int y = 0; y < height; ++y)
{
	for (int x = 0; x < width; ++x)
	{
		color = image.getColor(x, y);
		color.getHsb(hue, saturation, brightness);

		avgHue += hue;
		avgSaturation += saturation;
		avgBrightness += brightness;
	}
}

avgHue /= numPixels;
avgSaturation /= numPixels;
avgBrightness /= numPixels;
1 Like

The way I’ve approached this in the past is to resize the image down to 1 x 1 pixel with interpolation, then just check the colour of that pixel! I used OpenCV IIRC but if you have trouble, let me know and I’ll dig out the code.

2 Likes

I had never use IRC in opencv, can you show me some code?

IIRC is short for “If I remember correctly” - sorry for the confusion I really should know better than to use obscure abbreviations!

You should be able to use :

image.resize(1,1);

and then check the colour of the single pixel in that image to get the average.

Hope this helps!

Seb

1 Like