【问题标题】:WICConvertBitmapSource BGR to Gray unexpected pixel format conversionWICConvertBitmapSource BGR 到 Gray 的意外像素格式转换
【发布时间】:2021-06-08 11:29:33
【问题描述】:

我正在使用WICConvertBitmapSource 函数将像素格式从 BGR 转换为灰色,我得到了意想不到的像素值。

...

pIDecoder->GetFrame( 0, &pIDecoderFrame ); 

pIDecoderFrame->GetPixelFormat( &pixelFormat ); // GUID_WICPixelFormat24bppBGR

IWICBitmapSource * dst;
WICConvertBitmapSource( GUID_WICPixelFormat8bppGray, pIDecoderFrame, &dst );

4x3 图像示例,包含以下内容 BGR 像素值:

[  0,   0, 255,   0, 255,   0, 255,   0,   0;
   0, 255, 255, 255, 255,   0, 255,   0, 255;
   0,   0,   0, 119, 119, 119, 255, 255, 255;
 233, 178,  73, 233, 178,  73, 233, 178,  73]

我得到的灰色像素值:

[127, 220,  76;
 247, 230, 145;
   0, 119, 255;
 168, 168, 168]

我期望得到的灰色像素值 (ITU-R BT.601 conversion)

[ 76, 149,  29;
 225, 178, 105;
   0, 119, 255;
 152, 152, 152]

后台发生了什么样的转换,有没有办法强制转换为我想要的行为?

另外值得一提的是,Gray -> BGR 和 BGRA -> BGR 的转换工作正常(如预期)

【问题讨论】:

    标签: c++ windows image-processing imaging color-space


    【解决方案1】:

    至于“后台发生了什么样的转换”这个问题:似乎使用了不同的转换算法。使用 WINE 项目来计算灰度值,似乎给出了相同的结果,所以它给了我们一个很好的近似值。使用的公式是R * 0.2126 + G * 0.7152 + B * 0.0722

    copypixels_to_8bppGray(source):

    float gray = (bgr[2] * 0.2126f + bgr[1] * 0.7152f + bgr[0] * 0.0722f) / 255.0f;
    

    除此之外,它还针对 sRGB 颜色空间进行了校正。

    copypixels_to_8bppGray(source):

    gray = to_sRGB_component(gray) * 255.0f;
    

    to_sRGB_component(source):

    static inline float to_sRGB_component(float f)
    {
        if (f <= 0.0031308f) return 12.92f * f;
        return 1.055f * powf(f, 1.0f/2.4f) - 0.055f;
    }
    

    插入一些值:

        B    G    R    WINE          You're getting
          0    0  255  127.1021805   127
          0  255    0  219.932749    220
        255    0    0   75.96269736   76
        0    255  255  246.7295889   247
        255  255    0  229.4984163   230
        255    0  255  145.3857605   145
          0    0    0   12.92          0
    

    至于另一个问题,我对框架太陌生了,无法回答,所以我将把它留给其他人回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2015-02-16
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 2014-03-11
      相关资源
      最近更新 更多