"标准" RGB到灰度转换 [英] "Standard" RGB to Grayscale Conversion
问题描述
我正在尝试编写一个转换器算法,该算法采用JPEG图像并返回其PGM(便携式灰度图)版本。
问题是我无法理解官方JPG-> PGM转换器如何根据从经典RGB格式开始分配给最终像素(我猜,0-> 255)的值来工作。
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.
一开始,我使用了这个公式(它与OpenCV的CV_RGB2GRAY转换使用的公式相同):
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
0.30*R + 0.59*G + 0.11*B = val
我写了一个简单的代码来测试我的结果:它需要一个彩色图像及其PGM版本(已经转换)使用GIMP)。然后它使用前面的公式转换彩色图像。目标是使像素到像素的灰度图像等于PGM输入。
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?
推荐答案
问题是我无法理解官方JPG-> PGM转换器根据从经典RGB格式开始分配给最终像素(我猜,0-> 255)的值来工作。
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
我相信你想使用 C srgb
。
尝试一下,看看它是否与您期望的结果相符。
I believe you want to use the formula for Csrgb
.
Try it out and see if it matches the results you're expecting.
基本上,你会这样做:
- 取
R,G,B
颜色(每个[0,1]
范围)
- 如果他们是我在范围
0..255
中,只需除以255.0
- 如果他们是我在范围
- Take
R, G, B
color (each in[0,1]
range)- If they're in the range
0..255
instead, simply divide by255.0
- If they're in the range
- 这可能是你之前申请的线性变换
- 这是您缺少的非线性伽玛校正件
- 查看 此WolframAlpha情节
-
C srgb = 12.92 C linear
当C linear < = 0.0031308
-
C srgb = 1.055 C linear 1 / 2.4 - 0.055
当C < sub> linear > 0.0031308
- This is the nonlinear gamma correction piece you were missing
- Check out this WolframAlpha plot
Csrgb = 12.92 Clinear
whenClinear <= 0.0031308
Csrgb = 1.055 Clinear1/2.4 - 0.055
whenClinear > 0.0031308
这篇关于"标准" RGB到灰度转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!