【发布时间】:2018-10-23 18:28:52
【问题描述】:
我正在使用 OpenEXR 在 Python 中读取 EXR 文件。我有带有半数据(float16)的 R、G 和 B 通道。我尝试使用 Numpy 将数据从 float16 转换为 uint8(0-255 色),但未成功。
rCh = getChanEXR(imageFile, 'R','HALF')
rCh = np.array(rCh).astype('uint8')
因此,我将 R 通道像素值放入变量 rCh。然后我将 array.array 转换为 np.array,以便我可以使用 astype 方法将其转换为 uint8。我是新手,所以我显然不正确,因为所有值都变为 0。最初,这些值是这样的:0.0、2.9567511226945634e-14、1.2295237050707897e-10 等。
除了 float16 值之外,我还有一些常规的浮点值需要标准化。我想我需要标准化 float16 值,然后才能将它们设置在 0-255 的范围内。
有什么想法吗?谢谢。
添加这里提到的 def 的代码 getChanEXR(只是基于 python OpenEXR 文档中用于获取频道数据的代码的自定义 def。
def getChanEXR(curEXRStr, curChannel, dataType):
#import OpenEXR, Imath, array
pt = 'none'
if dataType == 'HALF':
pt = Imath.PixelType(Imath.PixelType.HALF)
if dataType == 'FLOAT':
pt = Imath.PixelType(Imath.PixelType.FLOAT)
if dataType == 'UINT':
pt = Imath.PixelType(Imath.PixelType.UINT)
chanstr = OpenEXR.InputFile(curEXRStr).channel(curChannel, pt)
chan = array.array('f', chanstr)
return chan
【问题讨论】:
-
感谢菲利波的回复。我认为您的答案是正确的,但是有一步使我无法做到。数据是 array.array 的形式,所以当我使用 min 或 max 时,它告诉我它不能与 array.array 一起使用。如果我使用 np.asarray 来转换它,所有的值都会变成 0。
-
你应该规范化数据之前将其转换为
np.uint8,你可以使用标准pythonmin()和max()与array.array或者你可以将它转换为一个 numpy 浮点数组,对其进行规范化,然后转换为 8 位 -
什么是
getChanEXR?它不会出现在 google search foropenexr getchanexr或 OpenEXR documentation search forgetChanEXR中。 -
getChanEXR 只是我根据文档中的 openEXR python 代码创建的定义。这就是我获取频道数据的方式。
def getChanEXR(curEXRStr, curChannel, dataType): #import OpenEXR, Imath, array pt = 'none' if dataType == 'HALF': pt = Imath.PixelType(Imath.PixelType.HALF) if dataType == 'FLOAT': pt = Imath.PixelType(Imath.PixelType.FLOAT) if dataType == 'UINT': pt = Imath.PixelType(Imath.PixelType.UINT) chanstr = OpenEXR.InputFile(curEXRStr).channel(curChannel, pt) chan = array.array('f', chanstr) return chan
标签: python numpy data-conversion openexr