在OpenCV中阅读.exr文件

vbopmzt1  于 2022-11-15  发布在  其他
关注(0)|答案(3)|浏览(264)

我已经使用blender生成了一些深度图,并以OpenEXR格式保存了z缓冲区值(32位)。有没有办法使用OpenCV 2.4.13和python 2.7从.exr文件(逐像素深度信息)中访问值?在任何地方都找不到示例。我只能在文档中看到支持这种文件格式。但试图读取这样的文件会导致错误。

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

错误:

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

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

我找到的最接近的是这个link和这个link

a14dhokn

a14dhokn1#

我可能会晚一点去参加聚会,但是;是的,您完全可以使用OpenCV。

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

应该能满足你的需要

t9aqgxwy

t9aqgxwy2#

完整解决方案

@Iwohlhart的解决方案为我抛出了一个错误,

# first import os and enable the necessary flags to avoid cv2 errors

import os
os.environ["OPENCV_IO_ENABLE_OPENEXR"]="1"
import cv2

# then just type in following

img = cv2.imread(PATH2EXR, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)
'''
you might have to disable following flags, if you are reading a semantic map/label then because it will convert it into binary map so check both lines and see what you need
''' 
# img = cv2.imread(PATH2EXR) 
 
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
3htmauhk

3htmauhk3#

您可以使用OpenEXR软件包

pip --no-cache-dir install OpenEXR

如果上述操作失败,请安装OpenEXR dev库,然后按上述方法安装python包

sudo apt-get install openexr
sudo apt-get install libopenexr-dev

如果未安装gcc

sudo apt-get install gcc
sudo apt-get install g++

读取exr文件

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

相关问题