Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在YCbCr模式下正确读取图像?_Python_Image Processing_Computer Vision_Rgb_Ycbcr - Fatal编程技术网

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

Python 如何在YCbCr模式下正确读取图像?,python,image-processing,computer-vision,rgb,ycbcr,Python,Image Processing,Computer Vision,Rgb,Ycbcr,如何知道我是否在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[:

如何知道我是否在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
不受欢迎。但是,它使用
Image.convert
from
PIL
转换模式

详情:

我更改了
convert\u rgb\u to\u ycbcr(img)
函数,它给出了相同的结果

使用的实施:


谢谢你的邀请。有没有提到这种计算方法?另外还有两个问题:既然scipy.misc.imread现在已经贬值了,那么读取图像的最佳方式是什么?最后还有一个imread的展平参数,它假设将3个通道展平为1,这是如何实现的?我不确定,您如何定义最佳?您可以使用opencv
imread
,枕头
open
。你的意思是使用
scipy
?对于展平,这可能很有用:
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]) 
[165. 165. 165. 166. 167.]
[165 165 165 166 167]