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?
To harold's point about the "Y plane": standard color JPEGs are encoded using the YCbCr colorspace, where Y is the luminance component (i.e. the brightness) and Cb and Cr are the blue-difference and red-difference chroma components. So one way of turning a color JPEG into a grayscale one is to simply drop the Cb and Cr components.
There is a utility called
jpegtran
than can do this losslessly, using the-grayscale
option. (The lossless part would really only matter if you wanted to end up with a JPEG and not PGM, to avoid generation loss.) In any case, this would probably be the fastest way to do this transformation, because it doesn't even decode the image into pixels, much less doing math on each one.SIMPLE ALGORITHM TO CONVERT RGB IMAGE TO GRAYSCALE IN OPENCV PYTHON!
I used comments so code is self-explanatory.But it works swiftly.
SO I used this image file ...
My program generated following Grayscale file ..
Average method is the most simple one. 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.
If you have an color image like the image shown above and you want to convert it into grayscale using average method.
Converts a single input pixel in the default RGB ColorModel to a single gray pixel.
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:
R, G, B
color (each in[0,1]
range)0..255
instead, simply divide by255.0
Clinear = 0.2126 R + 0.7152 G + 0.0722 B
Csrgb
according to it's formula, based onClinear
Csrgb = 12.92 Clinear
whenClinear <= 0.0031308
Csrgb = 1.055 Clinear1/2.4 - 0.055
whenClinear > 0.0031308
In theory, with a few pixels (3, in this case), you can determine what their algorithm is doing. Juste pick your three pixel (p1, p2, p3), their RGB value and their PGM gray value, and you have:
RedConstant * p1.redValue + GreenConstant * p1.greenValue + BlueConstant * p1.blueValue = p1.grayValue
RedConstant * p2.redValue + GreenConstant * p2.greenValue + BlueConstant * p2.blueValue = p2.grayValue
RedConstant * p3.redValue + GreenConstant * p3.greenValue + BlueConstant * p3.blueValue = p3.grayValue.
Then solve this problem (look up "equation solver" or something) and see what are the constants they use.