numpy 使用OpenImageIO python分离通道

kjthegm6  于 2023-08-05  发布在  Python
关注(0)|答案(1)|浏览(68)

我想从图像中渲染级别曲线,为此我只需要一个通道。当使用read_image时,它会生成RGBRGBRGB,这与matplotlib轮廓不兼容。从ImageInput的文档来看,我应该能够执行以下操作:

pixels = numpy.zeros((spec.nchannels, spec.height, spec.width), "uint8")
for channel in range(spec.nchannels) :
    pixels[channel] = file.read_image(0, 0, channel, channel + 1, "uint8")

字符串
由于我使用的是float,我的代码现在看起来像这样:

import OpenImageIO as oiio
import matplotlib.pyplot
import numpy

inp = oiio.ImageInput.open('test.exr')
if inp:
    spec = inp.spec()
    xres = spec.width
    yres = spec.height
    nchannels = spec.nchannels
    print(nchannels)
    pixels = numpy.zeros((spec.nchannels, spec.height, spec.width), 'float')
    for channel in range(spec.nchannels) :
        pixels[channel] = inp.read_image(0, 0, channel, channel + 1, 'float')
    inp.close()
    matplotlib.pyplot.contour(pixels[0], extent = [0, 49152, 0, 49252])
    matplotlib.pyplot.colorbar()
    matplotlib.pyplot.show()


但这个例子似乎有bug:

Traceback (most recent call last):
  File "/home/torbjorr/Dokument/terraformer/experiments/levelcurves.py", line 14, in <module>
    pixels[channel] = inp.read_image(0, 0, channel, channel + 1, 'float')
ValueError: could not broadcast input array from shape (1024,1024,1) into shape (1024,1024)


在我的例子中正确的形状是(1024,1024)

rkue9o1l

rkue9o1l1#

我找到解决办法了。似乎该文档中存在一个bug。下面是一个工作解决方案:

import OpenImageIO as oiio
import matplotlib.pyplot
import numpy

inp = oiio.ImageInput.open('test.exr')
if inp:
    spec = inp.spec()
    xres = spec.width
    yres = spec.height
    nchannels = spec.nchannels
    pixels = inp.read_image(0, 0, 0, nchannels, 'float')
    print(pixels[:, :, 0].ndim)
    inp.close()
    levels = numpy.linspace(0, 8192 + 512, 18)
    matplotlib.pyplot.contour(numpy.flipud(pixels[:, :, 0]), levels, extent = [0, 49152, 0, 49252], cmap='tab10')
    matplotlib.pyplot.colorbar()
    matplotlib.pyplot.show()

字符串

相关问题