前面两点主要为深度图exr的处理,包括和png格式的转化
后面是比较常见的通用的读取方式
OpenEXR的安装是有点麻烦的
这里的深度图是三通道,每个通道都一样
def exr2tiff(exrpath):
File = OpenEXR.InputFile(exrpath)
PixType = Imath.PixelType(Imath.PixelType.FLOAT)
DW = File.header()['dataWindow']
Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1)
rgb = [np.frombuffer(File.channel(c, PixType), dtype=np.float32) for c in 'RGB']
r =np.reshape(rgb[0], (Size[1],Size[0]))
mytiff = np.zeros((Size[1], Size[0]), dtype=np.float32)
mytiff = r
return mytiff
import imageio
depth = imageio.imread(depthfile)
这里用到png包,将深度图归一化到一定的范围
下面的normalizationDepth为保存函数。
def saveUint16(z, path):
# Use pypng to write zgray as a grayscale PNG.
with open(path, 'wb') as f:
writer = png.Writer(width=z.shape[1], height=z.shape[0], bitdepth=16, greyscale=True)
zgray2list = z.tolist()
writer.write(f, zgray2list)
def depthToint16(dMap, minVal=0, maxVal=10):
#Maximum and minimum distance of interception
dMap[dMap>maxVal] = maxVal
# print(np.max(dMap),np.min(dMap))
dMap = ((dMap-minVal)*(pow(2,16)-1)/(maxVal-minVal)).astype(np.uint16)
return dMap
def normalizationDepth(depthfile, savepath):
correctDepth = readDepth(depthfile)
depth = depthToint16(correctDepth, 0, 10)
saveUint16(depth,savepath)
对应的读取方式为:
def loadDepth(dFile, minVal=0, maxVal=10):
dMap = scipy.misc.imread(dFile)
dMap = dMap.astype(np.float32)
dMap = dMap*(maxVal-minVal)/(pow(2,16)-1) + minVal
return dMap
除了下面几种,还有其他依赖包也很好用,不过这次主要用到这几种
import matplotlib.image as mpimg
import skimage
import imageio
import scipy.misc
# 下面读出来的是浮点数,有时候就是代表深度信息了
lena = mpimg.imread(imagepath)
# 下面读出来的是整数,可以理解为归一化处理了
lena = skimage.io.imread(imagepath)
lena = scipy.misc.imread(imagepath)
lena = imageio.imread(imagepath)
# 通过skimage.img_as_float可以转为浮点数
lena = skimage.img_as_float(lena)