PCL :PCL可视化工具在同一视口中以不同的颜色显示多点云(XYZ)

PCL: PCLvisualizer multiple point clouds (XYZ) in same viewport with different colours

本文关键字:PCL 多点 颜色 显示 XYZ 工具 可视化 视口      更新时间:2023-10-16

我有一个空间点的点云向量(仅限XYZ),我试图用不同的颜色在同一窗口中显示它。我在 PCL可视化工具中使用自定义颜色处理程序。

不幸的是,显示的查看器中的颜色并没有什么不同,但所有点都以相同的颜色显示(第二个)。似乎每次添加新的点云时,查看器中所有先前点的颜色都会更改。

这是我的代码:

pcl::visualization::PCLVisualizer viewer; 
viewer.setBackgroundColor (0, 0, 0); 
std::stringstream cloud_name; 
int counter(0); 
pcl::RGB rgb_color; 
for (auto curr_cloud : clouds_vector) 
{ 
rgb_color = pcl::GlasbeyLUT::at(counter); 
++counter; 
cloud_name.str(""); 
cloud_name << "Cloud " << counter; 
if (counter < 5) 
{ 
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> first_color(curr_cloud, rgb_color.r, rgb_color.g, rgb_color.b); 
viewer.addPointCloud<pcl::PointXYZ> (curr_cloud, first_color, cloud_name.str()); 
} 
else 
{ 
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> second_color(curr_cloud, rgb_color.r, rgb_color.g, rgb_color.b); 
viewer.addPointCloud<pcl::PointXYZ> (curr_cloud, second_color, cloud_name.str()); 
} 
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 8, cloud_name.str()); 
}

任何想法为什么会这样以及要改变什么?

提前非常感谢大家!

编辑:我已经尝试了一点,问题似乎不是我可视化点云本身的方式(上面描述的方式是使用具有随机值的新创建的云。

在我的用例中,我借助单个点云的索引提取多个点云。如果我这样做,它们在可视化时仍然保持相同的颜色。我仍然无法弄清楚为什么会这样,并且很高兴收到任何建议。以下是提取的完整代码(为了使用随机创建的母点云进行提取的最小示例):

PointCloudT::Ptr cloud (new PointCloudT);
// 100 points in point cloud, random coordinates
cloud->resize(100);
for (PointCloudT::iterator cloud_it (cloud->begin()); cloud_it != cloud->end(); ++cloud_it) {
cloud_it->x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud_it->y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud_it->z = 1024 * rand () / (RAND_MAX + 1.0f);
}
// Create index vectors containing 20 indices each
std::vector <pcl::PointIndices> point_indices_vector;
int offset(20);
pcl::PointIndices indices;
for (int i=0;i<5;++i) {
for (int j=0;j<20;++j) {
indices.indices.push_back(j + i * offset);
}
point_indices_vector.push_back(indices);
indices.indices.clear(); // <----- THIS WAS MISSING
}
// Create extraction object to copy points from 100 strong cloud to 20-point clouds
pcl::ExtractIndices<PointT> extract;
extract.setInputCloud (cloud);
extract.setNegative (false);
// Extract points and copy them to clouds vector
std::vector<PointCloudT::Ptr, Eigen::aligned_allocator<PointCloudT::Ptr> > clouds_vector;
PointCloudT::Ptr curr_segment_cloud;
for (auto curr_index_vector : point_indices_vector) {
curr_segment_cloud.reset (new PointCloudT);
// Copy points of current line segment from source cloud into current segment cloud
extract.setIndices(boost::make_shared<const pcl::PointIndices> (curr_index_vector));
extract.filter (*curr_segment_cloud);
// Push back point cloud into return vector
clouds_vector.push_back(curr_segment_cloud);
}
// Create viewer
pcl::visualization::PCLVisualizer viewer;
viewer.setBackgroundColor (0, 0, 0);
// Visualize point clouds from clouds vector
std::stringstream cloud_name;
int counter(0);
pcl::RGB rgb;
for (auto curr_cloud : clouds_vector) {
++counter;
cloud_name.str("");
cloud_name << "Cloud " << counter;
// Generate unique colour
rgb = pcl::GlasbeyLUT::at(counter);
// Create colour handle
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> colour_handle(curr_cloud, rgb.r, rgb.g, rgb.b);
// Add points to viewer and set parameters
viewer.addPointCloud<pcl::PointXYZ> (curr_cloud, colour_handle, cloud_name.str());
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 8, cloud_name.str());
}
// Keep viewer running
while (!viewer.wasStopped()) {
viewer.spinOnce(100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}

问题解决了!

因此,如果有人想使用代码...上面的两个片段现在都可以工作了。(我发现了错误并在上面的代码片段中更正了它)。

我很尴尬地说,我只是忘记在提取点之前清除我的提取索引向量。因此,最后的云又是完整的云,并且绘制了之前添加的所有点,使它们不可见。这样,所有点看起来都是相同的颜色。o.O