image_utilities.cpp
Go to the documentation of this file.
1 #include <cv_bridge/cv_bridge.h>
3 
4 #include <string>
5 #include <utility>
6 #include <vector>
7 
8 #include <ros/ros.h>
10 
11 
12 double const NXLIB_TIMESTAMP_OFFSET = 11644473600;
13 
14 std::string imageEncoding(bool isFloat, int channels, int bytesPerElement)
15 {
16  if (isFloat)
17  {
18  switch (channels)
19  {
20  case 1:
21  return (bytesPerElement == 4) ? sensor_msgs::image_encodings::TYPE_32FC1 :
23  case 2:
24  return (bytesPerElement == 4) ? sensor_msgs::image_encodings::TYPE_32FC2 :
26  case 3:
27  return (bytesPerElement == 4) ? sensor_msgs::image_encodings::TYPE_32FC3 :
29  case 4:
30  return (bytesPerElement == 4) ? sensor_msgs::image_encodings::TYPE_32FC4 :
32  }
33  }
34  else
35  {
36  switch (channels)
37  {
38  case 1:
39  switch (bytesPerElement)
40  {
41  case 1:
43  case 2:
45  case 4:
47  }
48  break;
49  case 2:
50  switch (bytesPerElement)
51  {
52  case 1:
54  case 2:
56  case 4:
58  }
59  break;
60  case 3:
61  switch (bytesPerElement)
62  {
63  case 1:
65  case 2:
67  case 4:
69  }
70  break;
71  case 4:
72  switch (bytesPerElement)
73  {
74  case 1:
76  case 2:
78  case 4:
80  }
81  break;
82  }
83  }
84 
85  ROS_ERROR("Invalid image encoding in binary NxLib node.");
86  return "";
87 }
88 
89 sensor_msgs::ImagePtr imageFromNxLibNode(NxLibItem const& node, std::string const& frame)
90 {
91  auto image = boost::make_shared<sensor_msgs::Image>();
92 
93  bool isFloat;
94  int width, height, channels, bytesPerElement;
95  double timestamp;
96  node.getBinaryDataInfo(&width, &height, &channels, &bytesPerElement, &isFloat, &timestamp);
97 
98  image->header.stamp.fromSec(timestamp - NXLIB_TIMESTAMP_OFFSET);
99  image->header.frame_id = frame;
100  image->width = width;
101  image->height = height;
102  image->step = width * channels * bytesPerElement;
103  image->encoding = imageEncoding(isFloat, channels, bytesPerElement);
104 
105  node.getBinaryData(image->data, 0);
106 
107  return image;
108 }
109 
110 std::pair<sensor_msgs::ImagePtr, sensor_msgs::ImagePtr> imagePairFromNxLibNode(const NxLibItem& node,
111  std::string const& frame)
112 {
113  auto leftImage = imageFromNxLibNode(node[itmLeft], frame);
114  auto rightImage = imageFromNxLibNode(node[itmRight], frame);
115 
116  return { leftImage, rightImage };
117 }
118 
119 std::vector<std::pair<sensor_msgs::ImagePtr, sensor_msgs::ImagePtr>> imagesFromNxLibNode(const NxLibItem& node,
120  std::string const& frame)
121 {
122  std::vector<std::pair<sensor_msgs::ImagePtr, sensor_msgs::ImagePtr>> result;
123 
124  if (node.isArray())
125  {
126  for (int i = 0; i < node.count(); i++)
127  {
128  result.push_back(imagePairFromNxLibNode(node[i], frame));
129  }
130  }
131  else
132  {
133  result.push_back(imagePairFromNxLibNode(node, frame));
134  }
135 
136  return result;
137 }
138 
139 ros::Time timestampFromNxLibNode(const NxLibItem& node)
140 {
141  double timestamp;
142  node.getBinaryDataInfo(0, 0, 0, 0, 0, &timestamp);
143 
144  return ros::Time(timestamp - NXLIB_TIMESTAMP_OFFSET);
145 }
146 
147 sensor_msgs::ImagePtr depthImageFromNxLibNode(NxLibItem const& node, std::string const& frame)
148 {
149  double timestamp;
150  cv::Mat pointMap;
151  node.getBinaryData(pointMap, &timestamp);
152 
153  cv::Mat depthImage;
154  cv::extractChannel(pointMap, depthImage, 2);
155 
156  // convert cv mat to ros image
157  cv_bridge::CvImage out_msg;
158  out_msg.header.stamp.fromSec(timestamp - NXLIB_TIMESTAMP_OFFSET);
159  out_msg.header.frame_id = frame;
161  out_msg.image = depthImage;
162 
163  return out_msg.toImageMsg();
164 }
const std::string TYPE_16UC4
sensor_msgs::ImagePtr imageFromNxLibNode(NxLibItem const &node, std::string const &frame)
const std::string TYPE_64FC3
const std::string TYPE_8UC4
ros::Time timestampFromNxLibNode(const NxLibItem &node)
double const NXLIB_TIMESTAMP_OFFSET
const std::string TYPE_32FC4
std::string encoding
sensor_msgs::ImagePtr depthImageFromNxLibNode(NxLibItem const &node, std::string const &frame)
std::vector< std::pair< sensor_msgs::ImagePtr, sensor_msgs::ImagePtr > > imagesFromNxLibNode(const NxLibItem &node, std::string const &frame)
std::string imageEncoding(bool isFloat, int channels, int bytesPerElement)
std::pair< sensor_msgs::ImagePtr, sensor_msgs::ImagePtr > imagePairFromNxLibNode(const NxLibItem &node, std::string const &frame)
const std::string TYPE_32FC1
const std::string TYPE_8UC2
const std::string MONO16
const std::string TYPE_64FC4
const std::string TYPE_16UC3
const std::string TYPE_16UC2
const std::string TYPE_32SC2
const std::string TYPE_64FC1
const std::string TYPE_64FC2
const std::string TYPE_32SC3
const std::string TYPE_8UC3
const std::string TYPE_32SC4
const std::string TYPE_32FC3
const std::string TYPE_32FC2
#define ROS_ERROR(...)
const std::string TYPE_32SC1
sensor_msgs::ImagePtr toImageMsg() const
std_msgs::Header header


ensenso_camera
Author(s): Ensenso
autogenerated on Thu May 16 2019 02:44:23