Astra Pro点云代码

背景

当前处于戴眼镜与口罩识别头部位姿分支
在这里插入图片描述

github上找到的python读取点云的代码

import time

import cv2 as cv
import numpy as np
import open3d
from openni import _openni2
from openni import openni2

SAVE_POINTCLOUDS = True  # 是否保存点云数据


def get_rgbd(color_capture, depth_stream, depth_scale=1000, depth_trunc=4, convert_rgb_to_intensity=False):
    # 获取彩色图像
    _, color_image = color_capture.read()
    color_image = color_image[:, ::-1, ::-1]

    # 获取深度图像
    depth_frame = depth_stream.read_frame()
    depth_image = np.frombuffer(depth_frame.get_buffer_as_uint16(), np.uint16)
    depth_image = depth_image.reshape(depth_frame.height, depth_frame.width)
    depth_image = depth_image.astype(np.float32)

    # 从深度和彩色图像创建RGBD图像
    color_image = np.ascontiguousarray(color_image)
    depth_image = np.ascontiguousarray(depth_image)
    rgbd = open3d.geometry.RGBDImage.create_from_color_and_depth(
        open3d.geometry.Image(color_image),
        open3d.geometry.Image(depth_image),
        depth_scale=depth_scale,
        depth_trunc=depth_trunc,
        convert_rgb_to_intensity=convert_rgb_to_intensity
    )

    return rgbd


def main():
    # 初始化OpenNI
    openni2.initialize()

    # 打开Astra彩色流(使用OpenCV)
    color_capture = cv.VideoCapture(1)
    color_capture.set(cv.CAP_PROP_FPS, 5)

    # 打开Astra深度流(使用OpenNI)
    depth_device = openni2.Device.open_any()
    depth_stream = depth_device.create_depth_stream()
    depth_stream.start()
    depth_stream.set_video_mode(
        _openni2.OniVideoMode(pixelFormat=_openni2.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_1_MM,
                              resolutionX=640,
                              resolutionY=480,
                              fps=5))
    depth_device.set_image_registration_mode(openni2.IMAGE_REGISTRATION_DEPTH_TO_COLOR)

    # 创建点云可视化器
    visualizer = open3d.visualization.Visualizer()
    visualizer.create_window("Pointcloud", width=1000, height=700)

    # Astra Pro相机的相机内参
    astra_camera_intrinsics = open3d.camera.PinholeCameraIntrinsic(
        width=640,
        height=480,
        fx=585,
        fy=585,
        cx=320,
        cy=250)

    # 创建初始点云
    pointcloud = open3d.geometry.PointCloud()
    visualizer.add_geometry(pointcloud)

    first = True
    prev_timestamp = 0
    num_stored = 0

    while True:
        rgbd = get_rgbd(color_capture, depth_stream)

        # 将图像转换为点云
        new_pointcloud = open3d.geometry.PointCloud.create_from_rgbd_image(
            rgbd,
            intrinsic=astra_camera_intrinsics,
        )
        # 翻转点云
        new_pointcloud.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
        # 设置渲染的点云为记录的点云
        pointcloud.points = new_pointcloud.points
        pointcloud.colors = new_pointcloud.colors

        # 每n秒保存一次点云
        if SAVE_POINTCLOUDS and time.time() > prev_timestamp + 5:
            filename = "pointcloud-%r.pcd" % num_stored
            open3d.io.write_point_cloud(filename, new_pointcloud)
            num_stored += 1
            print("已保存: %s" % filename)
            prev_timestamp = time.time()

        # 在第一帧中重置视点,以正确查看场景
        if first:
            visualizer.reset_view_point(True)
            first = False

        # 更新可视化器
        visualizer.update_geometry(pointcloud)
        visualizer.poll_events()
        visualizer.update_renderer()


if __name__ == "__main__":
    main()

在这里插入图片描述

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值