RobotsIO
Loading...
Searching...
No Matches
YarpCamera.cpp
1/*
2 * Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#include <RobotsIO/Camera/YarpCamera.h>
9#include <RobotsIO/Utils/ParametersYarpPort.h>
10
11#include <iostream>
12#include <thread>
13
14#include <yarp/cv/Cv.h>
15#include <yarp/eigen/Eigen.h>
16#include <yarp/os/LogStream.h>
17#include <yarp/os/Stamp.h>
18#include <yarp/os/Value.h>
19
20using namespace Eigen;
21using namespace RobotsIO::Camera;
22using namespace RobotsIO::Utils;
23using namespace yarp::cv;
24using namespace yarp::eigen;
25using namespace yarp::os;
26using namespace yarp::sig;
27
28
29YarpCamera::YarpCamera(const std::string& port_prefix, const bool& network_bootstrap)
30{
31 /* Check YARP network. */
32 if (!yarp_.checkNetwork())
33 {
34 throw(std::runtime_error(log_name_ + "::ctor. Error: YARP network is not available."));
35 }
36
37 /* Open rgb input port. */
38 if (!(port_rgb_.open("/" + port_prefix + "/rgb:i")))
39 {
40 std::string err = log_name_ + "::ctor. Error: cannot open rgb input port.";
41 throw(std::runtime_error(err));
42 }
43
44 /* Open depth input port. */
45 if (!(port_depth_.open("/" + port_prefix + "/depth:i")))
46 {
47 std::string err = log_name_ + "::ctor. Error: cannot open depth input port.";
48 throw(std::runtime_error(err));
49 }
50
51 if (network_bootstrap)
52 {
53 /* Read camera parameters from network. */
54 ParametersYarpPort network_parameters("/" + port_prefix + "/camera_parameters:i");
55 while (!(network_parameters.receive_parameters()))
56 {
57 std::this_thread::sleep_for(std::chrono::seconds(1));
58
59 yInfo() << log_name_ + "::ctor. Waiting for camera parameters on port " + "/" + port_prefix + "/dataset/camera_parameters:i";
60 }
61 parameters_ = CameraParameters(network_parameters);
62
63 Camera::initialize();
64
65 /* Log parameters. */
66 std::cout << log_name_ + "::ctor. Camera parameters:" << std::endl;
67 std::cout << log_name_ + " - width: " << parameters_.width() << std::endl;
68 std::cout << log_name_ + " - height: " << parameters_.height() << std::endl;
69 std::cout << log_name_ + " - fx: " << parameters_.fx() << std::endl;
70 std::cout << log_name_ + " - fy: " << parameters_.fy() << std::endl;
71 std::cout << log_name_ + " - cx: " << parameters_.cx() << std::endl;
72 std::cout << log_name_ + " - cy: " << parameters_.cy() << std::endl;
73 }
74}
75
76
77YarpCamera::YarpCamera
78(
79 const std::size_t& width,
80 const std::size_t& height,
81 const double& fx,
82 const double& cx,
83 const double& fy,
84 const double& cy,
85 const std::string& port_prefix,
86 const bool& enable_camera_pose
87) :
88 enable_camera_pose_(enable_camera_pose)
89{
90 /* Check YARP network. */
91 if (!yarp_.checkNetwork())
92 {
93 throw(std::runtime_error(log_name_ + "::ctor. Error: YARP network is not available."));
94 }
95
96 /* Store parameters. */
97 parameters_.width(width);
98 parameters_.height(height);
99 parameters_.fx(fx);
100 parameters_.cx(cx);
101 parameters_.fy(fy);
102 parameters_.cy(cy);
103 parameters_.initialized(true);
104
105 /* Open rgb input port. */
106 if (!(port_rgb_.open("/" + port_prefix + "/rgb:i")))
107 {
108 std::string err = log_name_ + "::ctor. Error: cannot open rgb input port.";
109 throw(std::runtime_error(err));
110 }
111
112 /* Open depth input port. */
113 if (!(port_depth_.open("/" + port_prefix + "/depth:i")))
114 {
115 std::string err = log_name_ + "::ctor. Error: cannot open depth input port.";
116 throw(std::runtime_error(err));
117 }
118
119 /* OPen camera pose input port. */
120 if (enable_camera_pose_)
121 {
122 if (!(port_pose_.open("/" + port_prefix + "/pose:i")))
123 {
124 std::string err = log_name_ + "::ctor. Error: cannot open pose input port.";
125 throw(std::runtime_error(err));
126 }
127 }
128
129 Camera::initialize();
130
131 /* Log parameters. */
132 std::cout << log_name_ + "::ctor. Camera parameters:" << std::endl;
133 std::cout << log_name_ + " - width: " << parameters_.width() << std::endl;
134 std::cout << log_name_ + " - height: " << parameters_.height() << std::endl;
135 std::cout << log_name_ + " - fx: " << parameters_.fx() << std::endl;
136 std::cout << log_name_ + " - fy: " << parameters_.fy() << std::endl;
137 std::cout << log_name_ + " - cx: " << parameters_.cx() << std::endl;
138 std::cout << log_name_ + " - cy: " << parameters_.cy() << std::endl;
139}
140
141YarpCamera::YarpCamera
142(
143 const std::string& data_path,
144 const std::size_t& width,
145 const double& height,
146 const double& fx,
147 const double& cx,
148 const double& fy,
149 const double& cy
150) :
151 Camera(data_path, width, height, fx, cx, fy, cy)
152{
153 Camera::initialize();
154}
155
156
157YarpCamera::~YarpCamera()
158{
159 /* Close ports. */
160 port_rgb_.close();
161
162 port_depth_.close();
163
164 if (enable_camera_pose_)
165 port_pose_.close();
166}
167
168
169std::pair<bool, MatrixXf> YarpCamera::depth(const bool& blocking)
170{
171 ImageOf<PixelFloat>* image_in;
172 image_in = port_depth_.read(blocking);
173
174 if (image_in == nullptr)
175 return std::make_pair(false, MatrixXf());
176
177 Stamp stamp;
178 port_depth_.getEnvelope(stamp);
179 time_stamp_depth_ = stamp.getTime();
180 is_time_stamp_depth_ = true;
181
182 cv::Mat image = yarp::cv::toCvMat(*image_in);
183 Map<Eigen::Matrix<float, Dynamic, Dynamic, Eigen::RowMajor>> float_image(image.ptr<float>(), image.rows, image.cols);
184
185 return std::make_pair(true, float_image);
186}
187
188
189std::pair<bool, Transform<double, 3, Affine>> YarpCamera::pose(const bool& blocking)
190{
191 if (enable_camera_pose_)
192 {
193 yarp::sig::Vector* pose_in = port_pose_.read(blocking);
194
195 if (pose_in != nullptr)
196 {
197 last_camera_pose_ = *pose_in;
198 }
199
200 if (last_camera_pose_.size() == 7)
201 {
202 /* If available, always return the last camera pose. */
204 pose = Translation<double, 3>(Vector3d(last_camera_pose_[0], last_camera_pose_[1], last_camera_pose_[2]));
205 pose.rotate(AngleAxisd(last_camera_pose_[6], Vector3d(last_camera_pose_[3], last_camera_pose_[4], last_camera_pose_[5])));
206
207 return std::make_pair(true, pose);
208 }
209 else
210 {
211 /* Camera pose enabled but not available, return (false, empty). */
212 return std::make_pair(false, Transform<double, 3, Affine>());
213 }
214 }
215 else
216 {
217 /* Camera pose not enabled, always return (true, identity). */
218 return std::make_pair(true, Transform<double, 3, Affine>::Identity());
219 }
220}
221
222
223std::pair<bool, cv::Mat> YarpCamera::rgb(const bool& blocking)
224{
225 ImageOf<PixelRgb>* image_in;
226 image_in = port_rgb_.read(blocking);
227
228 if (image_in == nullptr)
229 return std::make_pair(false, cv::Mat());
230
231 Stamp stamp;
232 port_rgb_.getEnvelope(stamp);
233 time_stamp_rgb_ = stamp.getTime();
234 is_time_stamp_rgb_ = true;
235
236 cv::Mat image = yarp::cv::toCvMat(*image_in);
237 cv::resize(image, image, cv::Size(parameters_.width(), parameters_.height()));
238
239 return std::make_pair(true, image);
240}
241
242
243std::pair<bool, double> YarpCamera::time_stamp_rgb() const
244{
245 return std::make_pair(is_time_stamp_rgb_, time_stamp_rgb_);
246}
247
248
249std::pair<bool, double> YarpCamera::time_stamp_depth() const
250{
251 return std::make_pair(is_time_stamp_depth_, time_stamp_depth_);
252}
std::pair< bool, Eigen::MatrixXf > depth(const bool &blocking) override
Definition: YarpCamera.cpp:169
std::pair< bool, Eigen::Transform< double, 3, Eigen::Affine > > pose(const bool &blocking) override
Definition: YarpCamera.cpp:189