Displaying differently colored point clouds with PCLVisualizer

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

Displaying differently colored point clouds with PCLVisualizer

Zoid
Hello all,

I am new in PCL and I have some difficulty (maybe trivial for some) to display different colours for a two-point point cloud.

More specifically some of my code is the following:

int rgb1 = 255<< 16 | 0 << 8 | 0;
int rgb2 = 0<< 16 | 255 << 8 | 0;
pcl17::PointCloud<pcl17::PointXYZRGB>::Ptr cloud_ref (new pcl17::PointCloud<pcl17::PointXYZRGB>);

cloud_ref->width = 2;
cloud_ref->height = 1;
cloud_ref->is_dense = false;
cloud_ref->points.resize(cloud_ref->width * cloud_ref->height);

cloud_ref->points[0].x = max_pt_orig.x;
cloud_ref->points[0].y = min_pt_orig.y;
cloud_ref->points[0].z = max_pt_orig.z;
cloud_ref->points[0].rgb=rgb2;

cloud_ref->points[1].x = min_pt_orig.x;
cloud_ref->points[1].y = max_pt_orig.y;
cloud_ref->points[1].z = min_pt_orig.z;
cloud_ref->points[1].rgb=rgb1;

pcl17::io::savePCDFileASCII ("cloud_ref.pcd", *cloud_ref);
pcl17::visualization::PCLVisualizer viewer;
viewer.setBackgroundColor (0, 0, 0);
viewer.addCoordinateSystem (1.0);
viewer.initCameraParameters ();
pcl17::visualization::PointCloudColorHandlerRGBField<pcl17::PointXYZRGB> rgbA(cloud_ref);
viewer.addPointCloud(cloud_ref, rgbA, "reference_cloud");
viewer.setPointCloudRenderingProperties (pcl17::visualization::PCL17_VISUALIZER_POINT_SIZE, 1, "reference_cloud");

Unfortunately, both points are visualized with read colour no matter what rgb1 and rgb2 values I have...

Could someone help me with that please?

Best,

Akis
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Sergey
Administrator
Hi Akis,

I think in this simple case PointCloudColorHandlers are not necessary. Simply add your point cloud with:

    viewer.addPointCloud(cloud_ref, "reference_cloud");

Regards,
Sergey
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Zoid
Hello and thx for your reply.

Unfortunately, I have tried that and I am still getting both points as red dots! :(

Any ideas?

Akis
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Sergey
Administrator
Akis,

Here is the thing. You compose the colors in integer variables and then assign to the `rgb` field, which is float. You should either have your colors in float format, or assign your integers to the `rgba` field, which is of integer type. (Do not worry that your point type is PointXYZRGB, it actually has `rgba` field.)

And as I mentioned, this will work without PointCloudColorHandler.

Regards,
Sergey
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Zoid
It works!! :) Thank you for your help Sergey!

Best,

Akis
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

nizar sallem
Hi,

This may not be the best way to do it in PCL.
Here is a snippet of code you could use to display two point clouds with
different colors without modifying your point cloud :

/** Assuming that you have two clouds (source and target) you want to
display in red and green respectively. */
// here you create a custom color handler color set to red
  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB>
red_source (source, 255, 0, 0);
// same color set to green
   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB>
green_target (target, 0, 255, 0);

// Now add the clouds to a visualizer with appropriate handlers
visualizer_.addPointCloud<pcl::PointXYZRGB> (source, red_source,
"source");

visualizer_.addPointCloud<pcl::PointXYZRGB> (target, green_target,
"target");

And that is it,
--
Nizar

On 19/09/2013 19:49, Zoid wrote:

> It works!! :) Thank you for your help Sergey!
>
> Best,
>
> Akis
>
>
>
> --
> View this message in context: http://www.pcl-users.org/Displaying-differently-colored-point-clouds-with-PCLVisualizer-tp4029756p4029760.html
> Sent from the Point Cloud Library (PCL) Users mailing list mailing list archive at Nabble.com.
> _______________________________________________
> [hidden email] / http://pointclouds.org
> http://pointclouds.org/mailman/listinfo/pcl-users
>
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Sergey
Administrator
Hi,

