Ubutntu下使用realsense d435i(二):获取某二维像素点的三维坐标

00 准备工作

准备工作包括安装驱动以及pyrealsense2,具体可参考上一篇:


01 通过二维像素点获得物体三维坐标

这个在上一篇已经介绍了代码:

详细的注释可以参考上一篇,这一篇只放一下代码,并将关键代码做简要介绍。


realsense官方的api介绍可以参考:


realsense提供的api中,通过二维像素点获得三维坐标的共有 两种方式

  • 获取该像素点的深度 --> 根据深度z,获得x和y
  • 通过点云计算x,y,z坐标

上述两种方法得到的结果完全相同。


将图像用opencv显示出来,计算的三维坐标结果也用cv2.putText()显示在图像上。

其中需要注意的是,颜色顺序不是RGB,而是BGR,所以想改字体颜色的时候需要注意一下。


1.1 通过深度计算(rs.rs2_deproject_pixel_to_point

# -*- 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 = [320, 240]        # 设置随机点,以相机中心点为例
        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, (320,240), 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.1.1 代码解析

  • config.enable_stream()函数可以设置不同的分辨率
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流


config.enable_stream(rs.stream.depth,  848, 480, rs.format.z16, 90)
config.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, 30)


config.enable_stream(rs.stream.depth,  1280, 720, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
  • aligned_depth_frame.get_distance(x, y)是关键代码
    • x和y为二维图像,像素点的坐标
    • 返回值为对应像素点的深度
  • camera_coordinate = rs.rs2_deproject_pixel_to_point(intrin=depth_intrin, pixel=[x, y], depth=dis)是关键代码
    • 输入 depth_intrin :从上一步获取
    • 输入 x :像素点的x
    • 输入 y :像素点的y
    • 输入 dis :上一步计算的真实距离(输入的dis与输出的距离z是一样的,改变的只是x与y)
    • 输出 camera_coordinate :二维点在实际中对象的三维坐标x,y,z
dis = aligned_depth_frame.get_distance(x, y)        # 获取该像素点对应的深度

camera_coordinate = rs.rs2_deproject_pixel_to_point(depth_intrin, depth_pixel, dis) # 获取对应像素点的三维坐标

1.2 通过点云计算

# -*- 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流

# config.enable_stream(rs.stream.depth,  848, 480, rs.format.z16, 90)
# config.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, 30)

# config.enable_stream(rs.stream.depth,  1280, 720, rs.format.z16, 30)
# config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)

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

pc = rs.pointcloud()        # 声明点云对象
points = rs.points()
 

''' 
获取图像帧
'''
def get_images():
    
    frames = pipeline.wait_for_frames()     # 等待获取图像帧,获取颜色和深度的框架集     

    depth_frame = frames.get_depth_frame()      # 获取depth帧 
    color_frame = frames.get_color_frame()      # 获取color帧

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

    return  img_color, img_depth, depth_frame, color_frame


''' 
获取随机点三维坐标(点云方法)
'''
def get_3d_camera_coordinate(depth_pixel, color_frame, depth_frame):
    x = depth_pixel[0]
    y = depth_pixel[1]

    ###### 计算点云 #####
    pc.map_to(color_frame)
    points = pc.calculate(depth_frame)
    vtx = np.asanyarray(points.get_vertices())
    #  print ('vtx_before_reshape: ', vtx.shape)        # 307200
    vtx = np.reshape(vtx,(480, 640, -1))   
    # print ('vtx_after_reshape: ', vtx.shape)       # (480, 640, 1)

    camera_coordinate = vtx[y][x][0]
    # print ('camera_coordinate: ',camera_coordinate)
    dis = camera_coordinate[2]
    return dis, camera_coordinate



if __name__=="__main__":
    while True:
        ''' 
        获取图像帧
        '''
        img_color, img_depth, depth_frame, color_frame = get_images()        # 获取图像


        ''' 
        获取随机点三维坐标
        '''
        depth_pixel = [320, 240]        # 设置随机点,以相机中心点为例
        dis, camera_coordinate = get_3d_camera_coordinate(depth_pixel, color_frame, depth_frame)
        print ('depth: ',dis)       # 深度单位是m
        print ('camera_coordinate: ',camera_coordinate)


        ''' 
        显示图像与标注
        '''
        #### 在图中标记随机点及其坐标 ####
        cv2.circle(img_color, (320,240), 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.2.1 代码解析

  • 点云计算的关键代码如下:
pc.map_to(color_frame)
points = pc.calculate(depth_frame)
vtx = np.asanyarray(points.get_vertices())
  • 坐标计算的关键代码如下
    • 注意reshape中参数的顺序
    • 注意是先y,在x
vtx = np.reshape(vtx,(480, 640, -1)) 
camera_coordinate = vtx[y][x][0]

02 待解决

  • depth_scale不为1,放到实际的depth中是应该乘还是除,还是不需要处理呢?
要将二维坐标点转换为相机三维坐标点,我们需要知道相机的内部参数和外部参数。 内部参数包括焦距、主点、图像尺寸等信息,可以通过相机标定获得。外部参数包括相机的位置和朝向,可以通过计算机视觉中的相机位姿估计方法获得。 假设已知相机的内部参数以及相机在世界坐标系下的位姿,我们可以通过以下步骤将二维坐标点转换为相机三维坐标点: 1. 将二维坐标点归一化,即将像素坐标 $(u,v)$ 转换为归一化坐标 $(x,y)$,其中 $x=(u-c_x)/f_x$,$y=(v-c_y)/f_y$,$c_x$ 和 $c_y$ 分别为主点的横纵坐标,$f_x$ 和 $f_y$ 分别为相机焦距在横纵方向上的分量。 2. 将归一化坐标 $(x,y)$ 转换为相机坐标系下的坐标 $(X_c,Y_c,Z_c)$,其中 $Z_c$ 为相机到目标物体的距离。 $$ \begin{bmatrix} X_c \\ Y_c \\ Z_c \end{bmatrix} = \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} Z_c $$ 3. 将相机坐标系下的坐标 $(X_c,Y_c,Z_c)$ 转换为世界坐标系下的坐标 $(X_w,Y_w,Z_w)$,其中 $(X_w,Y_w,Z_w)$ 为目标物体在世界坐标系下的坐标。 $$ \begin{bmatrix} X_w \\ Y_w \\ Z_w \\ 1 \end{bmatrix} = \begin{bmatrix} R_{11} & R_{12} & R_{13} & T_x \\ R_{21} & R_{22} & R_{23} & T_y \\ R_{31} & R_{32} & R_{33} & T_z \\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} X_c \\ Y_c \\ Z_c \\ 1 \end{bmatrix} $$ 其中 $R$ 为相机的旋转矩阵,$T$ 为相机的平移向量。 因此,将二维坐标点 $(u,v)$ 转换为相机三维坐标点 $(X_w,Y_w,Z_w)$ 的完整代码如下: ```python import numpy as np # 相机内部参数 fx = 100 # 焦距 fy = 100 cx = 320 # 主点 cy = 240 # 相机外部参数 R = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) # 旋转矩阵 T = np.array([0, 0, 0]) # 平移向量 # 二维坐标点 u = 200 v = 150 # 归一化坐标 x = (u - cx) / fx y = (v - cy) / fy # 相机坐标系下的坐标 Z_c = 1 # 假设相机到目标物体的距离为1 X_c = x * Z_c Y_c = y * Z_c # 世界坐标系下的坐标 P_c = np.array([X_c, Y_c, Z_c, 1]).reshape(4, 1) P_w = np.dot(np.hstack((R, T.reshape(3, 1))), P_c) X_w, Y_w, Z_w = P_w[0, 0], P_w[1, 0], P_w[2, 0] print('二维坐标点 ({}, {}) 转换为相机三维坐标点 ({}, {}, {})'.format(u, v, X_w, Y_w, Z_w)) ``` 需要注意的是,上述代码中默认相机的旋转矩阵为单位矩阵,即相机的朝向与世界坐标系重合。如果实际情况中相机的朝向不同,需要根据实际情况修改旋转矩阵 $R$ 的值。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值