Get Contrast out from an image

adrjork's icon

Hi everyone,
jit.rgb2hsl + jit.3m give me a lot of infos BUT contrast.
In a forum I found <<go over all the pixels, add all the brightness values, then divide by the number of pixels to calculate the average brightness of the image. Then go over each pixel again, calculate the difference between the brightness and the avg. brightness. square the result. Add the values for all pixels. Then divide by the number of pixels. I think that's pretty much the formula.>>
I tried to make a jit.gen object but without success.
Did Anyone find a solution?

Thanks in advance.

Floating Point's icon

no need to use jit.gen (but you could if you want), just use some jit.ops:

Max Patch
Copy patch and select New From Clipboard in Max.

edit: reposted patch with correct rescaling value (4 instead of 1.4)

adrjork's icon

Great, thanks so much!!!
Just a couple of questions:
1. Why rescaling at the end? (Why * 4?)
2. Are you sure that jit.3m outputs the avg.brightness? (Because I think that it outputs the avg.luma instead, i.e. the "perceived" brightness that's .299*Red + .587*Green + .114*Blue.)
3. Are you so kind to show me a jit.gen version? (jit.op seems to be such demanding for my CPU...)

Thanks in advance

Floating Point's icon

1. if you don't rescale, the max contrast can only be of value 0.25:
consider a 2-pixel image with values 0 and 1 (maximum contrast):
(0+1)/2 = 0.5; //average brightness
0-0.5= -0.5, 1-0.5 =0.5; //the differences are -0.5 and 0.5
square them: 0.25, 0.25
average contrast is thus 0.25, therefore to get a range between 0 and 1, multiply by 4

2. well you can use a different conversion if you want; I just chose the simplest.
If you want to use rgb2hsl you'll need to unpack the l plane into its own matrix

3. I'm no expert on jit.gen (or jit.pix or jit.gl.pix)-- I'm sure it would be pretty easy-- best to figure it it yourself!

adrjork's icon

Thanks really a lot for the explanations.