首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在YCbCr模式下正确读取图像?

如何在YCbCr模式下正确读取图像?
EN

Stack Overflow用户
提问于 2020-05-03 03:45:47
回答 1查看 1.5K关注 0票数 0

如何知道是否正确读取了YCbCr模式下的PNG图像?我得到不同的像素值,这是令人困惑的。

代码语言:javascript
复制
def convert_rgb_to_ycbcr(img):
    y = 16. + (64.738 * img[:, :, 0] + 129.057 * img[:, :, 1] + 25.064 * img[:, :, 2]) / 255.
    cb = 128. + (-37.945 * img[:, :, 0] - 74.494 * img[:, :, 1] + 112.439 * img[:, :, 2]) / 255.
    cr = 128. + (112.439 * img[:, :, 0] - 94.154 * img[:, :, 1] - 18.285 * img[:, :, 2]) / 255.
    return np.array([y, cb, cr]).transpose([1, 2, 0])


# method 1 - read as YCbCr directly
img = scipy.misc.imread(path, mode='YCbCr').astype(np.float)
print(img[0, :5, 0]) 
# returns [32. 45. 68. 78. 92.]

# method 2 - read as RGB and convert RGB to YCbCr
img = scipy.misc.imread(path, mode='RGB').astype(np.float)
img = convert_rgb_to_ycbcr(img)
print(img[0, :5, 0]) 
# returns[44.0082902  55.04281961 75.1105098  83.57022745 95.44837255]

我想使用方法1,因为code已经为我处理了转换,但是我找不到它的源代码。所以我自己定义了转换函数,但是得到了不同的像素值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-03 04:27:13

在最新的版本中,不推荐使用imread。然而,它使用Image.convertPIL转换模式。

详细信息:

https://pillow.readthedocs.io/en/3.1.x/reference/Image.html?highlight=convert#PIL.Image.Image.convert

https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes

https://github.com/scipy/scipy/blob/v0.18.0/scipy/misc/pilutil.py#L103-L155

我更改了您的convert_rgb_to_ycbcr(img)函数,它给出了相同的结果。

使用的实现:https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdprfx/b550d1b5-f7d9-4a0c-9141-b3dca9d7f525?redirectedfrom=MSDN

Conversion formula from RGB to YCbCr

代码语言:javascript
复制
import scipy.misc # scipy 1.1.0
import numpy as np

def convert_rgb_to_ycbcr(im):
    xform = np.array([[.299, .587, .114], [-.1687, -.3313, .5], [.5, -.4187, -.0813]])
    ycbcr = im.dot(xform.T)
    ycbcr[:,:,[1,2]] += 128
    return np.uint8(ycbcr)


# method 1 - read as YCbCr directly
img = scipy.misc.imread('test.jpg', mode='YCbCr').astype(np.float)
print(img[0, :5, 0]) 
# returns [32. 45. 68. 78. 92.]

# method 2 - read as RGB and convert RGB to YCbCr
img = scipy.misc.imread('test.jpg', mode='RGB').astype(np.float)
img = convert_rgb_to_ycbcr(img)
print(img[0, :5, 0]) 
代码语言:javascript
复制
[165. 165. 165. 166. 167.]
[165 165 165 166 167]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61569397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档