【发布时间】:2019-05-05 17:53:58
【问题描述】:
我有一个空间点的点云向量(仅限 XYZ),我试图在同一个窗口中以不同的颜色显示它。我在 PCLvisualizer 中使用自定义颜色处理程序。
不幸的是,在显示的查看器中颜色并没有不同,但是所有点都以相同的颜色显示(第二个)。每次添加新点云时,查看器中所有先前点的颜色似乎都发生了变化。
这是我的代码:
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));
}
【问题讨论】:
-
pcl::RGB保存uint范围内的值[0,255]。PointCloudColorHandlerCustom但是需要在[0,1]范围内的双精度值。尝试将 rgb 值除以 255,然后将其提供给PointCloudColorHandlerCustom。 -
感谢您的建议@kanstar,很抱歉花了这么长时间才回复...不过这似乎不是问题。我已经用随机创建的点云尝试了上述方法并且它正在工作。罪魁祸首似乎在于我创建可视化的云矢量的方式...请参阅上面原始帖子中的编辑。
标签: c++ point-cloud-library point-clouds