Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Standard" RGB to Grayscale Conversion

Tags:

I'm trying to write a converters algorithm that takes a JPEG image and returns its PGM (Portable Gray Map) version. The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.

At the beginning, I used this formula (it's the same used by OpenCV's CV_RGB2GRAY conversion):

0.30*R + 0.59*G + 0.11*B = val

I wrote a simple code to test my results: it takes a color image and its PGM version (already converted using GIMP). Then it converts the color image using the previous formula. The goal is to have a grayscale image that is pixel-to-pixel equal to the PGM input.

At this point, it does not return the same values. Can you help me?

like image 776
TheUnexpected Avatar asked Jul 12 '13 13:07

TheUnexpected


People also ask

How do you calculate RGB to gray?

You just have to take the average of three colors. Since its an RGB image, so it means that you have add r with g with b and then divide it by 3 to get your desired grayscale image. Its done in this way.

What are the RGB values for grayscale?

Gray Among The RGB - You Try It The RGB scale is calibrated so that when a color's three red/green/blue numbers are equal, the color is a shade of gray. E.g. red=50 green=50 blue=50 is gray, without any bias towards red, green, or blue hue.

How do you convert color to gray scale?

Edit menu > Edit Colors > Convert To Grayscale NOTE: Use the Edit > Edit Colors > Adjust Colors command to convert objects to grayscale and adjust the shades of gray at the same time.

Why do we convert RGB to grayscale?

Because it is a one layer image from 0-255 whereas the RGB have three different layer image. So that is a reason we prefer grey scale image instead of RGB.


1 Answers

The problem is that I can't understand how the "official" JPG->PGM convertitors work in terms of what value to assign to the final pixel (i guess, 0->255) starting from the classic RGB format.

There is likely a gamma adjustment in the conversion those "official" tools are using.
That is, it is not just a linear transform.

See this Wikipedia section for the details: Converting color to grayscale

I believe you want to use the formula for Csrgb.
Try it out and see if it matches the results you're expecting.

Basically, you'll do this:

  1. Take R, G, B color (each in [0,1] range)
    • If they're in the range 0..255 instead, simply divide by 255.0
  2. Compute Clinear = 0.2126 R + 0.7152 G + 0.0722 B
    • This is likely the linear transform you were applying before
  3. Compute Csrgb according to it's formula, based on Clinear
    • This is the nonlinear gamma correction piece you were missing
    • Check out this WolframAlpha plot
    • Csrgb = 12.92 Clinear when Clinear <= 0.0031308
    • Csrgb = 1.055 Clinear1/2.4 - 0.055 when Clinear > 0.0031308
like image 181
Timothy Shields Avatar answered Nov 10 '22 08:11

Timothy Shields