最近一直在和.exr格式的图片文件打交道,相信应该也有很多人和我一样读取报错。

import cv2	
sim_depth_path = '.dataset/0000.exr'
img = cv2.imread(sim_depth_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
cv2.imshow('show_exr', np.transpose(img2, (1, 2, 0)))
cv2.waitKey(0)
cv2.destroyAllWindows()

报错:
在这里插入图片描述这里讲一下两种读取.exr格式的图片:
(1)使用opencv
加一行环境设置 os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"

import cv2	
import os
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
sim_depth_path = '.dataset/0000.exr'
img = cv2.imread(sim_depth_path, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
cv2.imshow('show_exr', np.transpose(img2, (1, 2, 0)))
cv2.waitKey(0)
cv2.destroyAllWindows()

(2)使用OpenEXR库

def exr_loader(EXR_PATH, ndim=3):
    exr_file = OpenEXR.InputFile(EXR_PATH)
    cm_dw = exr_file.header()['dataWindow']
    size = (cm_dw.max.x - cm_dw.min.x + 1, cm_dw.max.y - cm_dw.min.y + 1)

    pt = Imath.PixelType(Imath.PixelType.FLOAT)

    if ndim == 3:
        # read channels indivudally
        allchannels = []
        for c in ['R', 'G', 'B']:
            # transform data to numpy
            channel = np.frombuffer(exr_file.channel(c, pt), dtype=np.float32)
            channel.shape = (size[1], size[0])
            allchannels.append(channel)

        # create array and transpose dimensions to match tensor style
        exr_arr = np.array(allchannels).transpose((0, 1, 2))
        return exr_arr

    if ndim == 1:
        # transform data to numpy
        channel = np.frombuffer(exr_file.channel('R', pt), dtype=np.float32)
        channel.shape = (size[1], size[0])  # Numpy arrays are (row, col)
        exr_arr = np.array(channel)
        return exr_arr

(3)使用imageio库

import imageio
import os
from PIL import Image
os.environ["OPENCV_IO_ENABLE_OPENEXR"] = "1"
img_path = r'./000000000-cameraNormals.exr'
image = imageio.imread(img_path, 'exr')
output = Image.fromarray(image)
output.show()

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