将浮点数组保存到图像(使用 EXR 格式)
Save float array to image (with EXR format)
以下代码不起作用,它在将图像写入磁盘之前将值转换为 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
作为一点奖励,文档非常没有帮助
$ pydoc cv2.imwrite
Help on built-in function imwrite in cv2:
cv2.imwrite = imwrite(...)
imwrite(filename, img[, params]) -> retval
没有说参数应该是什么...
如何将具有浮点值的数组保存为 EXR 格式? (使用 OpenCV)
您可以尝试以下方法:
arr = cv2.imread("output.exr", cv2.IMREAD_UNCHANGED).astype(np.float32)
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
以下代码不起作用,它在将图像写入磁盘之前将值转换为 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
作为一点奖励,文档非常没有帮助
$ pydoc cv2.imwrite
Help on built-in function imwrite in cv2:
cv2.imwrite = imwrite(...)
imwrite(filename, img[, params]) -> retval
没有说参数应该是什么...
如何将具有浮点值的数组保存为 EXR 格式? (使用 OpenCV)
您可以尝试以下方法:
arr = cv2.imread("output.exr", cv2.IMREAD_UNCHANGED).astype(np.float32)
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