Output of colored point clouds
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;
msg->points.resize(480*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 ();
}
}
msg->points.push_back (msg->points[n*480+m]);
is duplicating every point, you should leave it out.Thank @lucasw. As you say, I have duplicated every points. However, erasing this one line did not solve the problem.
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.Maybe on earlier versions of rviz the uninitialized points screw up all color rendering?
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.