一、pfm格式的读取
关于pfm格式,并未查到标准的定义或者解释,而在双目领域视差图的标签存储便是该格式。Scenceflow,Middlebury数据库中的视差图像就也是以pfm格式进行存储的。 PMF格式主要有两部分组成:头、元数据。
提供python读取pfm文件的代码:
def read_disp(filename):
# Scene Flow dataset
if filename.endswith('pfm'):
disp = np.ascontiguousarray(_read_pfm(filename)[0])
else:
raise Exception('Invalid disparity file format!')
return disp # [H, W]
def _read_pfm(file):
file = open(file, 'rb')
color = None
width = None
height = None
scale = None
endian = None
header = file.readline().rstrip()
if header.decode("ascii") == 'PF':
color = True
elif header.decode("ascii") == 'Pf':
color = False
else:
raise Exception('Not a PFM file.')
dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline().decode("ascii"))
if dim_match:
width, height = list(map(int, dim_match.groups()))
else:
raise Exception('Malformed PFM header.')
scale = float(file.readline().decode("ascii").rstrip())
if scale < 0: # little-endian
endian = '<'
scale = -scale
else:
endian = '>' # big-endian
data = np.fromfile(file, endian + 'f')
shape = (height, width, 3) if color else (height, width)
data = np.reshape(data, shape)
data = np.flipud(data)
return data, scale
二、npz格式的存储与读取
npz
文件是numpy
格式的压缩文件,在保存双目模型预测的视差结果时考虑采用npz的格式保存结果数据
2.1 npz格式文件创建与读取
np.savez()
保存npz格式
np.load()
读取npz格式
np.savez()
函数的第一个参数是文件名,其后的参数都是需要保存的数组。传递数组时可以使用关键字参数为数组命名,非关键字参数传递的数组会自动起名为arr_0、arr_1……
;np.savez()
函数输出的是一个扩展名为.npz的压缩文件,它包含多个与保存的数组对应的npy
文件,文件名对应数组名;读取.npz
文件时使用np.load()
函数,返回的是一个类似于字典的对象,因此可以通过数组名作为关键字对多个数组进行访问
import numpy as np
a = np.arange(5)
b = np.arange(6)
c = np.arange(7)
np.savez('test', a, b, arr_test = c) # 将多个数组保存到磁盘
# 读取数组
data = np.load('test.npz') #类似于字典{‘arr_0’:a,’arr_1’:b,’arr_2’:c}
print('arr_0 : ', data['arr_0'])
print('arr_1 : ', data['arr_1'])
print('arr_test: ', data['arr_test'])
--------------------------------------------------------------------------------
arr_0 : [0 1 2 3 4]
arr_1 : [0 1 2 3 4 5]
arr_test : [0 1 2 3 4 5 6]
另一案例
import numpy as np
def save_zpn(): # 保存.npz文件
a = np.zeros(10)
b = np.ones(5)
c = np.arange(3)
np.savez('my_npz_file.npz', zeros=a, ones=b, arange=c) # 这里可以指定文件名与保存的文件内容
def load_zpn(): # 读取.npz文件
my_npz = np.load('my_npz_file.npz')
print('npz文件保存的文件包括:', my_npz.files) # 打印全部文件名
print(my_npz['zeros']) # 拿到文件对应的内容
print(my_npz['ones'])
print(my_npz['arange'])
if __name__ == '__main__':
save_zpn()
load_zpn()
三、热力图的绘制
` 热力图的绘制主要用的是seaborn库中的sns函数
下面 附上一段我用的的代码
,输入分别是标签GT和预测结果,将两个结果做差以后采用 sns.heatmap()
函数完成热力图的绘制
def Error_Hot_Map(GT, img):
resImg = GT - img
plt.figure('1')
fig, ax1 = plt.subplots(figsize=(12.5, 7))
# 可以更换cmap的参数,调整热力图的风格
sns.heatmap(resImg, ax=ax1, vmin=-th, vmax=th, center=0, cmap='RdBu_r') # RdBu_r rainbow
ax1.set_title('error hot map')
ax1.set_xticklabels([]) # 设置x轴图例为空值
plt.xticks([])
plt.yticks([])
fig.savefig("G:/KT_136/Hot_Map.png", bbox_inches='tight')
plt.show()
plt.close()
print("")
效果图