Skip to content

Commit 2314157

Browse files
author
Markus Schoeler
committed
Now using new label color handler
1 parent 19a7740 commit 2314157

File tree

1 file changed

+15
-59
lines changed

1 file changed

+15
-59
lines changed

examples/segmentation/example_lccp_segmentation.cpp

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <pcl/console/parse.h>
4747
#include <pcl/io/pcd_io.h>
4848
#include <pcl/visualization/pcl_visualizer.h>
49+
#include <pcl/visualization/point_cloud_color_handlers.h>
4950

5051
//PCL other
5152
#include <pcl/filters/passthrough.h>
@@ -114,13 +115,6 @@ keyboardEventOccurred (const pcl::visualization::KeyboardEvent& event_arg,
114115

115116
/// ***** Prototypes ***** ///
116117

117-
/** \brief Fill vector with random colors
118-
* \param[in] label_colors_arg Vector to be filled.
119-
* \param[in] nr_labels_arg The number of random colors which will be drawn */
120-
void
121-
drawColors (std::vector<uint32_t>& label_colors_arg,
122-
const uint nr_labels_arg);
123-
124118
/** \brief Displays info text in the specified PCLVisualizer
125119
* \param[in] viewer_arg The PCLVisualizer to modify */
126120
void
@@ -324,7 +318,6 @@ LCCPSegmentation Parameters: \n\
324318

325319
/// Get the cloud of supervoxel centroid with normals and the colored cloud with supervoxel coloring (this is used for visulization)
326320
pcl::PointCloud<pcl::PointNormal>::Ptr sv_centroid_normal_cloud = pcl::SupervoxelClustering<PointT>::makeSupervoxelNormalCloud (supervoxel_clusters);
327-
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr sv_colored_cloud = super.getColoredCloud (); // This is also used for supervoxel output
328321

329322
/// The Main Step: Perform LCCPSegmentation
330323

@@ -343,29 +336,15 @@ LCCPSegmentation Parameters: \n\
343336
}
344337

345338
PCL_INFO ("Interpolation voxel cloud -> input cloud and relabeling\n");
346-
pcl::PointCloud<pcl::PointXYZL>::Ptr segment_labeled_cloud = super.getLabeledCloud ();
347-
lccp.relabelCloud (segment_labeled_cloud);
339+
pcl::PointCloud<pcl::PointXYZL>::Ptr sv_labeled_cloud = super.getLabeledCloud ();
340+
pcl::PointCloud<pcl::PointXYZL>::Ptr lccp_labeled_cloud = sv_labeled_cloud->makeShared ();
341+
lccp.relabelCloud (lccp_labeled_cloud);
348342
SuperVoxelAdjacencyList sv_adjacency_list;
349343
lccp.getSVAdjacencyList (sv_adjacency_list); // Needed for visualization
350344

351345
/// Creating Colored Clouds and Output
352-
pcl::PointCloud<PointT>::Ptr recolored_cloud_ptr (input_cloud_ptr->makeShared ());
353-
if (segment_labeled_cloud->size () == input_cloud_ptr->size ())
346+
if (lccp_labeled_cloud->size () == input_cloud_ptr->size ())
354347
{
355-
PCL_INFO ("Coloring cloud\n");
356-
std::vector<uint32_t> label_colors;
357-
drawColors (label_colors, supervoxel_clusters.size ());
358-
359-
/* NOTE: segment_labeled_cloud was produced by getLabeledCloud (), which performs an interpolation to the input cloud.
360-
Thus, segment_labeled_cloud and the input cloud should have the same size and points with the same ID should correspond to one another. */
361-
362-
for (int pointID = 0; pointID < segment_labeled_cloud->size (); ++pointID)
363-
{
364-
PointT& search_point = recolored_cloud_ptr->points[pointID];
365-
pcl::PointXYZL& label_point = segment_labeled_cloud->points[pointID];
366-
search_point.rgba = label_colors[label_point.label];
367-
}
368-
369348
if (output_specified)
370349
{
371350
PCL_INFO ("Saving output\n");
@@ -374,12 +353,12 @@ LCCPSegmentation Parameters: \n\
374353
if (hasField (input_pointcloud2, "label"))
375354
PCL_WARN ("Input cloud already has a label field. It will be overwritten by the lccp segmentation output.\n");
376355
pcl::PCLPointCloud2 output_label_cloud2, output_concat_cloud2;
377-
pcl::toPCLPointCloud2 (*segment_labeled_cloud, output_label_cloud2);
356+
pcl::toPCLPointCloud2 (*lccp_labeled_cloud, output_label_cloud2);
378357
pcl::concatenateFields (input_pointcloud2, output_label_cloud2, output_concat_cloud2);
379358
pcl::io::savePCDFile (outputname + "_out.pcd", output_concat_cloud2, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), save_binary_pcd);
380359
}
381360
else
382-
pcl::io::savePCDFile (outputname + "_out.pcd", *segment_labeled_cloud, save_binary_pcd);
361+
pcl::io::savePCDFile (outputname + "_out.pcd", *lccp_labeled_cloud, save_binary_pcd);
383362

