YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Ros2RGBDConversionUtils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
9#include <yarp/os/Value.h>
10
11#include <yarp/sig/ImageUtils.h>
12
13#include <algorithm>
14#include <cmath>
15#include <cstdint>
16#include <iomanip>
17
18using namespace yarp::dev;
19using namespace yarp::sig;
20using namespace yarp::os;
22using namespace std;
23
24namespace {
25YARP_LOG_COMPONENT(ROS2_RGBD_CONVERSION_UTILS, "yarp.device.Ros2RGBDConversionUtils");
26}
27
28
30{
31 yarp_stamp.update(ros_header.stamp.sec + (ros_header.stamp.nanosec / ONE_BILLION));
32}
33
34
36{
37 ros_header.stamp.sec = int(yarp_stamp.getTime());
38 ros_header.stamp.nanosec = int(ONE_BILLION * (yarp_stamp.getTime() - int(yarp_stamp.getTime())));
39}
40
43{
47 {
48 dest.setQuantum(0);
50 dest.setPixelSize(ros_image_src->step/ros_image_src->width); // this I think is rowsize
51 dest.resize(ros_image_src->width, ros_image_src->height);
52 size_t c = 0;
53 for (auto it = ros_image_src->data.begin(); it != ros_image_src->data.end(); it++)
54 {
55 dest.getRawImage()[c++]=*it;
56 }
57 }
58 else
59 {
60 yCError(ROS2_RGBD_CONVERSION_UTILS) << "Unsupported rgb format:" << ros_image_src->encoding;
61 }
62
63}
64
65
68{
69 if (ros_image_src->encoding == TYPE_16UC1)
70 {
71 dest.resize(ros_image_src->width, ros_image_src->height);
72 size_t c = 0;
73 uint16_t* p = (uint16_t*)(ros_image_src->data.data());
74 uint16_t* siz = (uint16_t*)(ros_image_src->data.data()) + (ros_image_src->data.size() / sizeof(uint16_t));
75 int count = 0;
76 for (; p < siz; p++)
77 {
78 float value = float(*p) / 1000.0;
79 ((float*)(dest.getRawImage()))[c++] = value;
80 count++;
81 }
82 }
83 else if (ros_image_src->encoding == TYPE_32FC1)
84 {
85 dest.resize(ros_image_src->width, ros_image_src->height);
86 size_t c = 0;
87 for (auto it = ros_image_src->data.begin(); it != ros_image_src->data.end(); it++)
88 {
89 dest.getRawImage()[c++] = *it;
90 }
91 }
92 else
93 {
94 yCError(ROS2_RGBD_CONVERSION_UTILS) << "Unsupported depth format:" << ros_image_src->encoding;
95 }
96}
97
98
99void yarp::dev::Ros2RGBDConversionUtils::updateStamp( sensor_msgs::msg::CameraInfo::SharedPtr ros_camera_info_src,
100 std::string& frame_id_dest,
102{
103 frame_id_dest = ros_camera_info_src->header.frame_id;
104 //how to do with seq??
106}
107
109{
110 dest.setPixelCode(src.getPixelCode());
111 dest.setQuantum(src.getQuantum());
112 dest.resize(src.width(), src.height());
113 for (size_t it = 0; it < src.getRawImageSize(); it++)
114 {
115 dest.getRawImage()[it] = src.getRawImage()[it];
116 }
117}
118
120{
121 dest.setQuantum(src.getQuantum());
122 dest.resize(src.width(), src.height());
123 for (size_t it = 0; it < src.getRawImageSize(); it++)
124 {
125 dest.getRawImage()[it] = src.getRawImage()[it];
126 }
127}
@ VOCAB_PIXEL_BGR
Definition Image.h:49
@ VOCAB_PIXEL_RGB
Definition Image.h:44
constexpr double ONE_BILLION
A mini-server for performing network communication in the background.
An abstraction for a time stamp and/or sequence number.
Definition Stamp.h:21
Image class with user control of representation details.
Definition Image.h:363
void setQuantum(size_t imgQuantum)
Definition Image.h:378
void setPixelCode(int imgPixelCode)
Definition Image.h:366
void setPixelSize(size_t imgPixelSize)
Definition Image.h:371
Typed image class.
Definition Image.h:605
void setQuantum(size_t imgQuantum)
Definition Image.cpp:451
size_t width() const
Gets width of image in pixels.
Definition Image.h:171
unsigned char * getRawImage() const
Access to the internal image buffer.
Definition Image.cpp:479
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:488
void resize(size_t imgWidth, size_t imgHeight)
Reallocate an image to be of a desired size, throwing away its current contents.
Definition Image.cpp:402
size_t getQuantum() const
The size of a row is constrained to be a multiple of the "quantum".
Definition Image.h:204
size_t height() const
Gets height of image in pixels.
Definition Image.h:177
virtual int getPixelCode() const
Gets pixel type identifier.
Definition Image.cpp:390
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
int Ros2ToYarpPixelCode(const std::string &roscode)
void deepCopyImageOf(const DepthImage &src, DepthImage &dest)
void updateStamp(sensor_msgs::msg::CameraInfo::SharedPtr ros_camera_info_src, std::string &frame_id_dest, yarp::os::Stamp &yarp_stamp)
void convertTimeStampRos2ToYarp(const std_msgs::msg::Header &ros_stamp, yarp::os::Stamp &yarp_stamp)
void convertDepthImageRos2ToYarpImageOf(sensor_msgs::msg::Image::SharedPtr ros_image_src, yarp::sig::ImageOf< yarp::sig::PixelFloat > &dest)
void convertRGBImageRos2ToYarpFlexImage(sensor_msgs::msg::Image::SharedPtr ros_image_src, yarp::sig::FlexImage &dest)
void convertTimeStampYarpToRos2(const yarp::os::Stamp &yarp_stamp, std_msgs::msg::Header &ros_stamp)
void deepCopyFlexImage(const yarp::sig::FlexImage &src, yarp::sig::FlexImage &dest)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.
#define TYPE_16UC1
#define TYPE_32FC1