在rgb图像中使用来自点云的聚簇索引

时间:2013-05-15 18:40:28

标签: c++ opencv point-cloud-library

我正在使用从两张图片中获取的深度图(我从opencv StereoBM中获取),现在我需要在其中找到群集 我决定使用pcl区域增长分段http://www.pointclouds.org/documentation/tutorials/region_growing_segmentation.php。在阅读本文http://blog.martinperis.com/2012/01/3d-reconstruction-with-opencv-and-point.html之后,我将cv :: Mat转换为点云,现在我有了集群索引 这在此处起作用https://gist.github.com/Daiver/5586252 现在我想要使用这些索引在StereoBM(cv :: Mat)的深度图上显示聚类

我正在尝试这个,但我对结果不满意

  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud; //cloud from depth map and rgb image
  std::vector <pcl::PointIndices> clusters;// clusters inices, extracted before
  for(int j = 0; j < clusters.size(); j++)
  {
      cv::Mat to_show = cv::Mat::zeros(288, 384, cv::DataType<uchar>::type);//image has size that equal size of rgb image and depth map        
      for(int i = 0; i < clusters[j].indices.size(); i++)
      {
        to_show.data[clusters[j].indices[i]] = 200;// regions in this Mat must be equal with regions from depth map
      }
      cv::imshow("", to_show);
      cv::waitKey();
  }

结果 一些集群 enter image description here 另一个集群 enter image description here

可视化云 enter image description here

我如何将集群投射到cv :: Mat? PS对不起我的写作错误。英语不是我的母语

UPD 我试图通过在mat_to_cloud函数中使用循环来“恢复”深度图

int counter = 0;
cv::Mat to_show = cv::Mat::zeros(288, 384, cv::DataType<uchar>::type);
for(int i = 0; i < cloud->height; i++)
{
  for(int j = 0; j < cloud->width; j++)
  {
    to_show.at<uchar>(i, j) = cloud->at(counter).z;
    counter++;
  }
}

enter image description here

另一种循环顺序     int counter = 0;     cv :: Mat to_show = cv :: Mat :: zeros(288,384,cv :: DataType :: type);     for(int j = 0; j&lt; cloud-&gt; width; j ++)     {       for(int i = 0; i&lt; cloud-&gt; height; i ++)       {         to_show.at(i,j)= cloud-&gt; at(counter).z;         计数器++;       }     }

enter image description here

我不知道为什么这些图像是相似的

2 个答案:

答案 0 :(得分:1)

我之前没有使用PCL,但看起来这行可能是错误的:

// regions in this Mat must be equal with regions from depth map
to_show.data[clusters[j].indices[i]] = 200;

to_show是一个opencv矩阵,但您使用点云中的索引。您需要先将索引转换为像素坐标。

答案 1 :(得分:0)

我解决了我的问题,但我的解决方案有点肮脏和愚蠢。 我制作了自己的简单重投影功能

void straight_reproject_cloud(cv::Mat& img_rgb, cv::Mat& img_disparity, pcl::PointCloud<pcl::PointXYZRGB>::Ptr& point_cloud_ptr)
{
    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);
        for (int j = 0; j < img_rgb.cols; j++)
        {
            uchar d = disp_ptr[j];
            if ( d == 0 ) continue; //Discard bad pixels
            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 = j;
            point.y = i;
            point.z = d;
            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->push_back (point);
        } 
    }
}

此功能可直接从图像向云添加点,而无需更改 新云与旧的重投影不符,但我可以使用它

现在,云中的点坐标被合并到图像中的坐标 我可以在图像中显示云中的所有聚类:

for(int k = 0; k < clusters.size(); k++)
{
    cv::Mat res = cv::Mat::zeros(img_rgb.rows, img_rgb.cols, CV_8U);
    //for(int i =0 ; i < point_cloud_ptr->points.size(); i++)
    for(int j =0 ; j < clusters[k].indices.size(); j++)
    {
        int i = clusters[k].indices[j];
        int x = point_cloud_ptr->at(i).x;
        int y = point_cloud_ptr->at(i).y;
        res.at<uchar>(y, x) = (int)(point_cloud_ptr->at(i).z);
    }
    cv::imshow("rec2", res);
    cv::waitKey();
}

enter image description here

新云

enter image description here