Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supervoxel coloring update #941

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions examples/segmentation/example_supervoxels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,8 @@ main (int argc, char ** argv)
std::cout << "Extracting supervoxels!\n";
super.extract (supervoxel_clusters);
std::cout << "Found " << supervoxel_clusters.size () << " Supervoxels!\n";
PointCloudT::Ptr colored_voxel_cloud = super.getColoredVoxelCloud ();
PointLCloudT::Ptr labeled_voxel_cloud = super.getLabeledVoxelCloud ();
PointCloudT::Ptr voxel_centroid_cloud = super.getVoxelCentroidCloud ();
PointCloudT::Ptr full_colored_cloud = super.getColoredCloud ();
PointNCloudT::Ptr sv_normal_cloud = super.makeSupervoxelNormalCloud (supervoxel_clusters);
PointLCloudT::Ptr full_labeled_cloud = super.getLabeledCloud ();

Expand All @@ -305,16 +304,25 @@ main (int argc, char ** argv)
std::cout << "Refining supervoxels \n";
super.refineSupervoxels (3, refined_supervoxel_clusters);

PointCloudT::Ptr refined_colored_voxel_cloud = super.getColoredVoxelCloud ();
PointLCloudT::Ptr refined_labeled_voxel_cloud = super.getLabeledVoxelCloud ();
PointNCloudT::Ptr refined_sv_normal_cloud = super.makeSupervoxelNormalCloud (refined_supervoxel_clusters);
PointLCloudT::Ptr refined_full_labeled_cloud = super.getLabeledCloud ();
PointCloudT::Ptr refined_full_colored_cloud = super.getColoredCloud ();

// THESE ONLY MAKE SENSE FOR ORGANIZED CLOUDS
pcl::io::savePNGFile (out_path, *full_colored_cloud, "rgb");
pcl::io::savePNGFile (refined_out_path, *refined_full_colored_cloud, "rgb");
pcl::io::savePNGFile (out_label_path, *full_labeled_cloud, "label");
pcl::io::savePNGFile (refined_out_label_path, *refined_full_labeled_cloud, "label");
if (cloud->isOrganized ())
{
pcl::io::savePNGFile (out_label_path, *full_labeled_cloud, "label");
pcl::io::savePNGFile (refined_out_label_path, *refined_full_labeled_cloud, "label");
//Save RGB from labels
pcl::io::PointCloudImageExtractorFromLabelField<PointLT> pcie (pcie.io::PointCloudImageExtractorFromLabelField<PointLT>::COLORS_RGB_GLASBEY);
//We need to set this to account for NAN points in the organized cloud
pcie.setPaintNaNsWithBlack (true);
pcl::PCLImage image;
pcie.extract (*full_labeled_cloud, image);
pcl::io::savePNGFile (out_path, image);
pcie.extract (*refined_full_labeled_cloud, image);
pcl::io::savePNGFile (refined_out_path, image);
}

