1.使用cv2读取彩色图像和深度图像
2.深度图像转点云
3.只保留面部区域的点云
4.显示
import cv2
import numpy as np
import open3d
def depth2xyz(depth_map,depth_cam_matrix,flatten=False,depth_scale=1000):
fx,fy = depth_cam_matrix[0,0],depth_cam_matrix[1,1]
cx,cy = depth_cam_matrix[0,2],depth_cam_matrix[1,2]
h,w=np.mgrid[0:depth_map.shape[0],0:depth_map.shape[1]]
z=depth_map/depth_scale
x=(w-cx)*z/fx
y=(h-cy)*z/fy
xyz=np.dstack((x,y,z)) if flatten==False else np.dstack((x,y,z)).reshape(-1,3)
#xyz=cv2.rgbd.depthTo3d(depth_map,depth_cam_matrix)
return xyz
img = cv2.imread('./2021-01-26/whq_0_color.png')
depth = cv2.imread('./2021-01-26/whq_0_depth.png', -1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
colors = img.reshape(-1,3)
depth_cam_matrix = np.array([[900.584, 0, 636.849],
[0, 900.584, 370.579],
[0, 0, 0]])
points = depth2xyz(depth, depth_cam_matrix, True)
index = np.where(points[:, 0] > 0.15)
points = np.delete(points, index, axis=0)
colors = np.delete(colors, index, axis=0)
index = np.where(points[:, 0] < -0.15)
points = np.delete(points, index, axis=0)
colors = np.delete(colors, index, axis=0)
index = np.where(points[:, 1] > 0.1)
points = np.delete(points, index, axis=0)
colors = np.delete(colors, index, axis=0)
index = np.where(points[:, 1] < -0.2)
points = np.delete(points, index, axis=0)
colors = np.delete(colors, index, axis=0)
index = np.where(points[:, 2] > 0.7)
points = np.delete(points, index, axis=0)
colors = np.delete(colors, index, axis=0)
index = np.where(points[:, 2] < 0.1)
points = np.delete(points, index, axis=0)
colors = np.delete(colors, index, axis=0)
point_cloud = open3d.geometry.PointCloud()
point_cloud.points = open3d.utility.Vector3dVector(points)
point_cloud.colors = open3d.utility.Vector3dVector(colors/255)
# point_cloud 需要用中括号括住
open3d.draw_geometries([point_cloud])