What algorithm does setImageType( OF_IMAGE_GRAYSCALE )use?

Hi, I want to know what algorithm is used to convert RGB to greyscale by using ofImage::setImageType(OF_IMAGE_GRAYSCALE).

As I tested, the following RGB values turned into the following greyscale amount.

67 71 44 => 65

166 195 213 => 197

144 148 37 => 124

I tried out some of common greyscale conversion formulas but none of them worked just like the test result above.

I think it uses freeImage internally, but I don’t know how to see the implementation part of the greyscale method which is “FreeImage_ConvertToGreyscale”.

Can anyone help me out finding the formula? Thanks.

Are you sure about those RGB values? In the source of Freeimage it says:

Luminance is calculated from the sRGB model using a D65 white point, using the Rec.709 formula 
L = ( 0.2126 * r ) + ( 0.7152 * g ) + ( 0.0722 * b )

And in the implementation:

#define LUMA_REC709(r, g, b) (0.2126F * r + 0.7152F * g + 0.0722F * b)
#define GREY(r, g, b) (BYTE)(LUMA_REC709(r, g, b) + 0.5F)

So the formula should be the above one for Luminance + 0.5, but this returns different values from the ones you’ve measured. Did you take those measurements in OF?

1 Like

Yes, I did. I got those measurements from loading a jpg image.
And here’s an example code to measure it without loading an image and I get the same results.

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofImage image;
    image.allocate(100, 100, OF_IMAGE_COLOR);
    image.setColor(ofColor(67, 71, 44));
    image.setImageType(OF_IMAGE_GRAYSCALE);
    cout << image.getColor(0, 0) << endl;
}

result : 65, 65, 65, 255

There’s an odd thing here I realized long ago.
if you call setImageType(...) on an ofImage instance it will use the freeImage algos.

but if you call setImageType(...) on an ofPixels instance a different algorithm is used, as you can see in the following.

@arturo any idea why it is like this and not a single implementation?
best