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