ROS接收颜色点云并转换为图像和颜色点云

5 篇文章 0 订阅
  1. 点云转换
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import numpy as np
import cv2.cv2 as cv

import rospy
from sensor_msgs.msg import PointCloud2
import ros_numpy

def main():
    rospy.init_node("subscriber")
    pub = rospy.Publisher('/stone/points', PointCloud2, queue_size=5)

    watch_points = np.zeros((480,640), dtype=[('x', np.float32), ('y', np.float32), ('z', np.float32), (('rgb', np.float32))])
    watch_rgb = np.zeros((480,640), dtype=[('r', np.uint8), ('g', np.uint8), ('b', np.uint8)])
    
    while True:
        msg = rospy.wait_for_message("/camera_03/depth_registered/points", PointCloud2)
        data_points = ros_numpy.numpify(msg)

        # rgb
        data_rgb = ros_numpy.point_cloud2.split_rgb_field(data_points)
        b, g, r = data_rgb['b'][:, :, np.newaxis], data_rgb['g'][:, :, np.newaxis], data_rgb['r'][:, :, np.newaxis]
        bgr = np.concatenate([b, g, r], axis=2)
        print(bgr.dtype, np.unique(bgr))
        # xyz
        x, y, z = data_points['x'], data_points['y'], data_points['z']
        x, y, z = x[:, :,   np.newaxis], y[:, :, np.newaxis], z[:, :, np.newaxis]
        xyz = np.concatenate([x, y, z], axis=2)
        print("XYZ: ", xyz.shape)

        # 显示
        cv.imshow("rgb", bgr)
        cv.waitKey(1)

        watch_points['x'] = xyz[:, :, 0]
        watch_points['y'] = xyz[:, :, 1]
        watch_points['z'] = xyz[:, :, 2]

        watch_rgb['r'] = bgr[:, :, 2]
        watch_rgb['g'] = bgr[:, :, 1]
        watch_rgb['b'] = bgr[:, :, 0]

        watch_points['rgb'] = ros_numpy.point_cloud2.merge_rgb_fields(watch_rgb)

        msg = ros_numpy.msgify(PointCloud2, watch_points, frame_id="camera_03_rgb_frame")
        pub.publish(msg)


if __name__ == "__main__":
    main()

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
点云转换为世界坐标需要以下步骤: 1. 获取点云数据。可以通过ROS中的sensor_msgs::PointCloud2消息获取点云数据。 2. 创建一个tf::TransformListener对象,用于获取点云数据的坐标系和世界坐标系之间的转换关系。 3. 将点云数据转换为pcl::PointCloud<pcl::PointXYZ>类型,以便进行坐标转换。 4. 遍历点云中的每个点,将其从点云坐标系转换为世界坐标系。可以使用tf::TransformListener对象的tf::StampedTransform()方法进行转换。 5. 将转换后的点云数据发布到ROS中,可以使用pcl::toROSMsg()方法将pcl::PointCloud<pcl::PointXYZ>类型的点云数据转换为sensor_msgs::PointCloud2消息,然后通过ROS中的Publisher发布点云数据。 以下是示例代码: ``` #include <ros/ros.h> #include <sensor_msgs/PointCloud2.h> #include <pcl_ros/point_cloud.h> #include <pcl_conversions/pcl_conversions.h> #include <pcl/point_types.h> #include <tf/transform_listener.h> void cloudCallback(const sensor_msgs::PointCloud2ConstPtr& input_cloud) { // Create a transform listener object tf::TransformListener listener; // Convert input point cloud message to pcl::PointCloud<pcl::PointXYZ> pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::fromROSMsg(*input_cloud, *cloud); // Get the transform from input cloud frame to world frame tf::StampedTransform transform; try{ listener.waitForTransform("world", input_cloud->header.frame_id, ros::Time(0), ros::Duration(3.0)); listener.lookupTransform("world", input_cloud->header.frame_id, ros::Time(0), transform); } catch (tf::TransformException ex){ ROS_ERROR("%s",ex.what()); return; } // Transform each point from input cloud frame to world frame for (size_t i = 0; i < cloud->points.size (); ++i) { tf::Point p(cloud->points[i].x, cloud->points[i].y, cloud->points[i].z); tf::Point pt = transform * p; cloud->points[i].x = pt.x(); cloud->points[i].y = pt.y(); cloud->points[i].z = pt.z(); } // Publish transformed point cloud sensor_msgs::PointCloud2 output_cloud; pcl::toROSMsg(*cloud, output_cloud); output_cloud.header.frame_id = "world"; output_cloud.header.stamp = input_cloud->header.stamp; pub.publish(output_cloud); } int main(int argc, char** argv) { ros::init(argc, argv, "transform_cloud"); ros::NodeHandle nh; // Subscribe to input point cloud topic ros::Subscriber sub = nh.subscribe<sensor_msgs::PointCloud2>("input_cloud", 1, cloudCallback); // Advertise output point cloud topic pub = nh.advertise<sensor_msgs::PointCloud2>("output_cloud", 1); ros::spin(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值