std::cout << "Constructing Boost Graph Library Adjacency List...\n";
typedef boost::adjacency_list<boost::setS, boost::setS, boost::undirectedS, uint32_t, float> VoxelAdjacencyList;
Expand All @@ -341,9 +349,9 @@ main (int argc, char ** argv)
{
if (show_supervoxels)
{
if (!viewer->updatePointCloud ((show_refined)?refined_colored_voxel_cloud:colored_voxel_cloud, "colored voxels"))
if (!viewer->updatePointCloud ((show_refined)?refined_labeled_voxel_cloud:labeled_voxel_cloud, "colored voxels"))
{
viewer->addPointCloud ((show_refined)?refined_colored_voxel_cloud:colored_voxel_cloud, "colored voxels");
viewer->addPointCloud ((show_refined)?refined_labeled_voxel_cloud:labeled_voxel_cloud, "colored voxels");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE,3.0, "colored voxels");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ pcl::SupervoxelClustering<PointT>::SupervoxelClustering (float voxel_resolution,
seed_resolution_ (seed_resolution),
adjacency_octree_ (),
voxel_centroid_cloud_ (),
color_importance_(0.1f),
spatial_importance_(0.4f),
normal_importance_(1.0f),
label_colors_ (0)
color_importance_ (0.1f),
spatial_importance_ (0.4f),
normal_importance_ (1.0f)
{
adjacency_octree_.reset (new OctreeAdjacencyT (resolution_));
if (use_single_camera_transform)
Expand Down Expand Up @@ -333,8 +332,6 @@ pcl::SupervoxelClustering<PointT>::makeSupervoxels (std::map<uint32_t,typename S
sv_itr->getVoxels (supervoxel_clusters[label]->voxels_);
sv_itr->getNormals (supervoxel_clusters[label]->normals_);
}
//Make sure that color vector is big enough
initializeLabelColors ();
}


Expand Down Expand Up @@ -544,60 +541,6 @@ pcl::SupervoxelClustering<PointT>::getSupervoxelAdjacency (std::multimap<uint32_
//if (neighbor_labels.size () == 0)
// std::cout << label<<"(size="<<sv_itr->size () << ") has "<<neighbor_labels.size () << "\n";
}

}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT> pcl::PointCloud<pcl::PointXYZRGBA>::Ptr
pcl::SupervoxelClustering<PointT>::getColoredCloud () const
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr colored_cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::copyPointCloud (*input_,*colored_cloud);

pcl::PointCloud <pcl::PointXYZRGBA>::iterator i_colored;
typename pcl::PointCloud <PointT>::const_iterator i_input = input_->begin ();
std::vector <int> indices;
std::vector <float> sqr_distances;
for (i_colored = colored_cloud->begin (); i_colored != colored_cloud->end (); ++i_colored,++i_input)
{
if ( !pcl::isFinite<PointT> (*i_input))
i_colored->rgb = 0;
else
{
i_colored->rgb = 0;
LeafContainerT *leaf = adjacency_octree_->getLeafContainerAtPoint (*i_input);
VoxelData& voxel_data = leaf->getData ();
if (voxel_data.owner_)
i_colored->rgba = label_colors_[voxel_data.owner_->getLabel ()];

}

}

return (colored_cloud);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT> pcl::PointCloud<pcl::PointXYZRGBA>::Ptr
pcl::SupervoxelClustering<PointT>::getColoredVoxelCloud () const
{
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr colored_cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);
for (typename HelperListT::const_iterator sv_itr = supervoxel_helpers_.cbegin (); sv_itr != supervoxel_helpers_.cend (); ++sv_itr)
{
typename PointCloudT::Ptr voxels;
sv_itr->getVoxels (voxels);
pcl::PointCloud<pcl::PointXYZRGBA> rgb_copy;
copyPointCloud (*voxels, rgb_copy);

pcl::PointCloud<pcl::PointXYZRGBA>::iterator rgb_copy_itr = rgb_copy.begin ();
for ( ; rgb_copy_itr != rgb_copy.end (); ++rgb_copy_itr)
rgb_copy_itr->rgba = label_colors_ [sv_itr->getLabel ()];

*colored_cloud += rgb_copy;
}

return colored_cloud;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -729,28 +672,6 @@ pcl::SupervoxelClustering<PointT>::setNormalImportance (float val)
normal_importance_ = val;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT> void
pcl::SupervoxelClustering<PointT>::initializeLabelColors ()
{
uint32_t max_label = static_cast<uint32_t> (getMaxLabel ());
//If we already have enough colors, return
if (label_colors_.size () > max_label)
return;

//Otherwise, generate new colors until we have enough
label_colors_.reserve (max_label + 1);
srand (static_cast<unsigned int> (time (0)));
while (label_colors_.size () <= max_label )
{
uint8_t r = static_cast<uint8_t>( (rand () % 256));
uint8_t g = static_cast<uint8_t>( (rand () % 256));
uint8_t b = static_cast<uint8_t>( (rand () % 256));
label_colors_.push_back (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT> int
pcl::SupervoxelClustering<PointT>::getMaxLabel () const
Expand Down
Loading