YARP
Yet Another Robot Platform
fakeDepthCameraDriver.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 
7 
8 #include <yarp/os/LogComponent.h>
9 #include <yarp/os/LogStream.h>
10 #include <yarp/os/Value.h>
11 
12 #include <algorithm>
13 #include <map>
14 #include <cmath>
15 
16 using namespace yarp::dev;
17 using namespace yarp::sig;
18 using namespace yarp::os;
19 
20 namespace {
21 YARP_LOG_COMPONENT(FAKEDEPTHCAMERA, "yarp.device.fakeDepthCamera")
22 }
23 
25  rgb_h(480),
26  rgb_w(640),
27  dep_h(480),
28  dep_w(640),
29  rgb_Vfov(36),
30  rgb_Hfov(50),
31  dep_Vfov(36),
32  dep_Hfov(50),
33  dep_far(6),
34  image(nullptr)
35 {}
36 
38 
40 {
41  Property cfg;
42  cfg.fromString(config.toString());
43  cfg.unput("device");
44  cfg.put("device", "fakeFrameGrabber");
45  testgrabber.open(cfg);
46  testgrabber.view(image);
47 
48  std::vector<std::tuple<double*, std::string, double> > param;
49  param.emplace_back(&rgb_h, "rgb_h", 480.0);
50  param.emplace_back(&rgb_w, "rgb_w", 640.0);
51  param.emplace_back(&dep_h, "rgb_h", 480.0);
52  param.emplace_back(&dep_w, "rgb_w", 640.0);
53  param.emplace_back(&accuracy, "accuracy", 0.001);
54  param.emplace_back(&rgb_Vfov, "rgb_Vfov", 50.0);
55  param.emplace_back(&rgb_Hfov, "rgb_Hfov", 36.0);
56  param.emplace_back(&dep_Vfov, "dep_Vfov", 50.0);
57  param.emplace_back(&dep_Hfov, "dep_Hfov", 36.0);
58  param.emplace_back(&dep_near, "dep_near", 0.2);
59  param.emplace_back(&dep_far, "dep_far", 6.0);
60  for (auto p : param)
61  {
62  if (config.check(std::get<1>(p)))
63  {
64  *std::get<0>(p) = config.find(std::get<1>(p)).asFloat64();
65  }
66  else
67  {
68  *std::get<0>(p) = std::get<2>(p);
69  }
70 
71  }
72 
73  return true;
74 }
75 
77 {
78  return true;
79 }
80 
82 {
83  return image->height();
84 }
85 
87 {
88  return image->width();
89 }
90 
92 {
93  yCWarning(FAKEDEPTHCAMERA) << "getRgbSupportedConfigurations not implemented yet";
94  return false;
95 }
96 
97 bool fakeDepthCameraDriver::getRgbResolution(int &width, int &height)
98 {
99  width = image->width();
100  height = image->height();
101  return true;
102 }
103 
104 bool fakeDepthCameraDriver::setRgbResolution(int width, int height)
105 {
106  return false;
107 }
108 
109 bool fakeDepthCameraDriver::setDepthResolution(int width, int height)
110 {
111  return false;
112 }
113 
114 bool fakeDepthCameraDriver::setRgbFOV(double horizontalFov, double verticalFov)
115 {
116  rgb_Hfov = horizontalFov;
117  rgb_Vfov = verticalFov;
118  return true;
119 }
120 
121 bool fakeDepthCameraDriver::setDepthFOV(double horizontalFov, double verticalFov)
122 {
123  dep_Hfov = horizontalFov;
124  dep_Vfov = verticalFov;
125  return true;
126 }
127 
129 {
130  accuracy = in_accuracy;
131  return true;
132 }
133 
134 bool fakeDepthCameraDriver::getRgbFOV(double &horizontalFov, double &verticalFov)
135 {
136  horizontalFov = rgb_Hfov;
137  verticalFov = rgb_Vfov;
138  return false;
139 }
140 
142 {
143  mirror = false;
144  return true;
145 }
146 
148 {
149  return false;
150 }
151 
153 {
154  intrinsic.put("physFocalLength", 0.5);
155  intrinsic.put("focalLengthX", 512);
156  intrinsic.put("focalLengthY", 512);
157  intrinsic.put("principalPointX", 235);
158  intrinsic.put("principalPointY", 231);
159  intrinsic.put("distortionModel", "plumb_bob");
160  intrinsic.put("k1", 0);
161  intrinsic.put("k2", 0);
162  intrinsic.put("t1", 0);
163  intrinsic.put("t2", 0);
164  intrinsic.put("k3", 0);
165 
166  intrinsic.put("stamp", yarp::os::Time::now());
167  return true;
168 }
169 
171 {
172  return image->height();
173 }
174 
176 {
177  return image->width();
178 }
179 
180 bool fakeDepthCameraDriver::getDepthFOV(double& horizontalFov, double& verticalFov)
181 {
182  horizontalFov = dep_Hfov;
183  verticalFov = dep_Vfov;
184  return false;
185 }
186 
188 {
189  intrinsic.put("physFocalLength", 0.5);
190  intrinsic.put("focalLengthX", 512);
191  intrinsic.put("focalLengthY", 512);
192  intrinsic.put("principalPointX", 235);
193  intrinsic.put("principalPointY", 231);
194  intrinsic.put("distortionModel", "plumb_bob");
195  intrinsic.put("k1", 0);
196  intrinsic.put("k2", 0);
197  intrinsic.put("t1", 0);
198  intrinsic.put("t2", 0);
199  intrinsic.put("k3", 0);
200 
201  intrinsic.put("stamp", yarp::os::Time::now());
202  return true;
203 }
204 
206 {
207  return accuracy;
208 }
209 
210 bool fakeDepthCameraDriver::getDepthClipPlanes(double& nearPlane, double& farPlane)
211 {
212  nearPlane = dep_near;
213  farPlane = dep_far;
214  return true;
215 }
216 
217 bool fakeDepthCameraDriver::setDepthClipPlanes(double nearPlane, double farPlane)
218 {
219  dep_near = nearPlane;
220  dep_far = farPlane;
221  return true;
222 }
223 
225 {
226  mirror = false;
227  return true;
228 }
229 
231 {
232  return false;
233 }
234 
236 {
237  extrinsic.resize(4, 4);
238  extrinsic.zero();
239 
240  extrinsic[0][0] = 1;
241  extrinsic[1][1] = 1;
242  extrinsic[2][2] = 1;
243  extrinsic[3][3] = 1;
244  return true;
245 }
246 
248 {
249  if (!image->getImage(imageof)) {return false;}
250  rgbImage.setPixelCode(VOCAB_PIXEL_RGB);
251  rgbImage.resize(imageof);
252  memcpy((void*)rgbImage.getRawImage(), (void*)imageof.getRawImage(), imageof.getRawImageSize());
253  if (timeStamp) {
254  timeStamp->update(yarp::os::Time::now());
255  }
256  return true;
257 }
258 
260 {
261  if (!image->getImage(imageof)) {return false;}
262  depthImage.resize(imageof);
263  for (size_t i = 0; i < imageof.width(); i++)
264  {
265  for (size_t j = 0; j < imageof.height(); j++)
266  {
267  PixelRgb pix = (*(PixelRgb*)imageof.getPixelAddress(i, j));
268  *(PixelFloat*)depthImage.getPixelAddress(i, j) = (float(pix.b) / 255.0)/3.0 + (float(pix.g) / 255.0) / 3.0 + (float(pix.r) / 255.0) / 3.0;
269  }
270  }
271  if (timeStamp) {
272  timeStamp->update(yarp::os::Time::now());
273  }
274  return true;
275 }
276 
277 bool fakeDepthCameraDriver::getImages(FlexImage& colorFrame, ImageOf<PixelFloat>& depthFrame, Stamp* colorStamp, Stamp* depthStamp)
278 {
279  return getRgbImage(colorFrame, colorStamp) & getDepthImage(depthFrame, depthStamp);
280 }
281 
283 {
284  return RGBD_SENSOR_OK_IN_USE;
285 }
286 
288 {
289  return "no error";
290 }
bool getImages(FlexImage &colorFrame, depthImage &depthFrame, Stamp *colorStamp=nullptr, Stamp *depthStamp=nullptr) override
~fakeDepthCameraDriver() override
bool setDepthAccuracy(double accuracy) override
Set the minimum detectable variation in distance [meter] when possible.
int getRgbHeight() override
Return the height of each frame.
bool setDepthResolution(int width, int height) override
Set the resolution of the depth image from the camera.
bool setRgbMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool getRgbFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the rgb camera.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool getDepthImage(depthImage &depthImage, Stamp *timeStamp=nullptr) override
double getDepthAccuracy() override
Get the minimum detectable variation in distance [meter].
bool getDepthFOV(double &horizontalFov, double &verticalFov) override
Get the field of view (FOV) of the depth camera.
bool getDepthMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool getRgbImage(FlexImage &rgbImage, Stamp *timeStamp=nullptr) override
Get the rgb frame from the device.
bool setRgbResolution(int width, int height) override
Set the resolution of the rgb image from the camera.
bool getRgbIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the rgb camera.
bool close() override
Close the DeviceDriver.
int getDepthWidth() override
Return the height of each frame.
bool getRgbSupportedConfigurations(yarp::sig::VectorOf< yarp::dev::CameraConfig > &configurations) override
Get the possible configurations of the camera.
bool getExtrinsicParam(yarp::sig::Matrix &extrinsic) override
Get the extrinsic parameters from the device.
std::string getLastErrorMsg(Stamp *timeStamp=nullptr) override
Return an error message in case of error.
int getDepthHeight() override
Return the height of each frame.
bool getDepthIntrinsicParam(Property &intrinsic) override
Get the intrinsic parameters of the depth camera.
bool setDepthFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the depth camera.
RGBDSensor_status getSensorStatus() override
Get the surrent status of the sensor, using enum type.
int getRgbWidth() override
Return the width of each frame.
bool setDepthClipPlanes(double nearPlane, double farPlane) override
Set the clipping planes of the sensor.
bool getRgbMirroring(bool &mirror) override
Get the mirroring setting of the sensor.
bool getRgbResolution(int &width, int &height) override
Get the resolution of the rgb image from the camera.
bool getDepthClipPlanes(double &nearPlane, double &farPlane) override
Get the clipping planes of the sensor.
bool setDepthMirroring(bool mirror) override
Set the mirroring setting of the sensor.
bool setRgbFOV(double horizontalFov, double verticalFov) override
Set the field of view (FOV) of the rgb camera.
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:74
virtual int width() const =0
Return the width of each frame.
virtual int height() const =0
Return the height of each frame.
virtual bool getImage(ImageType &image)=0
Get an image from the frame grabber.
bool open(const std::string &txt)
Construct and configure a device by its common name.
Definition: PolyDriver.cpp:140
A class for storing options and configuration information.
Definition: Property.h:34
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1063
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
Definition: Property.cpp:1046
A base class for nested structures that can be searched.
Definition: Searchable.h:66
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
An abstraction for a time stamp and/or sequence number.
Definition: Stamp.h:22
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition: Stamp.cpp:124
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition: Value.cpp:222
Image class with user control of representation details.
Definition: Image.h:414
void setPixelCode(int imgPixelCode)
Definition: Image.h:417
Typed image class.
Definition: Image.h:658
unsigned char * getPixelAddress(size_t x, size_t y) const
Get address of a pixel in memory.
Definition: Image.h:240
size_t width() const
Gets width of image in pixels.
Definition: Image.h:166
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
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:452
size_t height() const
Gets height of image in pixels.
Definition: Image.h:172
A class for a Matrix.
Definition: Matrix.h:43
void zero()
Zero the matrix.
Definition: Matrix.cpp:323
void resize(size_t r, size_t c)
Resize the matrix, if matrix is not empty preserve old content.
Definition: Matrix.cpp:265
Provides:
Definition: Vector.h:119
#define yCWarning(component,...)
Definition: LogComponent.h:143
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:77
@ VOCAB_PIXEL_RGB
Definition: Image.h:47
An interface for the device drivers.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22
float PixelFloat
Floating point pixel type.
Definition: Image.h:583
Packed RGB pixel type.
Definition: Image.h:464
unsigned char g
Definition: Image.h:466
unsigned char r
Definition: Image.h:465
unsigned char b
Definition: Image.h:467