ios - 如何修复此 YCrCb -> RBG 转换公式?

标签 ios c image image-processing colors

我使用的公式来自 this question :

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;

我假设是基于 ITU-R_BT.601 formula in the wiki article .

enter image description here

但是我认为公式不太正确,因为输出图像看起来像这样:

I don't actually have green hair

如何修改公式?

最佳答案

假设第一次计算的最大值(y == 255cr == 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)。

编辑 1

根据您发布的公式,您的第一次计算应如下所示:

rgbOutput[0] = ((298 * y) >> 8) + ((409 * cr) >> 8) - 223;

这会导致 480 的值(假设 ycr 的最大值),这也会导致截断。

编辑 2

据说建议使用以下等式:

equation

改用它,您的第一个计算应该是这样的:

rgbOutput[0] = ((255 * (y - 16)) / 219) + ((179 * (cr - 128)) / 112;

这导致 480 的值(假设 ycr 的最大值)(编辑 1 中的相同答案),这也会导致截断。

编辑 3

请参阅@Robert 的回答以获得完整的解决方案。

编辑 4

y == 0cr == 0 时,写入 y 的值也将导致截断,除非钳位是执行。

关于ios - 如何修复此 YCrCb -> RBG 转换公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25210852/

相关文章:

ios - -canOpenURL : failed for URL: "fbauth2:/" - error: "(null)"

iphone - 为什么Objective C中的以下代码无法模拟 'message sent to deallocated instance '

ios - 从 iOS 中的 UIActivityController 选项中删除注释

c - 使用文件处理在 C 中加密消息

ios - 加载图像时,UITableView 中的滚动非常不稳定

ios - Xcode 4.2 每次停止模拟器后都会跳转到 main.m

c++ - c 套接字以字符串形式发送命令

c - 获取窗口的当前尺寸,无论最小/正常/最大化状态如何

android - 如何排列动态图像?

python - 在Python中调整图像大小实际上并没有调整大小