Conversion to 8 bit

i have a pretty basic question but i dont find a solution for it.
I dont really understand the way ImageJ converts a color jpg to 8 bit.
If i have the values
red = 248, green = 177, blue = 149
the sum is 574 and divded by 3 it is 191.3333
ImageJ makes it 192.

If i have the values
red = 247, green = 176, blue = 148
the sum is 574 and divded by 3 it is 190.3333
ImageJ makes it 190.

This look just random to me, does anyone know how exactly that gets calculated? Is it not just pixel by pixel? All alpha values are 255 by the way.

Best regards

I haven’t checked the code, but just off the top of my head I assume it is something like:
Where each step is rounded to 0 decimal places prior to addition.

You can find the equations here:

RGB images are converted to grayscale using the formula gray=(red+green+blue)/3 or gray=0.299red+0.587green+0.114blue if “Weighted RGB to Grayscale Conversion” is checked in Edit>Options>Conversions .

See also:


1 Like

I think his original point was that this is incorrect (rounding differences) based on the results he listed. Thanks for linking the code.

The pixels are converted to grayscale using the formula

Which matches both posts above if the type for each operation is locked to an integer or byte, I suppose.

Thanks you @Research_Associate.

@Gwinzy: When I convert a created RGB image with your values red = 248, green = 177, blue = 149

I get a grayscale image with : 191

Which I also get with correct Java code.

yes, you are right (MycroscopyRA). When i look at the pixel in imageJ they are the same as my code. If i save them as jpeg and look at them in matlab as i did the values are different, probably the converting to jpeg changed them slightly?
Thanks for your help everyone.

1 Like

JPEG will change all of the values in your image - it is generally not considered appropriate for scientific analysis for this reason. Though, for whole slide images it is still frequently used due to file size.

1 Like