将float数组保存到图像(使用EXR格式)

时间:2017-08-03 11:00:36

标签: python python-3.x opencv opencv3.0

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

close all connections
${index}=  Open Connection  <<My IP Address here>>
log to console  ${index} -- Index will return as 1
${ret_val}=  get Connection  1
log to console  ${ret_val} -- the connection status is written 
${ret_val1}=  Start command  sudo service arm-mbed-ds stop - Executing this command says "Connection not Open"

作为一点奖励,文档非常无用

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

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

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

2 个答案:

答案 0 :(得分:1)

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

用于读取和写入大多数图像的救生器:

import numpy as np
import imageio

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

imageio.imwrite('float_img.exr', arr)

# To load the created exr file

img = imageio.imread('float_img.exr')

assert img.dtype == np.float32

答案 1 :(得分:0)

您可以尝试以下方法: arr = cv2.imread("output.exr", cv2.IMREAD_UNCHANGED).astype(np.float32)