如何知道我是否在YCbCr模式下正确读取了PNG图像?我得到了不同的像素值,这让人困惑
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,因为scipy已经为我处理了转换,但我无法找到它的源代码。我自己定义了转换函数,但得到的像素值不同
在最新的scipy版本中,
imread
被弃用。但是,它使用来自PIL
的Image.convert
来转换模式详情:
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://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdprfx/b550d1b5-f7d9-4a0c-9141-b3dca9d7f525?redirectedfrom=MSDN
Conversion formula from RGB to YCbCr
相关问题 更多 >
编程相关推荐