从点云转换为太(Conversion from PointCloud to Mat)

2019-10-17 22:00发布

比方说,我初始化一个点云。 我想存储在OpenCV中的垫数据类型的RGB通道。 我怎样才能做到这一点?

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);   //Create a new cloud
pcl::io::loadPCDFile<pcl::PointXYZRGBA> ("cloud.pcd", *cloud);

Answer 1:

我知道如何从垫(3D图像)转换为XYZRGB。 我想你能想出其他办法。 这里Q是差距深度矩阵。

 pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
 double px, py, pz;
 uchar pr, pg, pb;

 for (int i = 0; i < img_rgb.rows; i++)
 {
     uchar* rgb_ptr = img_rgb.ptr<uchar>(i);
     uchar* disp_ptr = img_disparity.ptr<uchar>(i);
     double* recons_ptr = recons3D.ptr<double>(i);
     for (int j = 0; j < img_rgb.cols; j++)
     {
         //Get 3D coordinates

          uchar d = disp_ptr[j];
          if ( d == 0 ) continue; //Discard bad pixels
          double pw = -1.0 * static_cast<double>(d) * Q32 + Q33; 
          px = static_cast<double>(j) + Q03;
          py = static_cast<double>(i) + Q13;
          pz = Q23;

          // Normalize the points
          px = px/pw;
          py = py/pw;
          pz = pz/pw;

          //Get RGB info
          pb = rgb_ptr[3*j];
          pg = rgb_ptr[3*j+1];
          pr = rgb_ptr[3*j+2];

          //Insert info into point cloud structure
          pcl::PointXYZRGB point;
          point.x = px;
          point.y = py;
          point.z = pz;
          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;


Answer 2:

难道我的理解对不对,你是只在点云的RGB值感兴趣,并不关心它的XYZ值?

然后,你可以这样做:

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>); 
//Create a new cloud
pcl::io::loadPCDFile<pcl::PointXYZRGBA> ("cloud.pcd", *cloud);

cv::Mat result; 

if (cloud->isOrganized()) {
    result = cv::Mat(cloud->height, cloud->width, CV_8UC3);

    if (!cloud->empty()) {

        for (int h=0; h<result.rows; h++) {
            for (int w=0; w<result.cols; w++) {
                pcl::PointXYZRGBA point = cloud->at(w, h);

                Eigen::Vector3i rgb = point.getRGBVector3i();

                result.at<cv::Vec3b>(h,w)[0] = rgb[2];
                result.at<cv::Vec3b>(h,w)[1] = rgb[1];
                result.at<cv::Vec3b>(h,w)[2] = rgb[0];
            }
        }
    }
}

我认为这足以表明其基本思想。

但这只是作品,如果你的点云的组织:

有组织的点云数据集是赋予点云类似于一个有组织的图像(或矩阵)状的结构,其中,数据被划分为行和列的名称。 这种点云的例子包括数据从立体相机或飞行时间相机的到来。 一个有组织的数据集的一个优点是,通过知道相邻点之间(例如像素)的关系,最近邻居操作是更有效的,从而加快了计算和降低的在PCL某些算法的成本。 (资源)



Answer 3:

我有同样的问题,我成功解决吧!

你应该首先转换坐标,使您的“地平面”是XOY平面。 核心API是PCL :: getTransformationFromTwoUnitVectorsAndOrigin

你可以看看我的问题:

祝好运!



文章来源: Conversion from PointCloud to Mat