从Point Cloud转换为Mat

时间:2013-03-30 09:18:32

标签: c++ opencv image-processing computer-vision point-cloud-library

假设我初始化了一个点云。我想将其RGB通道存储在opencv的Mat数据类型中。我怎么能这样做?

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

3 个答案:

答案 0 :(得分:0)

我知道如何从Mat(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;

答案 1 :(得分:0)

我是否理解,您只对点云的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];
            }
        }
    }
}

我认为这足以显示基本想法。

但这只有在你的点云有组织的情况下才有效:

  

有组织的点云数据集是指向点云的名称   这类似于有组织的图像(或矩阵)之类的结构   数据分为行和列。这种点云的例子   包括来自立体相机或Time of Flight相机的数据。该   有组织的数据集的优点是通过了解这种关系   在相邻点(例如像素)之间,最近邻居操作是   更有效率,从而加快计算速度并降低计算速度   PCL中某些算法的成本。 (Source)

答案 2 :(得分:0)

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

首先应该变换坐标,使“地平面”成为X-O-Y平面。 核心api是pcl::getTransformationFromTwoUnitVectorsAndOrigin

您可以查看我的question:

祝你好运!

相关问题