-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain__TSDF_Integrate__depth_only.py
214 lines (157 loc) · 8.92 KB
/
main__TSDF_Integrate__depth_only.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Created on Fri Mar 13 12:28:52 2020
@author: Margarita Chizh
Based on Open3D Tutorial: http://www.open3d.org/docs/release/tutorial/Advanced/rgbd_integration.html
# Open3D: www.open3d.org
# The MIT License (MIT)
# See license file or visit www.open3d.org for details
"""
import open3d as o3d
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
pic_num = 1
#==============================================================================
#==============================================================================
file_name = 'A_hat1' # ! - prefix of files names in Test_data folder
# PREPARE FRAMES NUMBERS:
num_Frames = 132 # ! - number of Frames in Test_data folder
skip_N_frames = 10 # ! - Can choose the range of integrated Frames - less frames = faster run
frames_nums = np.arange(0, num_Frames+1, skip_N_frames)
#==============================================================================
#==============================================================================
# == ICP functions: ==
# check: http://www.open3d.org/docs/release/tutorial/Basic/icp_registration.html
def draw_registration_result(source, target, transformation, title='Title'):
source_temp = source
target_temp = target
source_temp.paint_uniform_color([1, 0.706, 0])
target_temp.paint_uniform_color([0, 0.651, 0.929])
source_temp.transform(transformation)
o3d.visualization.draw_geometries([source_temp, target_temp], window_name=title)
def preprocess_point_cloud(pcd, voxel_size):
# print("\n- - Preprocessing - -\n:: Downsample with a voxel size %.3f." % voxel_size)
pcd_down = pcd.voxel_down_sample(voxel_size)
radius_normal = voxel_size * 2
# print(":: Estimate normal with search radius %.3f." % radius_normal)
pcd_down.estimate_normals(
o3d.geometry.KDTreeSearchParamHybrid(radius=radius_normal, max_nn=30))
radius_feature = voxel_size * 5
# print(":: Compute FPFH feature with search radius %.3f." % radius_feature)
pcd_fpfh = o3d.registration.compute_fpfh_feature(
pcd_down,
o3d.geometry.KDTreeSearchParamHybrid(radius=radius_feature, max_nn=100))
return pcd_down, pcd_fpfh
def execute_global_registration(source_down, target_down, source_fpfh,
target_fpfh, voxel_size):
distance_threshold = voxel_size * 1.5
# print("\n- - Global registration - -\n:: RANSAC registration on downsampled point clouds.")
result = o3d.registration.registration_ransac_based_on_feature_matching(
source_down, target_down, source_fpfh, target_fpfh, distance_threshold,
o3d.registration.TransformationEstimationPointToPoint(False), 4, [
o3d.registration.CorrespondenceCheckerBasedOnEdgeLength(0.9),
o3d.registration.CorrespondenceCheckerBasedOnDistance(
distance_threshold)
], o3d.registration.RANSACConvergenceCriteria(4000000, 500))
return result
def refine_registration(source, target, voxel_size):
distance_threshold = voxel_size * 0.4
# print("\n - - Refine - -\n:: Point-to-plane ICP registration is applied on original point")
radius_normal = voxel_size * 2
source.estimate_normals(
o3d.geometry.KDTreeSearchParamHybrid(radius=radius_normal, max_nn=30))
target.estimate_normals(
o3d.geometry.KDTreeSearchParamHybrid(radius=radius_normal, max_nn=30))
result = o3d.registration.registration_icp(
source, target, distance_threshold, result_ransac.transformation,
o3d.registration.TransformationEstimationPointToPlane())
return result
#==============================================================================
##=============## TSDF PARAMETERS ##=============##
#==============================================================================
voxel_size = 0.01 # 1cm
trunc = np.inf # Maximum depth limit. The Test depth frames were already truncated during Subject segmentation.
#==============================================================================
##=============## INTRINSICS ##=============##
#==============================================================================
# For Intel RealSense to get Intrinsics use command: color_frame.profile.as_video_stream_profile().intrinsics
# Intrinsics:
width = 1280
height = 720
fx = 920.003
fy = 919.888
cx = 640.124
cy = 358.495
#scale = 0.0010000000474974513 #converts [mm] to [meters]
# Create intrinsics matrix in the necessary format:
cameraIntrinsics = o3d.camera.PinholeCameraIntrinsic(width, height, fx, fy, cx, cy)
# print(cameraIntrinsics.intrinsic_matrix)
#==============================================================================
##=============## /ODOMETRY/ ICP ##=============##
#==============================================================================
# Instead of using Odometry, which is not accurate, we use ICP
# to find transformation of one frame into the next frame
num_of_poses = len(frames_nums)
# == Create a trajectory .log file: ==
from trajectory_io import *
metadata = [0, 0, 0]
traj = []
transform_0 = np.identity(4) # The first pose is the reference - we save identity transform matrix
traj.append(CameraPose(metadata, transform_0))
for i in range(num_of_poses-1):
# SOURCE frame:
depth_source = o3d.io.read_image("Test_data/%s_depth_frame%s.png"%(file_name, frames_nums[i]))
source = o3d.geometry.PointCloud.create_from_depth_image(depth_source, cameraIntrinsics)
source_down, source_fpfh = preprocess_point_cloud(source, voxel_size)
# TARGET frame:
depth_target = o3d.io.read_image("Test_data/%s_depth_frame%s.png"%(file_name, frames_nums[i+1]))
target = o3d.geometry.PointCloud.create_from_depth_image(depth_target, cameraIntrinsics)
target_down, target_fpfh = preprocess_point_cloud(target, voxel_size)
#----------------------------------------------------------------------
result_ransac = execute_global_registration(source_down, target_down,
source_fpfh, target_fpfh, voxel_size)
result_icp = refine_registration(source, target, voxel_size)
## Append to camera poses:
transform = result_icp.transformation
transform = np.dot(transform, transform_0)
transform_0 = transform
traj.append(CameraPose(metadata, transform))
# == Generate .log file from ICP transform: ==
write_trajectory(traj, "test_segm__DepthOnly.log")
camera_poses = read_trajectory("test_segm__DepthOnly.log")
#==============================================================================
##=============## TSDF VOLUME INTEGRATION ##=============##
#==============================================================================
# == TSDF volume integration: ==
volume = o3d.integration.ScalableTSDFVolume(
voxel_length = 0.01, # meters # ~ 1cm
sdf_trunc = 0.05, # meters # ~ several voxel_lengths
color_type = o3d.integration.TSDFVolumeColorType.NoColor)
print('\nnumber of camera_poses:', len(camera_poses),'\n')
# Passing zero array intead of Color frames (because 'RGBDImage.create_from_color_and_depth' expects Color anyway)
color_zero_arr = np.zeros((height, width))
color_zero = o3d.geometry.Image((color_zero_arr).astype(np.float32))
num_cam_pose = 0
for i in frames_nums:
print("Integrate %s-th image into the volume."%i)
depth = o3d.io.read_image("Test_data/%s_depth_frame%s.png"%(file_name, i))
rgbd = o3d.geometry.RGBDImage.create_from_color_and_depth(
color=color_zero, # !
depth=depth,
depth_trunc=trunc, # truncate the depth at z =
convert_rgb_to_intensity=True)
volume.integrate(rgbd, cameraIntrinsics, camera_poses[num_cam_pose].pose)
num_cam_pose +=1
#==============================================================================
##=============## TRIANGULAR MESH ##=============##
#==============================================================================
# # == Extract a triangle mesh from the volume (with the marching cubes algorithm)
# # and visualize it : ==
mesh = volume.extract_triangle_mesh()
print(mesh.compute_vertex_normals())
mesh.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]]) # flip!
o3d.visualization.draw_geometries([mesh])
# Save the resulting triangle mesh, load it back and visualize:
o3d.io.write_triangle_mesh('Mesh_DepthOnly__%s__every_%sth_%sframes.ply'%(file_name,skip_N_frames,len(camera_poses)), mesh)
meshRead = o3d.io.read_triangle_mesh('Mesh_DepthOnly__%s__every_%sth_%sframes.ply'%(file_name,skip_N_frames,len(camera_poses)))
o3d.visualization.draw_geometries([meshRead])