繁体   English   中英

WICConvertBitmapSource BGR 到 Gray 的意外像素格式转换

[英]WICConvertBitmapSource BGR to Gray unexpected pixel format conversion

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

...

pIDecoder->GetFrame( 0, &pIDecoderFrame ); 

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

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

具有以下 BGR 像素值的 4x3 图像示例:

[  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 转换

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

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

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

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

copypixels_to_8bppGray来源):

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

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

copypixels_to_8bppGray来源):

gray = to_sRGB_component(gray) * 255.0f;

to_sRGB_component ( 来源):

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM