Ask Your Question

Output of colored point clouds

asked 2017-08-11 07:37:29 -0600

Akitoshi gravatar image

Hello. I think that it is pretty rudimentary, but I will ask you a question because I can not solve it by trial and error. I am writing a program that outputs colored point clouds. So, I wrote the program as below, but on rviz a white point clouds is output. I changed the rgb value to various values, but there was no change. What should I do? Thank you.

#include <ros/ros.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>

typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloud;

int main(int argc, char** argv)
    ros::init (argc, argv, "pub_pcl");
    ros::NodeHandle nh;
    ros::Publisher pub = nh.advertise<PointCloud> ("points2", 1);

    ros::Rate loop_rate(1);
    while (nh.ok())
        PointCloud::Ptr msg (new PointCloud);
        msg->header.frame_id = "/my_frame";
        pcl_conversions::toPCL(ros::Time::now(), msg->header.stamp);
        msg->height = 480;
        msg->width = 640;

        for(int n=0; n<480; ++n) {
            for(int m=0; m<640; ++m) {
                msg->points[n*480+m].x = 1.0;
                msg->points[n*480+m].y = 0.01*(m-320);
                msg->points[n*480+m].z = 0.01*n;
                msg->points[n*480+m].r = 200;
                msg->points[n*480+m].g = 0;
                msg->points[n*480+m].b = 0;
                msg->points.push_back (msg->points[n*480+m]);

        pcl_conversions::toPCL(ros::Time::now(), msg->header.stamp);
        pub.publish (msg);
        ros::spinOnce ();
        loop_rate.sleep ();
edit retag flag offensive close merge delete


msg->points.push_back (msg->points[n*480+m]); is duplicating every point, you should leave it out.

lucasw gravatar image lucasw  ( 2017-08-11 10:50:40 -0600 )edit

Thank @lucasw. As you say, I have duplicated every points. However, erasing this one line did not solve the problem.

Akitoshi gravatar image Akitoshi  ( 2017-08-12 02:31:42 -0600 )edit

I tried running the code and it comes out red, though I changed the index to n*640+m, it's not going to make it through the whole point cloud otherwise (479 * 480 + 639 << 480 * 640). I'm using kinetic on 16.04.

lucasw gravatar image lucasw  ( 2017-08-13 09:35:09 -0600 )edit

Maybe on earlier versions of rviz the uninitialized points screw up all color rendering?

lucasw gravatar image lucasw  ( 2017-08-13 09:36:31 -0600 )edit

I'm using indigo on 14.04. Today, once again I tried running the same code, somehow the display on the left of the rviz screen changed and it comes out red. Was something amended in rviz's program? Anyway, I was able to solve it. I appreciate your cooperation.

Akitoshi gravatar image Akitoshi  ( 2017-08-14 14:10:24 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-02-13 20:22:57 -0600

pavel92 gravatar image


I am not sure that you can assign the values directly like that. Refer to the pcl::PointXYZRGB documentation:

It says "Due to historical reasons (PCL was first developed as a ROS package), the RGB information is packed into an integer and casted to a float. " and there is an example of how to pack/unpack the rgb data.

edit flag offensive delete link more


Next to that you have to also make sure that the RViz pointcloud2 display is configured to show RGB values of points, instead of one of the other options.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-14 01:54:37 -0600 )edit

True, RGB should be set in the Color Transformer in Rviz pointcloud2.
Btw, I tried it out yesterday and managed to assign the colors without doing the packing/unpacking. The assignment worked directly:
pcl::PointXYZRGB point;
point.r = 255;

pavel92 gravatar image pavel92  ( 2018-02-14 10:14:36 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-08-11 07:37:29 -0600

Seen: 1,919 times

Last updated: Feb 13 '18