【问题标题】:PIL Image Convert from RGB to YCbCr Results in 4 Channels Instead of 3 and Behaves Like RGBPIL 图像从 RGB 转换为 YCbCr 产生 4 个通道而不是 3 个通道,并且表现得像 RGB
【发布时间】:2014-07-07 12:35:50
【问题描述】:

嗯,标题很容易解释。我有一个图像文件,我想分别分成 Y、Cb 和 Cr。打开文件后,将其从RGB(打开图像文件时的默认模式)转换为YCbCr,然后使用numpy.array()将其转换为数组,结果是一个4通道的二维数组,这不是根据http://www.nmt.edu/tcc/help/pubs/pil/pil.pdf中的文档,我期望

这是我在解释器中所做的:

ImageFile = Image.open('filePath', 'r')
ImageFile = ImageFile.convert('YCbCr')
ImageFileYCbCr = numpy.array(ImageFile)
ImageFileYCbCr

导致

array([[[103, 140, 133,  95],
    [140, 133,  91, 141],
    [132,  88, 141, 131],
    ..., 
    [129,  65, 146, 129],
    [ 64, 146, 130,  65],
    [146, 129,  64, 147]],

   [[129,  64, 147, 129],
    [ 62, 149, 130,  62],
    [149, 130,  62, 149],
    ..., 

当我把它分成它的通道时

ImageFileY = copy.deepcopy(ImageFileYCbCr) # to make a separate copy as array is immutable
ImageFileY[:,:,1] *= 0
ImageFileY[:,:,2] *= 0
ImageFileY[:,:,3] *= 0
ImageFileYOnly = Image.fromarray(ImageFileY)
ImageFileYOnly.show()

它产生了一个红色通道,就好像它是一个 RGB。请问可以分别得到Y、Cb、Cr的值吗?

编辑:Numpy 1.3 版,Python 2.6 Linux Backtrack 5

【问题讨论】:

  • "... 根据http://www.google.com/url?sa=t&rct=j..." 中的文档,这不是我所期望的。该链接将我带到一个空白页面。查看url= 参数,我猜你的意思是链接到this pdf
  • 看看这里:stackoverflow.com/questions/2797102/… 这可能会有所帮助
  • 哦,你说得对,凯文。我会编辑它。谢谢。
  • 如果我在图像上调用getbands(),它会返回('Y', 'Cb', 'Cr'),而getpixel((0,0)) 返回一个包含3 个成员的元组,表示3 个波段。错误必须在转换为numpy
  • 有什么建议吗,马克?

标签: python python-imaging-library


【解决方案1】:

https://mail.python.org/pipermail/image-sig/2010-October/006526.html

这是 Numpy 的一个老错误。纠正它

>>> import numpy
>>> import Image as im
>>> image = im.open('bush640x360.png')
>>> ycbcr = image.convert('YCbCr')

>>> B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1', ycbcr.tostring())
>>> print B.shape
(360, 640, 3)
>>> im.fromarray(B[:,:,0], "L").show()

【讨论】:

  • 在 PIL 的更高版本(1.1.7,可能更早)中,tostring() 已被删除,因此第 5 行应为:B = numpy.ndarray((image.size[1], image.size[0], 3), 'u1', ycbcr.tobytes())
【解决方案2】:

仅供参考,未来来自 Google 的人:

现在看来可以了。

作为参考,我有 Pillow 6.1.0 和 numpy 1.17.0。正在做

img = np.array(Image.open(p).convert('YCbCr'))

给出相同的

img = Image.open(p).convert('YCbCr')
img = np.ndarray((img.size[1], img.size[0], 3), 'u1', img.tobytes())

并且与RGB不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多