rgbd
server_rgbd.cpp
Go to the documentation of this file.
1 #include "rgbd/server_rgbd.h"
2 
3 #include <boost/iostreams/filtering_streambuf.hpp>
4 #include <boost/iostreams/copy.hpp>
5 #include <boost/iostreams/filter/zstd.hpp>
6 
7 #include <opencv2/core/mat.hpp>
8 #include <opencv2/imgcodecs.hpp>
9 
10 #include <rgbd_msgs/RGBD.h>
11 
12 #include <ros/node_handle.h>
13 
15 
16 #include <sstream>
17 
18 #include "rgbd/image.h"
19 #include "rgbd/serialization.h"
20 
21 
22 namespace rgbd {
23 
24 const int ServerRGBD::MESSAGE_VERSION = 4;
25 
26 // ----------------------------------------------------------------------------------------
27 
28 ServerRGBD::ServerRGBD(ros::NodeHandle nh) : nh_(nh)
29 {
30 }
31 
32 // ----------------------------------------------------------------------------------------
33 
35 {
36  nh_.shutdown();
39 }
40 
41 // ----------------------------------------------------------------------------------------
42 
43 void ServerRGBD::initialize(const std::string& name, RGBStorageType rgb_type, DepthStorageType depth_type, const float service_freq)
44 {
45  pub_image_ = nh_.advertise<rgbd_msgs::RGBD>(name, 1);
46  rgb_type_ = rgb_type;
47  depth_type_ = depth_type;
48 
49  nh_.setCallbackQueue(&cb_queue_);
50  service_server_ = nh_.advertiseService(name, &ServerRGBD::serviceCallback, this);
52 }
53 
54 // ----------------------------------------------------------------------------------------
55 
56 void ServerRGBD::send(const Image& image)
57 {
58  {
60  image_ = image.clone();
61  }
62 
63  if (pub_image_.getNumSubscribers() == 0)
64  return;
65 
66  rgbd_msgs::RGBDPtr msg = boost::make_shared<rgbd_msgs::RGBD>();
67  msg->version = MESSAGE_VERSION;
68 
69  std::stringstream stream, stream2;
71  serialize(image, a, rgb_type_, depth_type_);
72  boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
73  in.push(boost::iostreams::zstd_compressor(boost::iostreams::zstd::best_speed));
74  in.push(stream);
75  boost::iostreams::copy(in, stream2);
76  tue::serialization::convert(stream2, msg->rgb);
77 
78  pub_image_.publish(msg);
79 }
80 
81 // ----------------------------------------------------------------------------------------
82 
83 bool ServerRGBD::serviceCallback(rgbd_msgs::GetRGBDRequest& req, rgbd_msgs::GetRGBDResponse& resp)
84 {
85  rgbd::Image image;
86  {
88  image = image_.clone();
89  }
90  // Check for valid input
91  if (req.compression != rgbd_msgs::GetRGBDRequest::JPEG && req.compression != rgbd_msgs::GetRGBDRequest::PNG)
92  {
93  ROS_ERROR_NAMED("ServerRGBD", "Invalid compression, only JPEG and PNG are supported (see ENUM in srv definition)");
94  return false;
95  }
96 
97  // Create resized images
98  cv::Mat resized_rgb, resized_depth;
99 
100  double ratio_rgb = static_cast<double>(req.width) / static_cast<double>(image.getRGBImage().cols);
101  double ratio_depth = static_cast<double>(req.width) / static_cast<double>(image.getDepthImage().cols);
102 
103  cv::resize(image.getRGBImage(), resized_rgb, cv::Size(req.width, static_cast<int>(image.getRGBImage().rows * ratio_rgb)));
104  cv::resize(image.getDepthImage(), resized_depth, cv::Size(req.width, static_cast<int>(image.getDepthImage().rows * ratio_depth)));
105 
106  // Compress images
107  std::string compression_str = req.compression == rgbd_msgs::GetRGBDRequest::JPEG ? ".jpeg" : ".png";
108  if (cv::imencode(compression_str, resized_rgb, resp.rgb_data) && cv::imencode(compression_str, resized_depth, resp.depth_data))
109  return true;
110 
111  ROS_ERROR_STREAM_NAMED("ServerRGBD", "cv::imencode with compression_str " << compression_str << " failed!");
112 
113  return false;
114 }
115 
116 // ----------------------------------------------------------------------------------------
117 
118 void ServerRGBD::serviceThreadFunc(const float freq)
119 {
120  ros::Rate r(freq);
121  while(nh_.ok())
122  {
123  cb_queue_.callAvailable();
124  r.sleep();
125  }
126 }
127 
128 }
sstream
rgbd::ServerRGBD::service_server_
ros::ServiceServer service_server_
Definition: server_rgbd.h:60
rgbd::ServerRGBD::initialize
void initialize(const std::string &name, RGBStorageType rgb_type=RGB_STORAGE_LOSSLESS, DepthStorageType depth_type=DEPTH_STORAGE_LOSSLESS, const float service_freq=10)
Initialize server.
Definition: server_rgbd.cpp:43
std::string
rgbd::Image::getDepthImage
const cv::Mat & getDepthImage() const
Get the depth image.
Definition: image.h:72
a
void a()
std::stringstream
rgbd::ServerRGBD::ServerRGBD
ServerRGBD(ros::NodeHandle nh=ros::NodeHandle())
Constructor.
Definition: server_rgbd.cpp:28
rgbd::ServerRGBD::image_
rgbd::Image image_
Definition: server_rgbd.h:66
tue::serialization::OutputArchive
rgbd::ServerRGBD::~ServerRGBD
virtual ~ServerRGBD()
Destructor.
Definition: server_rgbd.cpp:34
rgbd::ServerRGBD::image_mutex_
std::mutex image_mutex_
Definition: server_rgbd.h:67
tue::serialization::convert
void convert(Archive &a, std::vector< unsigned char > &data)
std::thread::joinable
T joinable(T... args)
rgbd::serialize
bool serialize(const Image &image, tue::serialization::OutputArchive &a, RGBStorageType rgb_type=RGB_STORAGE_JPG, DepthStorageType depth_type=DEPTH_STORAGE_PNG)
Definition: serialization.cpp:23
rgbd::DepthStorageType
DepthStorageType
Definition: image.h:36
rgbd::Image
Definition: image.h:43
rgbd::ServerRGBD::serviceThreadFunc
void serviceThreadFunc(const float frequency)
Function to be called in the thread proving the service.
Definition: server_rgbd.cpp:118
std::thread
rgbd
Definition: client.h:24
rgbd::RGBStorageType
RGBStorageType
Definition: image.h:29
std::unique_lock
server_rgbd.h
rgbd::ServerRGBD::rgb_type_
RGBStorageType rgb_type_
Definition: server_rgbd.h:63
image.h
rgbd::Image::getRGBImage
const cv::Mat & getRGBImage() const
Get the RGB color image.
Definition: image.h:78
rgbd::ServerRGBD::service_thread_
std::thread service_thread_
Definition: server_rgbd.h:70
rgbd::Image::clone
Image clone() const
Create a clone of this image. The depth and RGB images will be cloned, so the new image will not shar...
Definition: image.cpp:56
rgbd::ServerRGBD::MESSAGE_VERSION
const static int MESSAGE_VERSION
version of the RGBD message being used
Definition: server_rgbd.h:54
rgbd::ServerRGBD::depth_type_
DepthStorageType depth_type_
Definition: server_rgbd.h:64
rgbd::ServerRGBD::cb_queue_
ros::CallbackQueue cb_queue_
Definition: server_rgbd.h:61
rgbd::ServerRGBD::nh_
ros::NodeHandle nh_
Definition: server_rgbd.h:58
serialization.h
conversions.h
rgbd::ServerRGBD::serviceCallback
bool serviceCallback(rgbd_msgs::GetRGBDRequest &req, rgbd_msgs::GetRGBDResponse &resp)
serviceCallback
Definition: server_rgbd.cpp:83
rgbd::ServerRGBD::pub_image_
ros::Publisher pub_image_
Definition: server_rgbd.h:59
std::thread::join
T join(T... args)
rgbd::ServerRGBD::send
void send(const Image &image)
Write a new image to all interfaces.
Definition: server_rgbd.cpp:56