384363
if (sv_output_specified)
385364
{
@@ -448,18 +427,16 @@ LCCPSegmentation Parameters: \n\
448427
pcl::PointXYZRGBA vert_curr = supervoxel->centroid_;
449428

450429

451-
const uint32_t sv_neighbor_label = sv_adjacency_list[*itr_neighbor];
430+
const uint32_t sv_neighbor_label = sv_adjacency_list[*itr_neighbor];
452431
pcl::Supervoxel<PointT>::Ptr supervoxel_neigh = supervoxel_clusters.at (sv_neighbor_label);
453432
pcl::PointXYZRGBA vert_neigh = supervoxel_neigh->centroid_;
454-
455433

456434
points->InsertNextPoint (vert_curr.data);
457-
points->InsertNextPoint (vert_neigh.data);
458-
435+
points->InsertNextPoint (vert_neigh.data);
459436

460-
// Add the points to the dataset
461-
vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New ();
462-
polyLine->GetPointIds ()->SetNumberOfIds (2);
437+
// Add the points to the dataset
438+
vtkSmartPointer<vtkPolyLine> polyLine = vtkSmartPointer<vtkPolyLine>::New ();
439+
polyLine->GetPointIds ()->SetNumberOfIds (2);
463440
polyLine->GetPointIds ()->SetId (0,points->GetNumberOfPoints ()-2);
464441
polyLine->GetPointIds ()->SetId (1,points->GetNumberOfPoints ()-1);
465442
cells->InsertNextCell (polyLine);
@@ -474,10 +451,10 @@ LCCPSegmentation Parameters: \n\
474451
/// END: Calculate visualization of adjacency graph
475452

476453
/// Configure Visualizer
477-
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
454+
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
478455
viewer->setBackgroundColor (0, 0, 0);
479456
viewer->registerKeyboardCallback (keyboardEventOccurred, 0);
480-
viewer->addPointCloud (recolored_cloud_ptr, "maincloud");
457+
viewer->addPointCloud (lccp_labeled_cloud, "maincloud");
481458

482459
/// Visualization Loop
483460
PCL_INFO ("Loading viewer\n");
@@ -486,7 +463,7 @@ LCCPSegmentation Parameters: \n\
486463
viewer->spinOnce (100);
487464

488465
/// Show Segmentation or Supervoxels
489-
viewer->updatePointCloud ( (show_supervoxels) ? sv_colored_cloud : recolored_cloud_ptr, "maincloud");
466+
viewer->updatePointCloud ( (show_supervoxels) ? sv_labeled_cloud : lccp_labeled_cloud, "maincloud");
490467

491468
/// Show Normals
492469
if (normals_changed)
@@ -531,27 +508,6 @@ LCCPSegmentation Parameters: \n\
531508

532509
/// -------------------------| Definitions of helper functions|-------------------------
533510

534-
void
535-
drawColors (std::vector<uint32_t>& label_colors_arg,
536-
const uint nr_labels_arg)
537-
{
538-
// Create colors for coloring segments
539-
const uint32_t max_label = 1.1 * nr_labels_arg; //10% error margin..
540-
label_colors_arg.clear ();
541-
label_colors_arg.reserve (max_label);
542-
//NOTE First label pushed back can be used for errors
543-
label_colors_arg.push_back (static_cast<uint32_t> (0) << 16 | static_cast<uint32_t> (0) << 8 | static_cast<uint32_t> (0));
544-
545-
srand (static_cast<unsigned int> (time (0)));
546-
for (size_t i_label = 0; i_label < max_label; i_label++)
547-
{
548-
uint8_t r = static_cast<uint8_t> ( (rand () % 256));
549-
uint8_t g = static_cast<uint8_t> ( (rand () % 256));
550-
uint8_t b = static_cast<uint8_t> ( (rand () % 256));
551-
label_colors_arg.push_back (static_cast<uint32_t> (r) << 16 | static_cast<uint32_t> (g) << 8 | static_cast<uint32_t> (b));
552-
}
553-
}
554-
555511
void
556512
printText (boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer_arg)
557513
{

0 commit comments

Comments
 (0)