-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
PointCloudFactory.cpp
164 lines (154 loc) · 6.98 KB
/
PointCloudFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------
#include <Eigen/Dense>
#include "Open3D/Camera/PinholeCameraIntrinsic.h"
#include "Open3D/Geometry/Image.h"
#include "Open3D/Geometry/PointCloud.h"
#include "Open3D/Geometry/RGBDImage.h"
#include "Open3D/Utility/Console.h"
namespace open3d {
namespace {
using namespace geometry;
int CountValidDepthPixels(const Image &depth, int stride) {
int num_valid_pixels = 0;
for (int i = 0; i < depth.height_; i += stride) {
for (int j = 0; j < depth.width_; j += stride) {
const float *p = PointerAt<float>(depth, j, i);
if (*p > 0) num_valid_pixels += 1;
}
}
return num_valid_pixels;
}
std::shared_ptr<PointCloud> CreatePointCloudFromFloatDepthImage(
const Image &depth,
const camera::PinholeCameraIntrinsic &intrinsic,
const Eigen::Matrix4d &extrinsic,
int stride) {
auto pointcloud = std::make_shared<PointCloud>();
Eigen::Matrix4d camera_pose = extrinsic.inverse();
auto focal_length = intrinsic.GetFocalLength();
auto principal_point = intrinsic.GetPrincipalPoint();
int num_valid_pixels = CountValidDepthPixels(depth, stride);
pointcloud->points_.resize(num_valid_pixels);
int cnt = 0;
for (int i = 0; i < depth.height_; i += stride) {
for (int j = 0; j < depth.width_; j += stride) {
const float *p = PointerAt<float>(depth, j, i);
if (*p > 0) {
double z = (double)(*p);
double x = (j - principal_point.first) * z / focal_length.first;
double y =
(i - principal_point.second) * z / focal_length.second;
Eigen::Vector4d point =
camera_pose * Eigen::Vector4d(x, y, z, 1.0);
pointcloud->points_[cnt++] = point.block<3, 1>(0, 0);
}
}
}
return pointcloud;
}
template <typename TC, int NC>
std::shared_ptr<PointCloud> CreatePointCloudFromRGBDImageT(
const RGBDImage &image,
const camera::PinholeCameraIntrinsic &intrinsic,
const Eigen::Matrix4d &extrinsic) {
auto pointcloud = std::make_shared<PointCloud>();
Eigen::Matrix4d camera_pose = extrinsic.inverse();
auto focal_length = intrinsic.GetFocalLength();
auto principal_point = intrinsic.GetPrincipalPoint();
double scale = (sizeof(TC) == 1) ? 255.0 : 1.0;
int num_valid_pixels = CountValidDepthPixels(image.depth_, 1);
pointcloud->points_.resize(num_valid_pixels);
pointcloud->colors_.resize(num_valid_pixels);
int cnt = 0;
for (int i = 0; i < image.depth_.height_; i++) {
float *p = (float *)(image.depth_.data_.data() +
i * image.depth_.BytesPerLine());
TC *pc = (TC *)(image.color_.data_.data() +
i * image.color_.BytesPerLine());
for (int j = 0; j < image.depth_.width_; j++, p++, pc += NC) {
if (*p > 0) {
double z = (double)(*p);
double x = (j - principal_point.first) * z / focal_length.first;
double y =
(i - principal_point.second) * z / focal_length.second;
Eigen::Vector4d point =
camera_pose * Eigen::Vector4d(x, y, z, 1.0);
pointcloud->points_[cnt] = point.block<3, 1>(0, 0);
pointcloud->colors_[cnt++] =
Eigen::Vector3d(pc[0], pc[(NC - 1) / 2], pc[NC - 1]) /
scale;
}
}
}
return pointcloud;
}
} // unnamed namespace
namespace geometry {
std::shared_ptr<PointCloud> CreatePointCloudFromDepthImage(
const Image &depth,
const camera::PinholeCameraIntrinsic &intrinsic,
const Eigen::Matrix4d &extrinsic /* = Eigen::Matrix4d::Identity()*/,
double depth_scale /* = 1000.0*/,
double depth_trunc /* = 1000.0*/,
int stride /* = 1*/) {
if (depth.num_of_channels_ == 1) {
if (depth.bytes_per_channel_ == 2) {
auto float_depth =
ConvertDepthToFloatImage(depth, depth_scale, depth_trunc);
return CreatePointCloudFromFloatDepthImage(*float_depth, intrinsic,
extrinsic, stride);
} else if (depth.bytes_per_channel_ == 4) {
return CreatePointCloudFromFloatDepthImage(depth, intrinsic,
extrinsic, stride);
}
}
utility::PrintDebug(
"[CreatePointCloudFromDepthImage] Unsupported image format.\n");
return std::make_shared<PointCloud>();
}
std::shared_ptr<PointCloud> CreatePointCloudFromRGBDImage(
const RGBDImage &image,
const camera::PinholeCameraIntrinsic &intrinsic,
const Eigen::Matrix4d &extrinsic /* = Eigen::Matrix4d::Identity()*/) {
if (image.depth_.num_of_channels_ == 1 &&
image.depth_.bytes_per_channel_ == 4) {
if (image.color_.bytes_per_channel_ == 1 &&
image.color_.num_of_channels_ == 3) {
return CreatePointCloudFromRGBDImageT<uint8_t, 3>(image, intrinsic,
extrinsic);
} else if (image.color_.bytes_per_channel_ == 4 &&
image.color_.num_of_channels_ == 1) {
return CreatePointCloudFromRGBDImageT<float, 1>(image, intrinsic,
extrinsic);
}
}
utility::PrintDebug(
"[CreatePointCloudFromRGBDImage] Unsupported image format.\n");
return std::make_shared<PointCloud>();
}
} // namespace geometry
} // namespace open3d