YARP
Yet Another Robot Platform
PointCloudUtils-inl.h
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 #ifndef YARP_SIG_POINTCLOUDUTILS_INL_H
10 #define YARP_SIG_POINTCLOUDUTILS_INL_H
11 
12 #include <type_traits>
13 
14 
15 namespace {
16 
17 template<typename T1,
18  typename T2,
19  std::enable_if_t<std::is_same<T1, yarp::sig::DataXYZRGBA>::value &&
20  (std::is_same<T2, yarp::sig::PixelRgb>::value ||
21  std::is_same<T2, yarp::sig::PixelBgr>::value), int> = 0
22 >
23 inline void copyColorData(yarp::sig::PointCloud<T1>& pointCloud,
24  const yarp::sig::ImageOf<T2>& color,
25  const size_t u,
26  const size_t v)
27 {
28  pointCloud(u,v).r = color.pixel(u,v).r;
29  pointCloud(u,v).g = color.pixel(u,v).g;
30  pointCloud(u,v).b = color.pixel(u,v).b;
31 }
32 
33 template<typename T1,
34  typename T2,
35  std::enable_if_t<std::is_same<T1, yarp::sig::DataXYZRGBA>::value &&
36  (std::is_same<T2, yarp::sig::PixelRgba>::value ||
37  std::is_same<T2, yarp::sig::PixelBgra>::value), int> = 0
38 >
39 inline void copyColorData(yarp::sig::PointCloud<T1>& pointCloud,
40  const yarp::sig::ImageOf<T2>& color,
41  const size_t u,
42  const size_t v)
43 {
44  pointCloud(u,v).r = color.pixel(u,v).r;
45  pointCloud(u,v).g = color.pixel(u,v).g;
46  pointCloud(u,v).b = color.pixel(u,v).b;
47  pointCloud(u,v).a = color.pixel(u,v).a;
48 }
49 
50 template<typename T1,
51  typename T2,
52  std::enable_if_t<!std::is_same<T1, yarp::sig::DataXYZRGBA>::value ||
53  (!std::is_same<T2, yarp::sig::PixelRgb>::value &&
54  !std::is_same<T2, yarp::sig::PixelBgr>::value &&
55  !std::is_same<T2, yarp::sig::PixelRgba>::value &&
56  !std::is_same<T2, yarp::sig::PixelBgra>::value), int> = 0
57 >
58 inline void copyColorData(yarp::sig::PointCloud<T1>& pointCloud,
59  const yarp::sig::ImageOf<T2>& color,
60  size_t u,
61  size_t v)
62 {
63 }
64 
65 } // namespace
66 
67 template<typename T1, typename T2>
69  const yarp::sig::ImageOf<T2>& color,
70  const yarp::sig::IntrinsicParams& intrinsic)
71 {
72  yAssert(depth.width() != 0);
73  yAssert(depth.height() != 0);
74  yAssert(depth.width() == color.width());
75  yAssert(depth.height() == color.height());
76  size_t w = depth.width();
77  size_t h = depth.height();
78  yarp::sig::PointCloud<T1> pointCloud;
79  pointCloud.resize(w, h);
80 
81  for (size_t u = 0; u < w; ++u) {
82  for (size_t v = 0; v < h; ++v) {
83  // Depth
84  // De-projection equation (pinhole model):
85  // x = (u - ppx)/ fx * z
86  // y = (v - ppy)/ fy * z
87  // z = z
88  pointCloud(u,v).x = (u - intrinsic.principalPointX)/intrinsic.focalLengthX*depth.pixel(u,v);
89  pointCloud(u,v).y = (v - intrinsic.principalPointY)/intrinsic.focalLengthY*depth.pixel(u,v);
90  pointCloud(u,v).z = depth.pixel(u,v);
91 
92  copyColorData(pointCloud, color, u, v);
93  }
94  }
95  return pointCloud;
96 }
97 
98 #endif // YARP_SIG_POINTCLOUDUTILS_INL_H
#define yAssert(x)
Definition: Log.h:297
Typed image class.
Definition: Image.h:647
T & pixel(size_t x, size_t y)
Definition: Image.h:663
size_t width() const
Gets width of image in pixels.
Definition: Image.h:153
size_t height() const
Gets height of image in pixels.
Definition: Image.h:159
The PointCloud class.
Definition: PointCloud.h:27
virtual void resize(size_t width, size_t height)
Resize the PointCloud.
Definition: PointCloud.h:64
yarp::sig::PointCloud< T1 > depthRgbToPC(const yarp::sig::ImageOf< yarp::sig::PixelFloat > &depth, const yarp::sig::ImageOf< T2 > &color, const yarp::sig::IntrinsicParams &intrinsic)
depthRgbToPC, compute the colored PointCloud given depth image, color image and the intrinsic paramet...
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...
double focalLengthX
Result of the product of the physical focal length(mm) and the size sx of the individual imager eleme...
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.