我使用的公式来自这个问题 https://stackoverflow.com/questions/8838481/kcvpixelformattype-420ypcbcr8biplanarfullrange-frame-to-uiimage-conversion:
uint8_t *rgbBuffer = malloc(imageWidth * imageHeight * 3);
// .. iterate over height and width
// from ITU-R BT.601, rounded to integers
rgbOutput[0] = (298 * (y - 16) + 409 * cr - 223) >> 8;
rgbOutput[1] = (298 * (y - 16) + 100 * cb + 208 * cr + 136) >> 8;
rgbOutput[2] = (298 * (y - 16) + 516 * cb - 277) >> 8;
我认为这是基于wiki 文章中的 ITU-R_BT.601 公式 http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion.
但是我认为这个公式不太正确,因为输出图像如下所示:
如何修正公式?
假设第一次计算的最大值(y == 255
and cr == 255
):
rgbOutput[0] = (298 * (255 - 16) + 409 * 255 - 223) >> 8;
rgbOutput[0] = (298 * 239 + 104295 - 223) >> 8;
rgbOutput[0] = (71222 + 104295 - 223) >> 8;
rgbOutput[0] = 175294 >> 8; // 175294 == 0x2ACBE
rgbOutput[0] = 684; // 684 == 0x2AC
的最大值rgbOutput[0]
能容纳的是255
。您正在尝试分配684
到它,导致截断。分配给它的实际值是172
(0xAC
).
EDIT 1
根据您发布的公式,您的第一个计算应该如下:
rgbOutput[0] = ((298 * y) >> 8) + ((409 * cr) >> 8) - 223;
这会产生一个值(假设最大值为y
and cr
) of 480
,这也会导致截断。
EDIT 2
据说推荐使用以下等式:
使用这个,你的第一个计算应该是这样的:
rgbOutput[0] = ((255 * (y - 16)) / 219) + ((179 * (cr - 128)) / 112;
这会产生一个值(假设最大值为y
and cr
) of 480
(编辑 1 中的答案相同),这也会导致截断。
EDIT 3
有关完整的解决方案,请参阅@Robert 的回答。
EDIT 4
When y == 0
and cr == 0
,写入的值y
除非执行钳位,否则也会导致截断。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)