YARP
Yet Another Robot Platform
PointCloudUtils.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 
10 #include <algorithm>
11 #include <cstring>
12 
13 using namespace yarp::sig;
14 
15 namespace {
16 YARP_LOG_COMPONENT(POINTCLOUDUTILS, "yarp.sig.PointCloudUtils")
17 }
18 
20  const yarp::sig::IntrinsicParams &intrinsic)
21 {
22  yCAssert(POINTCLOUDUTILS, depth.width() != 0);
23  yCAssert(POINTCLOUDUTILS, depth.height() != 0);
24  size_t w = depth.width();
25  size_t h = depth.height();
26  PointCloud<DataXYZ> pointCloud;
27  pointCloud.resize(w, h);
28 
29  for (size_t u = 0; u < w; ++u) {
30  for (size_t v = 0; v < h; ++v) {
31  // De-projection equation (pinhole model):
32  // x = (u - ppx)/ fx * z
33  // y = (v - ppy)/ fy * z
34  // z = z
35  pointCloud(u,v).x = (u - intrinsic.principalPointX)/intrinsic.focalLengthX*depth.pixel(u,v);
36  pointCloud(u,v).y = (v - intrinsic.principalPointY)/intrinsic.focalLengthY*depth.pixel(u,v);
37  pointCloud(u,v).z = depth.pixel(u,v);
38  }
39  }
40  return pointCloud;
41 }
42 
44  const yarp::sig::IntrinsicParams& intrinsic,
45  const PCL_ROI& roi,
46  size_t step_x,
47  size_t step_y)
48 {
49  yCAssert(POINTCLOUDUTILS, depth.width() != 0);
50  yCAssert(POINTCLOUDUTILS, depth.height() != 0);
51 
52  size_t max_x = roi.max_x == 0 ? depth.width() : std::min(roi.max_x, depth.width());
53  size_t max_y = roi.max_y == 0 ? depth.height() : std::min(roi.max_y, depth.height());
54  size_t min_x = std::min(roi.min_x, max_x);
55  size_t min_y = std::min(roi.min_y, max_y);
56  // avoid step larger than ROI and division by zero
57  step_x = std::max<size_t>(std::min(step_x, max_x - min_x), 1);
58  step_y = std::max<size_t>(std::min(step_y, max_y - min_y), 1);
59 
60  PointCloud<DataXYZ> pointCloud;
61  size_t size_x = (max_x - min_x) / step_x;
62  size_t size_y = (max_y - min_y) / step_y;
63  pointCloud.resize(size_x, size_y);
64 
65  for (size_t i = 0, u = min_x; u < max_x; i++, u += step_x) {
66  for (size_t j = 0, v = min_y; v < max_y; j++, v += step_y) {
67  // De-projection equation (pinhole model):
68  // x = (u - ppx)/ fx * z
69  // y = (v - ppy)/ fy * z
70  // z = z
71  pointCloud(i, j).x = (u - intrinsic.principalPointX) / intrinsic.focalLengthX * depth.pixel(u, v);
72  pointCloud(i, j).y = (v - intrinsic.principalPointY) / intrinsic.focalLengthY * depth.pixel(u, v);
73  pointCloud(i, j).z = depth.pixel(u, v);
74  }
75  }
76  return pointCloud;
77 }
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::sig
Signal processing.
Definition: Image.h:25
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
yarp::sig::utils::PCL_ROI::min_y
size_t min_y
Definition: PointCloudUtils.h:30
yarp::sig::utils::PCL_ROI
Definition: PointCloudUtils.h:27
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::utils::PCL_ROI::max_x
size_t max_x
Definition: PointCloudUtils.h:29
yarp::sig::utils::PCL_ROI::max_y
size_t max_y
Definition: PointCloudUtils.h:31
yarp::sig::ImageOf
Typed image class.
Definition: Image.h:647
yarp::sig::PointCloud
The PointCloud class.
Definition: PointCloud.h:27
yarp::sig::PointCloud::resize
virtual void resize(size_t width, size_t height)
Resize the PointCloud.
Definition: PointCloud.h:64
yarp::sig::utils::depthToPC
yarp::sig::PointCloud< yarp::sig::DataXYZ > depthToPC(const yarp::sig::ImageOf< yarp::sig::PixelFloat > &depth, const yarp::sig::IntrinsicParams &intrinsic)
depthToPC, compute the PointCloud given depth image and the intrinsic parameters of the camera.
Definition: PointCloudUtils.cpp:19
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::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::sig::utils::PCL_ROI::min_x
size_t min_x
Definition: PointCloudUtils.h:28
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yarp::sig::ImageOf::pixel
T & pixel(size_t x, size_t y)
Definition: Image.h:663
yarp::sig::IntrinsicParams
The IntrinsicParams struct to handle the intrinsic parameter of cameras(RGB and RGBD either).
Definition: IntrinsicParams.h:44
PointCloudUtils.h