将浮点数组保存到图像(EXR 格式)

Ove*_*ivr 5 python opencv python-3.x opencv3.0

以下代码不起作用,它在将图像写入磁盘之前将值转换为 np.uint8。

import cv2
import numpy as np
# Generate dummy gradient with float values
arr = np.arange(0,10,0.02)
arr = np.repeat(arr, arr.shape[0])
arr.reshape((500,500))
cv2.imwrite('output.exr',arr)
# At this point, returns True. Opening the image with OpenEXR 1.4 shows values have become UINT8 instead of Float
arr = cv2.imread('output.exr')
# Shape is (1000, 1000, 3)
print(arr[10,10])
array([0, 0, 0], dtype=uint8) # All float data is lost
Run Code Online (Sandbox Code Playgroud)

作为一点奖励,文档非常没有帮助

$ pydoc cv2.imwrite
Help on built-in function imwrite in cv2:

cv2.imwrite = imwrite(...)
    imwrite(filename, img[, params]) -> retval
Run Code Online (Sandbox Code Playgroud)

没有说参数应该是什么......

如何将带有浮点值的数组保存为 EXR 格式?(使用 OpenCV)

Ove*_*ivr 5

imageio可以保存任何数据格式的 EXR 文件,(并且它还支持大量其他图像格式)。

读取和写入大多数图像几乎可以挽救生命:

import numpy as np
import imageio

# Generate dummy random image with float values
arr = np.random.uniform(0.0, 1.0, size=(500,500))

# freeimage lib only supports float32 not float64 arrays
arr = arr.astype("float32")

# Write to disk
imageio.imwrite('float_img.exr', arr)

# Read created exr from disk
img = imageio.imread('float_img.exr')

assert img.dtype == np.float32
Run Code Online (Sandbox Code Playgroud)