Rosanswers logo

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 ();

Originally posted by Akitoshi on ROS Answers with karma: 11 on 2017-08-11

Post score: 1

Original comments

Comment by lucasw on 2017-08-11:
msg->points.push_back (msg->points[n*480+m]); is duplicating every point, you should leave it out.

Comment by Akitoshi on 2017-08-12:
Thank @lucasw. As you say, I have duplicated every points. However, erasing this one line did not solve the problem.

Comment by lucasw on 2017-08-13:
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.

Comment by lucasw on 2017-08-13:
Maybe on earlier versions of rviz the uninitialized points screw up all color rendering?

Comment by Akitoshi on 2017-08-14:
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.


1 Answer 1


Rosanswers logo


I am not sure that you can assign the values directly like that. Refer to the pcl::PointXYZRGB documentation: http://docs.pointclouds.org/1.7.0/structpcl_1_1_point_x_y_z_r_g_b.html

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.

Originally posted by pavel92 with karma: 1655 on 2018-02-13

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by gvdhoorn on 2018-02-14:
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.

Comment by pavel92 on 2018-02-14:
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;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.