Realsense D435使用Python获取相机坐标系下某点三维坐标

# -*- coding: utf-8 -*-
import pyrealsense2 as rs
import numpy as np
import cv2

''' 
设置
'''
pipeline = rs.pipeline()  # 定义流程pipeline,创建一个管道
config = rs.config()  # 定义配置config
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 15)  # 配置depth流
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 15)  # 配置color流

pipe_profile = pipeline.start(config)  # streaming流开始

# 创建对齐对象与color流对齐
align_to = rs.stream.color  # align_to 是计划对齐深度帧的流类型
align = rs.align(align_to)  # rs.align 执行深度帧与其他帧的对齐

''' 
获取对齐图像帧与相机参数
'''


def get_aligned_images():
    frames = pipeline.wait_for_frames()  # 等待获取图像帧,获取颜色和深度的框架集
    aligned_frames = align.process(frames)  # 获取对齐帧,将深度框与颜色框对齐

    aligned_depth_frame = aligned_frames.get_depth_frame()  # 获取对齐帧中的的depth帧
    aligned_color_frame = aligned_frames.get_color_frame()  # 获取对齐帧中的的color帧

    #### 获取相机参数 ####
    depth_intrin = aligned_depth_frame.profile.as_video_stream_profile().intrinsics  # 获取深度参数(像素坐标系转相机坐标系会用到)
    color_intrin = aligned_color_frame.profile.as_video_stream_profile().intrinsics  # 获取相机内参

    #### 将images转为numpy arrays ####
    img_color = np.asanyarray(aligned_color_frame.get_data())  # RGB图
    img_depth = np.asanyarray(aligned_depth_frame.get_data())  # 深度图(默认16位)

    return color_intrin, depth_intrin, img_color, img_depth, aligned_depth_frame


''' 
获取随机点三维坐标
'''


def get_3d_camera_coordinate(depth_pixel, aligned_depth_frame, depth_intrin):
    x = depth_pixel[0]
    y = depth_pixel[1]
    dis = aligned_depth_frame.get_distance(x, y)  # 获取该像素点对应的深度
    # print ('depth: ',dis)       # 深度单位是m
    camera_coordinate = rs.rs2_deproject_pixel_to_point(depth_intrin, depth_pixel, dis)
    # print ('camera_coordinate: ',camera_coordinate)
    return dis, camera_coordinate


if __name__ == "__main__":
    while True:
        ''' 
        获取对齐图像帧与相机参数
        '''
        color_intrin, depth_intrin, img_color, img_depth, aligned_depth_frame = get_aligned_images()  # 获取对齐图像与相机参数

        ''' 
        获取随机点三维坐标
        '''
        depth_pixel = [430, 260]  # 设置随机点
        dis, camera_coordinate = get_3d_camera_coordinate(depth_pixel, aligned_depth_frame, depth_intrin)
        print('depth: ', dis)  # 深度单位是m
        print('camera_coordinate: ', camera_coordinate)

        ''' 
        显示图像与标注
        '''
        #### 在图中标记随机点及其坐标 ####
        cv2.circle(img_color, (430, 260), 8, [255, 0, 255], thickness=-1)
        cv2.putText(img_color, "Dis:" + str(dis) + " m", (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.2, [0, 0, 255])
        cv2.putText(img_color, "X:" + str(camera_coordinate[0]) + " m", (80, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.2,
                    [255, 0, 0])
        cv2.putText(img_color, "Y:" + str(camera_coordinate[1]) + " m", (80, 120), cv2.FONT_HERSHEY_SIMPLEX, 1.2,
                    [255, 0, 0])
        cv2.putText(img_color, "Z:" + str(camera_coordinate[2]) + " m", (80, 160), cv2.FONT_HERSHEY_SIMPLEX, 1.2,
                    [255, 0, 0])

        #### 显示画面 ####
        cv2.imshow('RealSence', img_color)
        key = cv2.waitKey(1)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在gazebo仿真中,我们可以通过以下步骤使用D435相机获取某个点的三维坐标: 1. 首先,确保已经在gazebo仿真环境中正确配置了D435相机模型,并将其添加到场景中。 2. 在仿真环境中创建一个可编程的控制器或插件,以便能够通过代码访问和控制相机。通常,这可以通过插件编程语言(例如C++)或ROS中的节点来实现。 3. 在控制器或插件中,运用D435相机对应的API函数,以启动相机并开始获取图像数据。这需要设置相机的一些参数,如分辨率、帧率等。 4. 接下来,我们要识别以目标点为中心的像素坐标。可以使用OpenCV或其他图像处理库来分析相机图像数据,并通过阈值或特征点检测等算法定位目标点的像素坐标。 5. 一旦得到目标点的像素坐标,我们需要将其转换为相机坐标系下的坐标。可以通过相机的内参矩阵和畸变系数,使用OpenCV提供的函数将像素坐标转换为相机坐标。 6. 最后,将相机坐标转换为世界坐标系下的坐标。在gazebo仿真中,通过查找相机模型在场景中的位置和方向,可以使用相应的变换矩阵将相机坐标转换为世界坐标。 综上所述,通过配置相机模型并编写相应的控制器或插件,在gazebo仿真中使用D435相机获取某个点的三维坐标可以通过图像处理和变换矩阵的方法来实现。 ### 回答2: 在gazebo仿真中,可以通过使用d435相机获取某个点的三维坐标。首先,确保已经在gazebo环境中加载并启动了d435相机模型。 接下来,需要编写一个用于获取三维坐标的程序或脚本。以下是一个示例Python脚本,用于在gazebo仿真中获取d435相机中某个点的三维坐标: ```python import rospy from sensor_msgs.msg import PointCloud2 from sensor_msgs import point_cloud2 as pc2 def callback(data): # 将PointCLoud2消息转换为点云数据 pc_data = pc2.read_points(data, skip_nans=True) # 定义待获取坐标的像素点位置 pixel_x = 320 pixel_y = 240 # 通过像素点位置获取相机坐标系下的三维坐标 for i, p in enumerate(pc_data): if i == pixel_y * width + pixel_x: x, y, z = p rospy.loginfo("3D Point Coordinates: x = %f, y = %f, z = %f", x, y, z) break def listener(): rospy.init_node('point_cloud_listener', anonymous=True) rospy.Subscriber('/d435/points', PointCloud2, callback) rospy.spin() if __name__ == '__main__': listener() ``` 在该示例中,首先通过定义待获取坐标的像素点位置,这里我们假设要获取第240行,第320列的像素点的坐标。 接下来,在回调函数中,将PointCLoud2消息转换为点云数据。然后,通过迭代点云数据,找到指定像素点位置对应的三维坐标。 最后,在获取三维坐标后,可以通过rospy.loginfo函数打印出来。 请注意,该示例中的topic名称`/d435/points`是指d435相机发布的点云数据的topic名称,在实际使用中需要根据实际情况进行修改。 通过运行上述代码,就可以在gazebo仿真中获取d435相机中指定像素点位置的三维坐标信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值