Working with RGB Cloud

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Working with RGB Cloud

Wyllich
Hello,

For some reason, I can't manage to get the correct colors via pcl_viewer when I try to visualize the output.

Here's a snippet :

#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
//#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>

#include <iostream>
#include <vector>

int main (int argc, char** argv){
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  pcl::PointCloud<pcl::PointXYZRGB>::Ptr colorisedCloud (new pcl::PointCloud<pcl::PointXYZRGB>);
 
  /*if(pcl::io::loadPLYFile<pcl::PointXYZ>(argv[1], *cloud)==-1){
    PCL_ERROR ("Couldn't read file \n");
    return (-1);
  }
 
  if(pcl::io::loadPLYFile<pcl::PointXYZRGB>(argv[1], *colorisedCloud)==-1){
    PCL_ERROR ("Couldn't read file \n");
    return (-1);
  }*/
 
 
  if(pcl::io::loadPCDFile<pcl::PointXYZ>(argv[1], *cloud)==-1){
    PCL_ERROR ("Couldn't read file \n");
    return (-1);
  }
 
  if(pcl::io::loadPCDFile<pcl::PointXYZRGB>(argv[1], *colorisedCloud)==-1){
    PCL_ERROR ("Couldn't read file \n");
    return (-1);
  }
 
   
  pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;

  kdtree.setInputCloud(cloud);

  pcl::PointXYZ searchPoint = cloud->points[0];
   
  uint8_t r, g, b;
  uint32_t red, green, blue, white;
 
  r=255, g=0, b=0;
  red = (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
 
  r=0, g=255,b=0;
  green = (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
 
  r=0, g=0,b=255;
  blue = (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
 
  r=255, g=255, b=255;
  white = (static_cast<uint32_t>(r) << 16 | static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
 
  int K = 10;
 
  std::vector <int> indexKNN(K);
  std::vector <float> KNNDistances(K);
 
  for(size_t i=0; i<colorisedCloud -> points.size(); ++i)
    colorisedCloud -> points[ i ].rgb = green;
   
   
  if(kdtree.nearestKSearch(searchPoint, K, indexKNN, KNNDistances)>0){
    std::cout << "Found " << K << " points of coordinates : " << std::endl;
    for(size_t i=0; i<indexKNN.size(); ++i){
        colorisedCloud->points[ indexKNN[i] ].rgb = blue;
        std::cout << " x : " << colorisedCloud->points[ indexKNN[i] ].x
                  << " y : " << colorisedCloud->points[ indexKNN[i] ].y
                  << " z : " << colorisedCloud->points[ indexKNN[i] ].z
                  << " at distance " << KNNDistances[i] << std::endl;
    }
  }
       
  pcl::io::savePCDFileASCII ("output.pcd", *colorisedCloud);
 
  return(0);
}
Reply | Threaded
Open this post in threaded view
|

Re: Working with RGB Cloud

Wyllich
An update :
   I circumvent the problem by directly using assignments of the type:
      point.r = value_r, point.g=value_g, point.b=value_b.

Nonetheless, I am still interested in how you use the structure point.rgb = ...

Thanks in advance.