Reading .exr files in OpenCV

醉酒当歌 提交于 2021-01-04 04:18:30

问题


I have generated some depth maps using blender and have saved z-buffer values(32 bits) in OpenEXR format. Is there any way to access values from a .exr file (pixel by pixel depth info) using OpenCV 2.4.13 and python 2.7? There is no example anywhere to be found. All I can see in documentation that this file format is supported. But trying to read such a file results in error.

new=cv2.imread("D:\\Test1\\0001.exr")
cv2.imshow('exr',new)
print new[0,0]

Error:

print new[0,0]
TypeError: 'NoneType' object has no attribute '__getitem__'

and

cv2.imshow('exr',new)
cv2.error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow

Closest I found is this link and this link.


回答1:


I might be a little late to the party but; Yes you can definitely use OpenCV for that.

cv2.imread(PATH_TO_EXR_FILE,  cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)  

should get you what you need




回答2:


You can use OpenEXR package

pip --no-cache-dir install OpenEXR

To read exr file

def read_depth_exr_file(filepath: Path):
    exrfile = exr.InputFile(filepath.as_posix())
    raw_bytes = exrfile.channel('B', Imath.PixelType(Imath.PixelType.FLOAT))
    depth_vector = numpy.frombuffer(raw_bytes, dtype=numpy.float32)
    height = exrfile.header()['displayWindow'].max.y + 1 - exrfile.header()['displayWindow'].min.y
    width = exrfile.header()['displayWindow'].max.x + 1 - exrfile.header()['displayWindow'].min.x
    depth_map = numpy.reshape(depth_vector, (height, width))
    return depth_map


来源:https://stackoverflow.com/questions/44630793/reading-exr-files-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!