rgbd
server_ros.cpp
Go to the documentation of this file.
1 #include "rgbd/server_ros.h"
2 
3 #include <pcl/point_types.h>
4 #include <pcl/point_cloud.h>
5 #include <pcl_ros/point_cloud.h>
6 
7 #include "rgbd/view.h"
8 #include "rgbd/ros/conversions.h"
9 
10 #include <sensor_msgs/Image.h>
11 #include <sensor_msgs/CameraInfo.h>
12 
13 
14 namespace rgbd {
15 
16 // ----------------------------------------------------------------------------------------
17 
18 ServerROS::ServerROS(ros::NodeHandle nh) : nh_(nh)
19 {
20 }
21 
22 // ----------------------------------------------------------------------------------------
23 
25 {
26  nh_.shutdown();
27 }
28 
29 // ----------------------------------------------------------------------------------------
30 
31 void ServerROS::initialize(std::string ns, const bool publish_rgb, const bool publish_depth, const bool publish_pc)
32 {
33  if (!ns.empty() && ns.back() != '/')
34  {
35  ns.push_back('/');
36  }
37  if (publish_rgb)
38  {
39  pub_rgb_img_ = std::make_shared<ros::Publisher>(nh_.advertise<sensor_msgs::Image>(ns + "rgb/image", 1));
40  pub_rgb_info_ = std::make_shared<ros::Publisher>(nh_.advertise<sensor_msgs::CameraInfo>(ns + "rgb/camera_info", 1));
41  ROS_DEBUG_STREAM_NAMED("ServerROS", "rgb image topic: " << pub_rgb_img_->getTopic());
42  ROS_DEBUG_STREAM_NAMED("ServerROS", "rgb camera info topic: " << pub_rgb_info_->getTopic());
43  }
44  else
45  {
46  ROS_DEBUG_NAMED("ServerROS", "rgb image publisher not initialized");
47  ROS_DEBUG_NAMED("ServerROS", "rgb camera info publisher not initialized");
48  }
49  if (publish_depth)
50  {
51  pub_depth_img_ = std::make_shared<ros::Publisher>(nh_.advertise<sensor_msgs::Image>(ns + "depth/image", 1));
52  pub_depth_info_ = std::make_shared<ros::Publisher>(nh_.advertise<sensor_msgs::CameraInfo>(ns + "depth/camera_info", 1));
53  ROS_DEBUG_STREAM_NAMED("ServerROS", "depth image topic: " << pub_depth_img_->getTopic());
54  ROS_DEBUG_STREAM_NAMED("ServerROS", "depth camera info topic: " << pub_depth_info_->getTopic());
55  }
56  else
57  {
58  ROS_DEBUG_NAMED("ServerROS", "depth image publisher not initialized");
59  ROS_DEBUG_NAMED("ServerROS", "depth camera info publisher not initialized");
60  }
61  if (publish_pc)
62  {
63  pub_depth_pc_ = std::make_shared<ros::Publisher>(nh_.advertise<pcl::PointCloud<pcl::PointXYZ> >(ns + "depth/points", 1));
64  ROS_DEBUG_STREAM_NAMED("ServerROS", "pointcloud topic: " << pub_depth_pc_->getTopic());
65  }
66  else
67  {
68  ROS_DEBUG_NAMED("ServerROS", "pointcloud publisher not initialized");
69  }
70 
71 }
72 
73 // ----------------------------------------------------------------------------------------
74 
75 void ServerROS::send(const Image& image)
76 {
77  if ((pub_depth_img_ || pub_depth_pc_) && image.getDepthImage().data)
78  {
79  // Convert camera info to message
80  rgbd::View view(image, image.getDepthImage().cols);
81 
82  if (pub_depth_img_ && (pub_depth_img_->getNumSubscribers() || pub_depth_info_->getNumSubscribers()))
83  {
84  // Convert to image messages
85  sensor_msgs::Image msg;
86  sensor_msgs::CameraInfo info_msg;
87 
88  rgbd::convert(image.getDepthImage(), view.getRasterizer(), msg, info_msg);
89 
90  msg.header.stamp.fromSec(image.getTimestamp());
91  msg.header.frame_id = image.getFrameId();
92  info_msg.header = msg.header;
93 
94  // Publish
95  pub_depth_img_->publish(msg);
96  pub_depth_info_->publish(info_msg);
97  }
98  if (pub_depth_pc_ && pub_depth_pc_->getNumSubscribers())
99  {
100  // Create point cloud
101  pcl::PointCloud<pcl::PointXYZRGB>::Ptr pc_msg(new pcl::PointCloud<pcl::PointXYZRGB>());
102 
103  pc_msg->header.stamp = static_cast<uint64_t>(image.getTimestamp() * 1e6);
104  pc_msg->header.frame_id = image.getFrameId();
105  pc_msg->width = 0;
106  pc_msg->height = 1;
107  pc_msg->is_dense = true;
108 
109  // Fill point cloud
110  for(int y = 0; y < view.getHeight(); ++y)
111  {
112  for(int x = 0; x < view.getWidth(); ++x)
113  {
114  geo::Vector3 p;
115  if (view.getPoint3D(x, y, p))
116  {
117  const cv::Vec3b& c = view.getColor(x, y);
118 
119  // Push back and correct for geolib frame
120  pc_msg->points.push_back(pcl::PointXYZRGB());
121  pcl::PointXYZRGB& p_pcl = pc_msg->points.back();
122  p_pcl.x = static_cast<float>(p.x);
123  p_pcl.y = static_cast<float>(-p.y);
124  p_pcl.z = static_cast<float>(-p.z);
125  p_pcl.r = c[2];
126  p_pcl.g = c[1];
127  p_pcl.b = c[0];
128  pc_msg->width++;
129  }
130  else
131  {
132  pc_msg->is_dense = false;
133  }
134  }
135  }
136 
137  // Publish
138  pub_depth_pc_->publish(pc_msg);
139  }
140  }
141 
142  if (pub_rgb_img_ && (pub_rgb_img_->getNumSubscribers() || pub_rgb_info_->getNumSubscribers()) && image.getRGBImage().data)
143  {
144  // Convert camera info to message
145  rgbd::View view(image, image.getRGBImage().cols);
146 
147  // Convert to image messages
148  sensor_msgs::Image msg;
149  sensor_msgs::CameraInfo info_msg;
150 
151  rgbd::convert(image.getRGBImage(), view.getRasterizer(), msg, info_msg);
152 
153  msg.header.stamp.fromSec(image.getTimestamp());
154  msg.header.frame_id = image.getFrameId();
155  info_msg.header = msg.header;
156 
157  // Publish
158  pub_rgb_img_->publish(msg);
159  pub_rgb_info_->publish(info_msg);
160  }
161 }
162 
163 }
geo::Vector3::y
const real & y() const
std::string
rgbd::Image::getDepthImage
const cv::Mat & getDepthImage() const
Get the depth image.
Definition: image.h:72
rgbd::Image::getTimestamp
double getTimestamp() const
Get the timestamp.
Definition: image.h:90
rgbd::ServerROS::pub_rgb_img_
std::shared_ptr< ros::Publisher > pub_rgb_img_
Definition: server_ros.h:48
rgbd::ServerROS::~ServerROS
virtual ~ServerROS()
Destructor.
Definition: server_ros.cpp:24
rgbd::View::getWidth
const int & getWidth() const
Definition: view.h:17
std::string::back
T back(T... args)
conversions.h
rgbd::ServerROS::ServerROS
ServerROS(ros::NodeHandle nh=ros::NodeHandle())
Constructor.
Definition: server_ros.cpp:18
std::string::push_back
T push_back(T... args)
rgbd::Image
Definition: image.h:43
rgbd::View::getRasterizer
const geo::DepthCamera & getRasterizer() const
Definition: view.h:54
rgbd::ServerROS::nh_
ros::NodeHandle nh_
Definition: server_ros.h:47
rgbd::View::getHeight
const int & getHeight() const
Definition: view.h:22
rgbd
Definition: client.h:24
rgbd::ServerROS::pub_rgb_info_
std::shared_ptr< ros::Publisher > pub_rgb_info_
Definition: server_ros.h:49
rgbd::View::getPoint3D
bool getPoint3D(int x, int y, geo::Vector3 &p) const
Definition: view.h:37
rgbd::ServerROS::send
void send(const Image &image)
Publish a new image to the selected ROS topics.
Definition: server_ros.cpp:75
server_ros.h
rgbd::Image::getRGBImage
const cv::Mat & getRGBImage() const
Get the RGB color image.
Definition: image.h:78
geo::Vector3
geo::Vector3::z
const real & z() const
rgbd::ServerROS::initialize
void initialize(std::string ns="", const bool publish_rgb=false, const bool publish_depth=false, const bool publish_pc=false)
initialize server
Definition: server_ros.cpp:31
rgbd::convert
bool convert(const cv::Mat &image, sensor_msgs::Image &image_msg)
Convert either a rgb or depth image, cv::Mat, to an image message.
Definition: conversions.cpp:32
rgbd::Image::getFrameId
const std::string & getFrameId() const
Get the frame_id.
Definition: image.h:84
rgbd::ServerROS::pub_depth_pc_
std::shared_ptr< ros::Publisher > pub_depth_pc_
Definition: server_ros.h:52
geo::Vector3::x
const real & x() const
view.h
std::string::empty
T empty(T... args)
c
void c()
rgbd::ServerROS::pub_depth_img_
std::shared_ptr< ros::Publisher > pub_depth_img_
Definition: server_ros.h:50
rgbd::View::getColor
const cv::Vec3b & getColor(int x, int y) const
Definition: view.h:27
rgbd::View
Definition: view.h:11
rgbd::ServerROS::pub_depth_info_
std::shared_ptr< ros::Publisher > pub_depth_info_
Definition: server_ros.h:51