For the sake of completeness, there is another (fast) way to display a point cloud with a specific color:

    visualizer_.addPointCloud<pcl::PointXYZRGB> (source, "source");
    visualizer_.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_COLOR, 1.0f, 0.0f, 0.0f, "source");

The numbers in the second line are the R, G, and B components of the color in [0..1] range.

Regards,
Sergey
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Zoid
Hello again,

I have a little bit more complicated problem now. I have extracted the following indices stored in a vector of type: std::vector<pcl17::PointIndices> cluster_indices. Then I am trying to paint the points belonging to the clusters indicated by "cluster_indices" by writing the following code:

for (int i = 0; i < cluster_indices.size (); i++){

    for (size_t it = 0; it < cluster_indices[i].indices.size(); ++it)
      cloud_cropped->points[cluster_indices[i].indices[it]].rgba = 255<<16;
}

And finally I am trying to see the result at the PCLVisualizer as follows:

if (!viewer->wasStopped ())
{
    if (cloud_detected_first_time){    
      viewer->addPointCloud<pcl17::PointXYZRGB> (cloud_cropped);
      cloud_detected_first_time = false;  
      viewer->spinOnce(100);
    }
    else{
      viewer->updatePointCloud<pcl17::PointXYZRGB> (cloud_cropped);
      viewer->spinOnce(100);
    }
}

Unfortunately, all the points remain white...

And one last question. If I want to paint the points of each individual cluster with different colour, do you have any idea of how to do that?

Thank you for your help in advance. :)

Best,

Akis
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

nizar sallem
Hi,

Well conerning the white stuff you may want to hit '5' to display RGB
but if your points are not of the same type this could be the problem.
Then, you should be able to do it easier. I will propose two methods
which have different execution speed, so if you are not concerned with
speed you can choose any of them:

1. mark the keypoints you want: in this approach you will iterate
through your points as in ths code snippet:

  for (size_t i = 0; i < keypoints->size (); ++i)
   {
     std::stringstream ss;
     ss << "sphere_" << i;
     visualizer_.addSphere<PointT> ((*keypoints)[i], 0.005, 255, 0, 0,
ss.str ());
   }
You can use other shape or color of course.

2. superpose clouds: you can take advantage of the fact that XYZ
components will be the same on the cloud and the keypoint so just add
the keypoints on top of the cloud at the same viewport with the color
you choose.



On 08/10/2013 14:40, Zoid wrote:

> Hello again,
>
> I have a little bit more complicated problem now. I have extracted the
> following indices stored in a vector of type:
> std::vector<pcl17::PointIndices> cluster_indices. Then I am trying to paint
> the points belonging to the clusters indicated by "cluster_indices" by
> writing the following code:
>
> for (int i = 0; i < cluster_indices.size (); i++){
>
>      for (size_t it = 0; it < cluster_indices[i].indices.size(); ++it)
>        cloud_cropped->points[cluster_indices[i].indices[it]].rgba = 255<<16;
> }
>
> And finally I am trying to see the result at the PCLVisualizer as follows:
>
> if (!viewer->wasStopped ())
> {
>      if (cloud_detected_first_time){
>        viewer->addPointCloud<pcl17::PointXYZRGB> (cloud_cropped);
>        cloud_detected_first_time = false;
>        viewer->spinOnce(100);
>      }
>      else{
>        viewer->updatePointCloud<pcl17::PointXYZRGB> (cloud_cropped);
>        viewer->spinOnce(100);
>      }
> }
>
> Unfortunately, all the points remain white...
>
> And one last question. If I want to paint the points of each individual
> cluster with different colour, do you have any idea of how to do that?
>
> Thank you for your help in advance. :)
>
> Best,
>
> Akis
>
>
>
> --
> View this message in context: http://www.pcl-users.org/Displaying-differently-colored-point-clouds-with-PCLVisualizer-tp4029756p4030048.html
> Sent from the Point Cloud Library (PCL) Users mailing list mailing list archive at Nabble.com.
> _______________________________________________
> [hidden email] / http://pointclouds.org
> http://pointclouds.org/mailman/listinfo/pcl-users
>
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Zoid
Hi nizar and thank you for your reply.

I tried pressing the '5' button but nothing happens... :/ I was wandering if there is a way to paint the actual points of "cloud_cropped" point cloud instead of assigning small spheres or superimposing "cloud_cropped" with other coloured points... If there isn't any I'll have to implement your solutions.  

Thank you very much for your help!!!

Akis
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

nizar sallem
There s always a way ;)
I think colors are changed but you are displaying different types of
points in the viewer so that they only appear white.
Use the ColorHandler and you will be able to see the colors.

--
Nizar

On 08/10/2013 15:37, Zoid wrote:

> Hi nizar and thank you for your reply.
>
> I tried pressing the '5' button but nothing happens... :/ I was wandering if
> there is a way to paint the actual points of "cloud_cropped" point cloud
> instead of assigning small spheres or superimposing "cloud_cropped" with
> other coloured points... If there isn't any I'll have to implement your
> solutions.
>
> Thank you very much for your help!!!
>
> Akis
>
>
>
> --
> View this message in context: http://www.pcl-users.org/Displaying-differently-colored-point-clouds-with-PCLVisualizer-tp4029756p4030056.html
> Sent from the Point Cloud Library (PCL) Users mailing list mailing list archive at Nabble.com.
> _______________________________________________
> [hidden email] / http://pointclouds.org
> http://pointclouds.org/mailman/listinfo/pcl-users
>
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Zoid
It works!!! :) Thank you so much Nizar.

Best,

Akis
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

boyihu
In reply to this post by Zoid
Hi,

I am a new to the PCL, I am trying to repeat and and run this example and here is my code:

int main(int argc, char** argv)
{
        int rgb1 = 255<< 16 | 0 << 8 | 0;
    int rgb2 = 0<< 16 | 255 << 8 | 0;
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_ref (new pcl::PointCloud<pcl::PointXYZRGB>);

    cloud_ref->width = 2;
    cloud_ref->height = 1;
    cloud_ref->is_dense = false;
    cloud_ref->points.resize(cloud_ref->width * cloud_ref->height);

    cloud_ref->points[0].x = 1;
    cloud_ref->points[0].y = 1;
    cloud_ref->points[0].z = 1;
    cloud_ref->points[0].rgb=rgb2;

    cloud_ref->points[1].x = 0;
    cloud_ref->points[1].y = 0;
    cloud_ref->points[1].z = 0;
    cloud_ref->points[1].rgb=rgb1;

    //pcl::io::savePCDFileASCII ("cloud_ref.pcd", *cloud_ref);
    pcl::visualization::PCLVisualizer viewer;
    viewer.setBackgroundColor (0, 0, 0);
    viewer.addCoordinateSystem (1.0);
    viewer.initCameraParameters ();
    //pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgbA(cloud_ref);
    viewer.addPointCloud(cloud_ref, "reference_cloud");
    viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "reference_cloud");

    while (!viewer.wasStopped ())
    {
    //you can also do cool processing here
    //FIXME: Note that this is running in a separate thread from viewerPsycho
    //and you should guard against race conditions yourself...
    //user_data++;
    }
   
    return 0;
}

I do not know why it is not working as it should be, the visualization window pops up, but it is all write and does not display any point.

Can anybody tells me where the fault is? Thanks.

Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

Sergey
Administrator
Hi,

You should call

    viewer.spinOnce (100);

inside the while loop.

Cheers,
Sergey
Reply | Threaded
Open this post in threaded view
|

Re: Displaying differently colored point clouds with PCLVisualizer

boyihu
Thank you! Your suggestion solved the problem, cool!