Bootstrap

realsense提取到的像素坐标转相机坐标系(三维空间)

先上代码,文字描述和原理过一阵再补,写毕业论文忙的一匹,大家先凑合着看看注释吧,注释都改成中文了。

// An highlighted block
#!/usr/bin/python
#-*- coding:utf-8 -*-
import numpy as np
# ros
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
import message_filters
from std_msgs.msg import Float64MultiArray

#image process
import cv2
from cv_bridge import CvBridge, CvBridgeError

camera_factor = 1000;
camera_cx = 649.190; #4个内参,取决于你的相机本身
camera_cy = 346.390;
camera_fx = 913.031;
camera_fy = 913.837;
#m = cx_target; #图像坐标系下的u,v
#n = cy_target;
m=0
n=0

def callback1(depth_data):
    #print(depth_data.data)
    global m,n
    depth_image_raw = bridge.imgmsg_to_cv2(depth_data, desired_encoding='passthrough')
    depth_image_raw = depth_image_raw/1000.0 #单位转换
    print(depth_image_raw.astype(np.float32)[m][n])
    d=depth_image_raw.astype(np.float32)[m][n]
    x = (d/ camera_factor)*1000; # 前后
    y = (n - camera_cx) * x / camera_fx; #左右
    z = (m - camera_cy) * x / camera_fy; #上下
    print(x,y,z)
    array = Float64MultiArray()
    array.data.append(x)
    array.data.append(y)
    array.data.append(z)
    pub.publish(array)

def callback2(msg):
    global m,n
    m=msg.data[0]
    n=msg.data[1]

if __name__ == '__main__':
    rospy.init_node('image_converter', anonymous=True)  
    rospy.Subscriber('/pixel_result_out', Float64MultiArray, callback2)
    rospy.Subscriber('/camera/depth/image_rect_raw', Image, callback1)
    
    pub = rospy.Publisher('/result_out_xyz', Float64MultiArray,queue_size=1)
    global bridge
    bridge = CvBridge()
    rospy.spin()
;