YARP
Yet Another Robot Platform
RGBDRosConversionUtils.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <cmath>
7 #include <algorithm>
8 #include <iomanip>
9 #include <cstdint>
10 
11 #include <yarp/os/LogComponent.h>
12 #include <yarp/os/Value.h>
13 #include <yarp/sig/ImageUtils.h>
15 
16 #include "RGBDRosConversionUtils.h"
17 #include "rosPixelCode.h"
18 
19 using namespace yarp::dev;
20 using namespace yarp::sig;
21 using namespace yarp::os;
22 using namespace yarp::dev::RGBDRosConversionUtils;
23 
24 namespace {
25 YARP_LOG_COMPONENT(RGBD_ROS, "yarp.device.RGBDRosConversion")
26 }
27 
28 commonImageProcessor::commonImageProcessor(std::string cameradata_topic_name, std::string camerainfo_topic_name)
29 {
30  if (this->topic(cameradata_topic_name)==false)
31  {
32  yCError(RGBD_ROS) << "Error opening topic:" << cameradata_topic_name;
33  }
34  if (m_subscriber_camera_info.topic(camerainfo_topic_name) == false)
35  {
36  yCError(RGBD_ROS) << "Error opening topic:" << camerainfo_topic_name;
37  }
38  m_cameradata_topic_name = cameradata_topic_name;
39  m_camerainfo_topic_name = camerainfo_topic_name;
40  m_contains_rgb_data = false;
41  m_contains_depth_data = false;
42 }
43 commonImageProcessor::~commonImageProcessor()
44 {
45  this->close();
46  m_subscriber_camera_info.close();
47 }
48 
49 bool commonImageProcessor::getLastRGBData(yarp::sig::FlexImage& data, yarp::os::Stamp& stmp)
50 {
51  if (m_contains_rgb_data == false) { return false;}
52 
53  //this blocks untils the first data is received;
54  /*size_t counter = 0;
55  while (m_contains_rgb_data == false)
56  {
57  yarp::os::Time::delay(0.1);
58  if (counter++ > 100) { yCDebug(RGBD_ROS) << "Waiting for incoming rgb data..."; counter = 0; }
59  }*/
60 
61  m_port_mutex.lock();
62  data = m_lastRGBImage;
63  stmp = m_lastStamp;
64  m_port_mutex.unlock();
65  return true;
66 }
67 
68 bool commonImageProcessor::getLastDepthData(yarp::sig::ImageOf<yarp::sig::PixelFloat>& data, yarp::os::Stamp& stmp)
69 {
70  if (m_contains_depth_data == false) { return false;}
71 
72  //this blocks untils the first data is received;
73  /*size_t counter = 0;
74  while (m_contains_depth_data == false)
75  {
76  yarp::os::Time::delay(0.1);
77  if (counter++ > 100) { yCDebug(RGBD_ROS) << "Waiting for incoming depth data..."; counter = 0; }
78  }*/
79 
80  m_port_mutex.lock();
81  data = m_lastDepthImage;
82  stmp = m_lastStamp;
83  m_port_mutex.unlock();
84  return true;
85 }
86 
87 size_t commonImageProcessor::getWidth() const
88 {
89  return m_lastDepthImage.width();
90 }
91 
92 size_t commonImageProcessor::getHeight() const
93 {
94  return m_lastDepthImage.height();
95 }
96 
97 void commonImageProcessor::onRead(yarp::rosmsg::sensor_msgs::Image& v)
98 {
99  m_port_mutex.lock();
101  if (yarp_pixcode == VOCAB_PIXEL_RGB ||
102  yarp_pixcode == VOCAB_PIXEL_BGR)
103  {
104  m_lastRGBImage.setPixelCode(yarp_pixcode);
105  m_lastRGBImage.resize(v.width, v.height);
106  size_t c = 0;
107  for (auto it = v.data.begin(); it != v.data.end(); it++)
108  {
109  m_lastRGBImage.getRawImage()[c++]=*it;
110  }
111  m_lastStamp.update();
112  m_contains_rgb_data = true;
113  }
114  else if (v.encoding == TYPE_16UC1)
115  {
116  m_lastDepthImage.resize(v.width, v.height);
117  size_t c = 0;
118  uint16_t* p = (uint16_t*)(v.data.data());
119  uint16_t* siz = (uint16_t*)(v.data.data()) + (v.data.size() / sizeof(uint16_t));
120  int count = 0;
121  for (; p < siz; p++)
122  {
123  float value = float(*p) / 1000.0;
124  ((float*)(m_lastDepthImage.getRawImage()))[c++] = value;
125  count++;
126  }
127  m_lastStamp.update();
128  m_contains_depth_data = true;
129  }
130  else if (v.encoding == TYPE_32FC1)
131  {
132  m_lastDepthImage.resize(v.width, v.height);
133  size_t c = 0;
134  for (auto it = v.data.begin(); it != v.data.end(); it++)
135  {
136  m_lastDepthImage.getRawImage()[c++] = *it;
137  }
138  m_lastStamp.update();
139  m_contains_depth_data = true;
140  }
141  else
142  {
143  yCError(RGBD_ROS) << "Unsupported rgb/depth format:" << v.encoding;
144  }
145  m_port_mutex.unlock();
146 }
147 
148 bool commonImageProcessor::getFOV(double& horizontalFov, double& verticalFov) const
149 {
150  yarp::rosmsg::sensor_msgs::CameraInfo* tmp = m_subscriber_camera_info.read(true);
151  m_lastCameraInfo = *tmp;
153  params.focalLengthX = m_lastCameraInfo.K[0];
154  params.focalLengthY = m_lastCameraInfo.K[4];
155  params.principalPointX = m_lastCameraInfo.K[2];
156  params.principalPointY = m_lastCameraInfo.K[5];
157  yCError(RGBD_ROS) << "getIntrinsicParam not yet implemented";
158  return false;
159 }
160 
161 bool commonImageProcessor::getIntrinsicParam(yarp::os::Property& intrinsic) const
162 {
163  yarp::rosmsg::sensor_msgs::CameraInfo* tmp = m_subscriber_camera_info.read(true);
164  m_lastCameraInfo = *tmp;
165  intrinsic.clear();
167  params.focalLengthX = m_lastCameraInfo.K[0];
168  params.focalLengthY = m_lastCameraInfo.K[4];
169  params.principalPointX = m_lastCameraInfo.K[2];
170  params.principalPointY = m_lastCameraInfo.K[5];
171  // distortion model
172  if (m_lastCameraInfo.distortion_model=="plumb_bob")
173  {
174  params.distortionModel.type = YarpDistortion::YARP_PLUMB_BOB;
175  params.distortionModel.k1 = m_lastCameraInfo.D[0];
176  params.distortionModel.k2 = m_lastCameraInfo.D[1];
177  params.distortionModel.t1 = m_lastCameraInfo.D[2];
178  params.distortionModel.t2 = m_lastCameraInfo.D[3];
179  params.distortionModel.k3 = m_lastCameraInfo.D[4];
180  }
181  else
182  {
183  yCError(RGBD_ROS) << "Unsupported distortion model";
184  }
185  params.toProperty(intrinsic);
186  return false;
187 }
188 
189 
191 {
192  dest.setPixelCode(src.getPixelCode());
193  dest.setQuantum(src.getQuantum());
194  dest.setExternal(src.getRawImage(), src.width(), src.height());
195 }
196 
198 {
199  dest.setQuantum(src.getQuantum());
200  dest.setExternal(src.getRawImage(), src.width(), src.height());
201 }
202 
205  const std::string& frame_id,
206  const yarp::rosmsg::TickTime& timeStamp,
207  const unsigned int& seq)
208 {
209  dest.data.resize(src.getRawImageSize());
210  dest.width = src.width();
211  dest.height = src.height();
212  memcpy(dest.data.data(), src.getRawImage(), src.getRawImageSize());
214  dest.step = src.getRowSize();
215  dest.header.frame_id = frame_id;
216  dest.header.stamp = timeStamp;
217  dest.header.seq = seq;
218  dest.is_bigendian = 0;
219 }
220 
223  const std::string& frame_id,
224  const yarp::rosmsg::TickTime& timeStamp,
225  const unsigned int& seq)
226 {
227  dest.data.resize(src.getRawImageSize());
228 
229  dest.width = src.width();
230  dest.height = src.height();
231 
232  memcpy(dest.data.data(), src.getRawImage(), src.getRawImageSize());
233 
235  dest.step = src.getRowSize();
236  dest.header.frame_id = frame_id;
237  dest.header.stamp = timeStamp;
238  dest.header.seq = seq;
239  dest.is_bigendian = 0;
240 }
A class for storing options and configuration information.
Definition: Property.h:34
void clear()
Remove all associations.
Definition: Property.cpp:1057
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: CameraInfo.h:371
std::vector< std::uint8_t > data
Definition: Image.h:62
yarp::rosmsg::std_msgs::Header header
Definition: Image.h:56
yarp::rosmsg::TickTime stamp
Definition: Header.h:45
Image class with user control of representation details.
Definition: Image.h:414
void setQuantum(size_t imgQuantum)
Definition: Image.h:429
void setPixelCode(int imgPixelCode)
Definition: Image.h:417
int getPixelCode() const override
Gets pixel type identifier.
Definition: Image.h:775
void setQuantum(size_t imgQuantum)
Definition: Image.cpp:501
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
void setExternal(const void *data, size_t imgWidth, size_t imgHeight)
Use this to wrap an external image.
Definition: Image.cpp:903
size_t getRowSize() const
Size of the underlying image buffer rows.
Definition: Image.h:192
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition: Image.cpp:541
size_t getRawImageSize() const
Access to the internal buffer size information (this is how much memory has been allocated for the im...
Definition: Image.cpp:550
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition: Image.h:199
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
virtual int getPixelCode() const
Gets pixel type identifier.
Definition: Image.cpp:440
#define yCError(component,...)
Definition: LogComponent.h:154
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
@ VOCAB_PIXEL_BGR
Definition: Image.h:52
@ VOCAB_PIXEL_RGB
Definition: Image.h:47
void shallowCopyImages(const yarp::sig::FlexImage &src, yarp::sig::FlexImage &dest)
void deepCopyImages(const yarp::sig::FlexImage &src, yarp::rosmsg::sensor_msgs::Image &dest, const std::string &frame_id, const yarp::rosmsg::TickTime &timeStamp, const unsigned int &seq)
std::string yarp2RosPixelCode(int code)
int Ros2YarpPixelCode(const std::string &roscode)
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22
#define TYPE_16UC1
Definition: rosPixelCode.h:38
#define TYPE_32FC1
Definition: rosPixelCode.h:42
The IntrinsicParams struct to handle the intrinsic parameter of cameras(RGB and RGBD either).
double focalLengthY
Result of the product of the physical focal length(mm) and the size sy of the individual imager eleme...
DistortionModel distortionModel
Distortion model of the image.
double focalLengthX
Result of the product of the physical focal length(mm) and the size sx of the individual imager eleme...
void toProperty(yarp::os::Property &intrinsic) const
toProperty, convert the struct to a Property.
double principalPointX
Horizontal coordinate of the principal point of the image, as a pixel offset from the left edge.
double principalPointY
Vertical coordinate of the principal point of the image, as a pixel offset from the top edge.