- python程序:
运行时先开启摄像头,键盘单按“a”时,可以保存当前点云。
保存了.ply格式内容,且转换为.pcd格式并保存
.ply文件可以通过meshlab进行查看。
import pyrealsense2 as rs
import numpy as np
import cv2
import open3d as o3d
import copy
import time
from helpers import *
import os
VISUALIZE = True
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipeline.start(config)
i=1
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
depth_intrin = depth_frame.profile.as_video_stream_profile().intrinsics
profile = frames.get_profile()
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
#480*640
color_image = np.asanyarray(color_frame.get_data())
key = cv2.waitKey(1)
if key == ord("a"):
print("type of depth_image:",type(depth_image))
print("shape of depth_image:",depth_image.shape)
o3d_color = o3d.geometry.Image(color_image)
o3d_depth = o3d.geometry.Image(depth_image)
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(o3d_color, o3d_depth,
depth_scale=1000.0,
depth_trunc=3.0,
convert_rgb_to_intensity=False)
intrinsics = profile.as_video_stream_profile().get_intrinsics()
# 转换为open3d中的相机参数
pinhole_camera_intrinsic = o3d.camera.PinholeCameraIntrinsic(
intrinsics.width, intrinsics.height,
intrinsics.fx, intrinsics.fy,
intrinsics.ppx, intrinsics.ppy
)
o3d_result = o3d.geometry.PointCloud.create_from_rgbd_image(
rgbd_image,
pinhole_camera_intrinsic
)
o3d.io.write_point_cloud("/home/lt/Downloads/instance-segmentation-master/multiple_camera/1.ply", o3d_result)
#ply to pcd
mesh_ply = o3d.io.read_triangle_mesh("/home/lt/Downloads/instance-segmentation-master/multiple_camera/1.ply")
mesh_ply.compute_vertex_normals()
# V_mesh 为ply网格的顶点坐标序列,shape=(n,3),这里n为此网格的顶点总数,其实就是浮点型的x,y,z三个浮点值组成的三维坐标
V_mesh = np.asarray(mesh_ply.vertices)
print("ply info:", mesh_ply)
print("ply vertices shape:", V_mesh.shape)
# o3d.visualization.draw_geometries([mesh_ply], window_name="ply", mesh_show_wireframe=True)
# ply -> stl
mesh_stl = o3d.geometry.TriangleMesh()
mesh_stl.vertices = o3d.utility.Vector3dVector(V_mesh)
mesh_stl.compute_vertex_normals()
# print("stl info:", mesh_stl)
# o3d.visualization.draw_geometries([mesh_stl], window_name="stl")
o3d.io.write_triangle_mesh("/home/lt/Downloads/instance-segmentation-master/multiple_camera/1.stl", mesh_stl)
# stl/ply -> pcd
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(V_mesh)
print("pcd info:", pcd)
print("type of pcd:", type(pcd))
# print("shape of pcd:", pcd.shape)
o3d.visualization.draw_geometries([pcd], window_name="pcd")
# save pcd
o3d.io.write_point_cloud("/home/lt/Downloads/instance-segmentation-master/multiple_camera/1.pcd", pcd)
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()
-
.ply文件:
PLY文件格式是Stanford大学开发的一套三维mesh模型数据格式,不同于三维引擎中常用的场景图文件格式和脚本文件,每个PLY文件只用于描述一个多边形模型对象(Object),该模型对象可以通过诸如顶点、面等数据进行描述,每一类这样的数据被称作一种元素(Element)。相比于现代的三维引擎中所用到的各种复杂格式,PLY实在是种简单的不能再简单的文件格式。 -
.pcd文件:
https://blog.csdn.net/qq_43049432/article/details/99288502?spm=1001.2101.3001.6650.17&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7Eessearch%7Evector-17.essearch_pc_relevant&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7Eessearch%7Evector-17.essearch_pc_relevant