import scipy.io as scio
import open3d as o3d
import numpy as np
import cv2
import matplotlib
from matplotlib import pyplot as plt
# read mat file
data = scio.loadmat("D:\xx\A2J\ITOP-Dataset\itop_side_test/1.mat")
depth_map = data['DepthNormal'][:,:,3]
image = np.zeros(depth_map.shape, dtype = np.uint8)for r in range(depth_map.shape[0]):
for c in range(depth_map.shape[1]):
if depth_map[r,c]< 6.0: # max depth value is 6.0
image[r,c]= depth_map[r,c]/6.0 * 255
# Display image
plt.figure("test", figsize=(8,8))# 创建一个名为astronaut的窗口,并设置大小
plt.subplot(2,2,1)
plt.title("U8")
plt.imshow(image)
plt.subplot(2,2,2)
plt.title("Float")
plt.imshow(depth_map)
plt.subplot(2,2,3)
plt.title("U8-gray")
plt.imshow(image, plt.cm.gray)
plt.subplot(2,2,4)
plt.title("Float-gray")
plt.imshow(depth_map, plt.cm.gray)
plt.show()## save image
plt.imsave('plt-u8.png', image)
plt.imsave('plt-float.png',depth_map)
显示结果
2.2 显示深度图像 (cv2)
保存Numpy数组为图像
保存之前,归一化为8位整数
生成8位灰度数据
data = scio.loadmat("D:\xx\A2J\ITOP-Dataset\itop_side_test/1.mat")
depth_map = data['DepthNormal'][:,:,3]
image = np.zeros(depth_map.shape, dtype = np.uint8)for r in range(depth_map.shape[0]):
for c in range(depth_map.shape[1]):
if depth_map[r,c]< 6.0: # max depth value is 6.0
image[r,c]= depth_map[r,c]/6.0 * 255
cv2.imwrite("opencv_gray.png", image)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(image, alpha=1.0), cv2.COLORMAP_JET)
cv2.imwrite("opencv_color.png", depth_colormap)# Stack multiple images horizontal
depth_image = np.zeros((depth_map.shape[0],depth_map.shape[1],3), dtype = np.uint8)
depth_image[:,:,0] = image[:,:]
depth_image[:,:,1] = image[:,:]
depth_image[:,:,2] = image[:,:]
images = np.hstack((depth_image, depth_colormap))# hstack image must have the same shape
cv2.imshow('images', images)
cv2.waitKey()
cv2.destroyAllWindows()