ROS下 读取图片和深度图并生成点云(2)

不知道什么情况,把这些东西方一起就消失了,所以又写了一篇
#1、3 生成点云

//
// Created by bat2 on 2021/9/13.
// the purpose of this demo is to show how to turn rgb and depth into Pointcloud and publish it
//

#include "ros/ros.h"

#include <sensor_msgs/Image.h>

#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>

#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>

#include <pcl/point_cloud.h>

#include<pcl/point_types.h>
#include<pcl_conversions/pcl_conversions.h>
#include<pcl/visualization/cloud_viewer.h>
#include <sensor_msgs/PointCloud2.h>


using namespace std;
using namespace sensor_msgs;
using namespace message_filters;
using namespace cv;
typedef pcl::PointXYZRGB PointT;
typedef pcl::PointCloud<PointT> testcloud;
class picture2Point
{
public:
    ros::NodeHandle nh;
    ros::Publisher pcl_pub;

    picture2Point(): nh("~"){
        message_filters::Subscriber<sensor_msgs::Image>rgb_sub(nh,"/camera/rgb/image_raw",1);
        message_filters::Subscriber<sensor_msgs::Image>depth_sub(nh,"/camera/depth/image_raw",1);
        pcl_pub=nh.advertise<sensor_msgs::PointCloud2>("pointCloud",1);// 添加的  发布点云

        typedef message_filters::sync_policies::ExactTime<sensor_msgs::Image, sensor_msgs::Image> MySyncPolicy;
        message_filters::Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), rgb_sub, depth_sub);
        sync.registerCallback(boost::bind(&picture2Point::callback, this, _1, _2));

        ros::spin();
    }
    ~picture2Point(){};

    sensor_msgs::PointCloud2 output;

    void callback(const sensor_msgs::ImageConstPtr &RGB_image,const sensor_msgs::ImageConstPtr &depth_image)
    {
        try {
            cv_bridge::CvImageConstPtr image_ptr=cv_bridge::toCvShare(RGB_image);
            cv::Mat RGB_image=image_ptr->image;

            cv_bridge::CvImageConstPtr image_Ptr1=cv_bridge::toCvShare(depth_image);
            cv::Mat Depth_image=image_Ptr1->image;

       cv::imshow("rgb",RGB_image);
        cv::imshow("depth",Depth_image);
        cv::waitKey(10);

            picture2Point::generatePointcloud(RGB_image,Depth_image);
        }
        catch (cv_bridge::Exception &e)
        {
            ROS_ERROR("cv_bridge Exception %s",e.what());
        }
    }
    void generatePointcloud(const cv::Mat &rgb,const cv::Mat &depth)  //点云生存
    {
        pcl::PointCloud<pcl::PointXYZRGB> tmp;
        for(size_t v=0;v<rgb.rows;v++)
        {
            for(size_t u=0;u<rgb.cols;u++)
            {
                float d=depth.ptr<float>(v)[u];
                if(d<0.01||d>10)
                    continue;
                PointT p;
                p.z=d;
                p.x=(u-320)*d/554.25468;
                p.y=(v-240.5)*d/554.254;

                p.b=rgb.ptr<unsigned>(v)[u*3];
                p.g=rgb.ptr<unsigned>(v)[u*3+1];
                p.r=rgb.ptr<unsigned>(v)[u*3+2];
                tmp.points.push_back(p);
            }
        }
        pcl::visualization::CloudViewer viewer("pcd viewer");

        sensor_msgs::PointCloud2  output_msh;
        pcl::toROSMsg(tmp,output_msh);
        output_msh.header.frame_id="support_depth";
        output_msh.header.stamp=ros::Time::now();
        pcl_pub.publish(output_msh);

    }
};



int main(int argc,char *argv[])
{
    ros::init(argc,argv,"image2point");
    picture2Point node;


    return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS(机器人操作系统)是一种用于机器人软件开发的开源框架。它提供了一系列工具和库,用于构建机器人系统中的各种功能。在ROS中,我们可以使用相应的包来同时读取深度图RGB图像数据。 要同时读取深度图RGB图,我们可以使用ROS中的图像传感器消息。在ROS中,有一个常用的传感器消息类型叫做`sensor_msgs/Image`。这个消息类型可以用来传递图像数据。 首先,我们需要在ROS中配置一个用于读取深度图的节点和一个用于读取RGB图像的节点。这两个节点将订阅不同的话题,分别接收来自深度相机和RGB相机的图像数据。 深度相机节点将发布深度图像到一个特定的话题,我们可以通过订阅这个话题来获取深度图像数据。例如,我们可以使用`rostopic echo`命令来查看深度图像数据。 RGB相机节点将发布RGB图像到另一个话题,我们可以通过订阅这个话题来获取RGB图像数据。例如,我们可以使用`rostopic echo`命令来查看RGB图像数据。 当我们同时运行这两个节点时,我们可以在ROS中编写一个脚本或程序来同时订阅这两个话题,从而实现同时读取深度图RGB图像。在这个脚本或程序中,我们可以使用ROS提供的图像传感器消息类型来处理和分析这些图像数据。 总的来说,通过配置深度相机节点和RGB相机节点,并订阅相应的话题,我们可以使用ROS来同时读取深度图RGB图像,从而进行机器人感知、视觉SLAM等任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值