簡體   English   中英

讀取 OpenCV 中的.exr 文件

[英]Reading .exr files in OpenCV

我使用攪拌器生成了一些深度圖,並以 OpenEXR 格式保存了 z 緩沖區值(32 位)。 有什么方法可以使用 OpenCV 2.4.13 和 python 2.7 訪問 a.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

我發現最近的是這個鏈接和這個鏈接

我參加聚會可能有點晚了,但是; 是的,您絕對可以為此使用 OpenCV。

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

應該給你你需要的東西

您可以使用OpenEXR

pip --no-cache-dir install OpenEXR

如果以上失敗,安裝OpenEXR dev庫,然后安裝python包如上

sudo apt-get install libopenexr-dev

讀取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

完整的解決方案

@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)
'''
you can also enable following flags but if you are reading a semantic map/label then it might convert it into binary map so check both lines and see what you need
''' 
# img = cv2.imread(PATH2EXR, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH )  
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM