如何将cv :: Mat转换为pcl :: pointcloud [英] How to convert cv::Mat to pcl::pointcloud

查看:374
本文介绍了如何将cv :: Mat转换为pcl :: pointcloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从opencv Mat pointcloud到pcl :: pointcloud?颜色对我来说并不重要,仅点本身就不重要.

How to get from a opencv Mat pointcloud to a pcl::pointcloud? The color is not important for me only the points itself.

推荐答案

您可以这样做:

pcl::PointCloud<pcl::PointXYZ>::Ptr SimpleOpenNIViewer::MatToPoinXYZ(cv::Mat OpencVPointCloud)
         {
             /*
             *  Function: Get from a Mat to pcl pointcloud datatype
             *  In: cv::Mat
             *  Out: pcl::PointCloud
             */

             //char pr=100, pg=100, pb=100;
             pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);//(new pcl::pointcloud<pcl::pointXYZ>);

             for(int i=0;i<OpencVPointCloud.cols;i++)
             {
                //std::cout<<i<<endl;

                pcl::PointXYZ point;
                point.x = OpencVPointCloud.at<float>(0,i);
                point.y = OpencVPointCloud.at<float>(1,i);
                point.z = OpencVPointCloud.at<float>(2,i);

                // when color needs to be added:
                //uint32_t rgb = (static_cast<uint32_t>(pr) << 16 | static_cast<uint32_t>(pg) << 8 | static_cast<uint32_t>(pb));
                //point.rgb = *reinterpret_cast<float*>(&rgb);

                point_cloud_ptr -> points.push_back(point);


             }
             point_cloud_ptr->width = (int)point_cloud_ptr->points.size();
             point_cloud_ptr->height = 1;

             return point_cloud_ptr;

         }

反之亦然

 cv::Mat MVW_ICP::PoinXYZToMat(pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr){

     cv::Mat OpenCVPointCloud(3, point_cloud_ptr->points.size(), CV_64FC1);
     for(int i=0; i < point_cloud_ptr->points.size();i++){
        OpenCVPointCloud.at<double>(0,i) = point_cloud_ptr->points.at(i).x;
        OpenCVPointCloud.at<double>(1,i) = point_cloud_ptr->points.at(i).y;
        OpenCVPointCloud.at<double>(2,i) = point_cloud_ptr->points.at(i).z;
    }

    return OpenCVPointCloud;
}

这篇关于如何将cv :: Mat转换为pcl :: pointcloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