I am trying to send different colored markers in RViz so that I can visualize whether my program is working accordingly. I am only able to send the coordinate values of the points of the object for now. My problem is that I am not able to set the colors for those points. Here is my code:
#include <ros/ros.h>
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <visualization_msgs/Marker.h>
#include "std_msgs/String.h"
#include "std_msgs/Float32.h"
#include "std_msgs/Int32.h"
#include "rosbag/bag.h"
#include <rosbag/view.h>
#include <vector>
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
ros::Publisher pub;
ros::Publisher marker_pub;
void
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
pcl::PointCloud<pcl::PointXYZRGB> output;
pcl::fromROSMsg(*input,output);
visualization_msgs::Marker points;
points.header.frame_id = "/camera_link";
points.header.stamp = ros::Time::now();
points.ns = "lines";
points.action = visualization_msgs::Marker::ADD;
points.pose.orientation.w = 1.0;
points.type = visualization_msgs::Marker::POINTS;
points.scale.x = 0.003;
points.scale.y = 0.003;
points.id = 0;
points.color.r=1; points.color.a=1;
geometry_msgs::Point p;
for(int i=0;i<ac1.size();i++) {
if(i%10==0 && !isnan(ac1.at(i)) && !isnan(bc1.at(i)) && !isnan(cc1.at(i)) && cc1.at(i)!=0) {
points.id=i; points.color.r=xc.at(i)/255; points.color.g=yc.at(i)/255; points.color.b=zc.at(i)/255;
p.x=ac1.at(i); p.y=bc1.at(i); p.z=cc1.at(i); points.points.push_back(p);
marker_pub.publish(points); }
}
cout<<"finish"<<endl;
}
int
main(int argc, char** argv)
{
ROS_INFO("main");
ros::init(argc, argv,"obj_rec");
ros::NodeHandle nh;
ros::Subscriber sub = nh.subscribe ("/camera/depth/points", 1, cloud_cb);
pub = nh.advertise<sensor_msgs::PointCloud2>("output",1);
marker_pub = nh.advertise<visualization_msgs::Marker> ("out",1);
ros::spin();
}
Here ac1
, bc1
and cc1
vectors contain the coordinates of the object and the xc
, yc
and zc
vector contains RGB values of corresponding points. I haven't put the entire code because it is too long and to run this single ROS node other C++ programs are required.
If you are using
POINTS
as your marker, as your code suggests, you should specify the fieldcolors
(not to be confused with the fieldcolor
). The fieldcolors
must be a list with the same number of elements as there are in the fieldpoints
. This is specified in the message documentation.This snippet produce three points with different colors:
This code produces the following: