-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathground.py
More file actions
368 lines (308 loc) · 14.4 KB
/
Copy pathground.py
File metadata and controls
368 lines (308 loc) · 14.4 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# -*- coding: utf-8 -*-
"""
Automatic Tree Cadastre
This program automatically creates a tree cadastre from a point cloud.
Copyright (c) 2022-2023 Sabine Zagst (s.zagst@tum.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from pathlib import Path
import numpy as np
import open3d as o3d
import matplotlib.pyplot as plt
import time
import CSF
# https://pypi.org/project/cloth-simulation-filter/
def process_ground(
cloud_array: np.ndarray,
mode: str,
step: float = 1.0,
visualize: bool = False,
save: bool = False,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
"""To prepare the point cloud for the next processing steps, the ground points are first
separated from the rest of the point cloud. After that, the height values of the non-ground
points are adjusted depending on the mode.
Parameters
----------
cloud_array : np.ndarray
3D point cloud (x,y,z)
mode : str
Mode to adjust the height values of the non-ground points. 3 available modes:
mean: Set ground as zero level by using the mean-z-value of all ground points.
median: Set ground as zero level by using the median-z-value of all ground points.
ground_grid: Get all non-ground points to the same height level by subtracting the median
value corresponding to the ground grid cell.
step : float, optional
Size of the ground cell in ground_grid mode, by default 1.0 [m]
visualize : bool, optional
If true, resulting point clouds are visualized, by default False
save : bool, optional
If true, saves a plot of the ground grid to Images/, by default False
Returns
-------
tuple[np.ndarray, np.ndarray, np.ndarray]
Array with ground points (x,y,z), array with non-ground points (x,y,z_new) and raster grid
containing the median of the z-values of all points in each raster cell. [x,y]
"""
# Separate ground from point cloud using the Cloth Simulation Filter
start = time.time()
ground_array, nonGround_array = separate_ground(cloud_array, visualize)
end = time.time()
print("CSF took {:5.3f}s to complete.\n".format(end - start), end=" ")
if mode == "mean":
# Set ground as zero level by using the mean-z-value of all ground points
mean = np.mean(ground_array, axis=0, dtype=float)
nonGround_array[:, 2] = nonGround_array[:, 2] - mean[2]
if mode == "median":
# Set ground as zero level by using the median-z-value of all ground points
median = np.mean(ground_array)
nonGround_array[:, 2] = nonGround_array[:, 2] - median[2]
if mode == "ground_grid":
start = time.time()
grid = median_ground_grid(ground_array, nonGround_array, step, save)
nonGround_array = uniform_height_level(nonGround_array, grid, step)
end = time.time()
print("It took {:5.3f}s to get an uniform height level. \n".format(end - start), end=" ")
else:
print("Select a valid ground processing mode.")
# Save and visualize the point cloud with non-ground points after updating its height-values
cloud_updatet = o3d.geometry.PointCloud()
cloud_updatet.points = o3d.utility.Vector3dVector(nonGround_array)
if visualize:
o3d.visualization.draw_geometries([cloud_updatet])
return ground_array, nonGround_array, grid
def separate_ground(
cloud_array: np.ndarray, visualize: bool = False
) -> tuple[np.ndarray, np.ndarray]:
"""Separate ground from point cloud using the Cloth Simulation Filter.
Github Repository: https://github.com/jianboqi/CSF
Parameters
----------
cloud_array : np.ndarray
Point cloud (x,y,z)
visualize : bool, optional
Should the resulting ground be visualized?, by default False
Returns
-------
tuple[np.ndarray, np.ndarray]
Ground array containing the ground points (x,y,z) and
nonGround array containing the remaining points (x,y,z)
"""
csf = CSF.CSF()
csf.setPointCloud(cloud_array)
# Parameter settings
# Source Parameter-Definitions:
# http://ramm.bnu.edu.cn/researchers/wumingzhang/english/default_contributions.htm
csf.params.bSloopSmooth = False # whether the post-processing is needed or not. This is used to handle the steep slopes. (If steep slopes, such as river bank, exists, this value should be set as 1; If the terrain is flat, then this value can be set as 0).
csf.params.time_step = 0.65 # time step for each iteration. Smaller the value is, more accurate the result may be, but more computing-time is needed. 0.65 is applicable to most of situations.
csf.params.class_threshold = 0.5 # a threshold used to classify the original point cloud into ground and non-ground parts based on the distances between original point cloud and the simulated particles of cloth. 0.5 is also adapted to most of situations.
csf.params.cloth_resolution = 0.5 # the horizon space between two particles (i.e., the resolution of cloth grid). This parameters should be smaller than the point spacing of original lidar point cloud. Usually, this can be set as 1/3 or point spacing.
csf.params.rigidness = 3 # greater the value is, harder the cloth will be (usually this value can be 1,2 or 3)
csf.params.interations = 500 # the maximum iteration times of cloth simulation. 500 is enough for most of situations.
# do filtering and get ground and non-ground indexes
groundIndexes = CSF.VecInt()
nonGroundIndexes = CSF.VecInt()
# Interesting question: Where is the Cloth exported to?
csf.do_filtering(groundIndexes, nonGroundIndexes, exportCloth=False)
groundIndexes = np.asarray(groundIndexes)
# necessary to get Array of int32 instead of VecInt object of CSF module
nonGroundIndexes = np.asarray(nonGroundIndexes)
# create clouds as numpy array and open3d point cloud for ground and non-ground points
nonGround_array = np.take(cloud_array, nonGroundIndexes, axis=0)
ground_array = np.take(cloud_array, groundIndexes, axis=0)
nonGround_o3d = o3d.geometry.PointCloud()
ground_o3d = o3d.geometry.PointCloud()
nonGround_o3d.points = o3d.utility.Vector3dVector(nonGround_array)
ground_o3d.points = o3d.utility.Vector3dVector(ground_array)
# output for user
print("CSF done.")
print(f"Number of non-ground points: {nonGround_array.shape[0]}")
print(f"Number of ground points: {ground_array.shape[0]}")
# save pointclouds
# o3d.io.write_point_cloud("ground.pcd", ground_o3d, write_ascii=False)
# o3d.io.write_point_cloud("non-ground.pcd", nonGround_o3d, write_ascii=False)
if visualize:
o3d.visualization.draw_geometries([ground_o3d])
o3d.visualization.draw_geometries([nonGround_o3d])
return ground_array, nonGround_array
def median_ground_grid(
ground_array: np.ndarray, nonGround_array: np.ndarray, step: float, save: bool
) -> np.ndarray:
"""This function rasterizes the ground point cloud in the x-y plane. Each raster cell has
the size step x step [m]. Then, for each raster cell, the median of the z-values of
all points contained in it is calculated.
Parameters
----------
ground_array : np.ndarray
Ground point cloud (x,y,z)
nonGround_array : np.ndarray
Point cloud with non-ground points
step : float
Size of a raster cell [m]
save : bool
If true, saves a plot of the ground grid to Images/.
Returns
-------
np.ndarray
Raster grid containing the median of the z-values of all points in each raster cell. [x,y]
"""
# Limits of the non-ground point cloud, since what matters in the end is its extent.
# The points of the non-ground point cloud should be brought to a uniform height level.
xLimits = np.round(
[np.min(nonGround_array[:, 0]), np.max(nonGround_array[:, 0])], 3
)
yLimits = np.round(
[np.min(nonGround_array[:, 1]), np.max(nonGround_array[:, 1])], 3
)
# Initialize variable last-median with median of all ground points.
# Contains later the last calculated median value.
last_median = np.median(ground_array[:, 2])
# Ground raster in x-y-layer
xRaster = np.round(
np.arange(xLimits[0], xLimits[1] + step, step), 3
) # arange excludes stop-value -> + step
yRaster = np.round(np.arange(yLimits[0], yLimits[1] + step, step), 3)
grid = np.full((len(xRaster), len(yRaster)), last_median)
# Fill the grid with the median z-value of the ground points in each grid cell
for i in range(len(xRaster) - 1):
# points with the cell's x-values
xColumn_points = np.round(
ground_array[np.round(ground_array[:, 0], 3) >= xRaster[i], :], 3
)
xColumn_points = xColumn_points[xColumn_points[:, 0] < xRaster[i + 1], :]
for j in range(len(yRaster) - 1):
# points with the cell's x and y-values
xColumn_points = xColumn_points[xColumn_points[:, 1] >= yRaster[j], :]
cell_points = xColumn_points[xColumn_points[:, 1] < yRaster[j + 1], :]
if np.size(cell_points) > 0:
z_median = np.median(cell_points[:, 2])
last_median = z_median
else:
z_median = last_median
grid[i, j] = z_median
c = plt.imshow(
np.rot90(grid, 1, (0, 1)),
cmap="summer",
extent=[xLimits[0], xLimits[1], yLimits[0], yLimits[1]],
)
plt.colorbar(c, label="Höhe [m]")
plt.title("Bodenraster mit medialen Höhenwerten")
plt.xlabel("x [m]")
plt.ylabel("y [m]")
if save:
p = Path("Images/")
p.mkdir(parents=True, exist_ok=True)
plt.savefig(p / "bodenraster_plot.jpg")
print("Plot bodenraster_plot.jpg is saved to Images/.")
plt.show()
plt.close()
return grid
def uniform_height_level(
nonGround_array: np.ndarray, grid: np.ndarray, step: float
) -> np.ndarray:
"""Get all non-ground points to the same height level by subtracting
the median value corresponding to the grid cell.
Parameters
----------
nonGround_array : np.ndarray
Point cloud with non-ground points (x,y,z).
grid : np.ndarray
Raster grid [x,y] containing the median of the z-values of all points in each raster cell.
step : float
Size of a raster cell. [m]
Returns
-------
np.ndarray
Point cloud with non-ground points (x,y,z_new) reduced to the same hight level.
"""
nonGround_updatet = np.zeros((1, 3))
# Limits of the non-ground point cloud, since what matters in the end is its extent.
# The points of the non-ground point cloud should be brought to a uniform height level.
xLimits = np.round(
[np.min(nonGround_array[:, 0]), np.max(nonGround_array[:, 0])], 3
)
yLimits = np.round(
[np.min(nonGround_array[:, 1]), np.max(nonGround_array[:, 1])], 3
)
# Ground raster in x-y-layer
xRaster = np.round(
np.arange(xLimits[0], xLimits[1] + step, step), 3
) # arange excludes stop-value -> + step
yRaster = np.round(np.arange(yLimits[0], yLimits[1] + step, step), 3)
# Get all non-ground points to the same height level by subtracting the
# median value corresponding to the grid cell.
for i in range(len(xRaster) - 1):
# points with the cell's x-values
xColumn_points = np.round(
nonGround_array[np.round(nonGround_array[:, 0], 3) >= xRaster[i], :], 3
)
xColumn_points = xColumn_points[xColumn_points[:, 0] < xRaster[i + 1], :]
for j in range(len(yRaster) - 1):
# points with the cell's x and y-values
xColumn_points = xColumn_points[xColumn_points[:, 1] >= yRaster[j], :]
cell_points = xColumn_points[xColumn_points[:, 1] < yRaster[j + 1], :]
if np.size(cell_points) > 0:
cell_points[:, 2] = cell_points[:, 2] - grid[i, j]
nonGround_updatet = np.concatenate(
(nonGround_updatet, cell_points), axis=0
)
else:
continue
nonGround_updatet = np.delete(nonGround_updatet, 0, 0)
return nonGround_updatet
def transform_back_z(
x_y_coords: np.ndarray,
ground_grid: np.ndarray,
nonGround_array: np.ndarray,
step: float,
) -> np.ndarray:
"""This function determines the matching z-coordinate to the given x- and y-coordinates using the ground grid.
Parameters
----------
x_y_coords : np.ndarray
Array with x- and y-coordinates (x, y) [m]
ground_grid : np.ndarray
Raster grid containing the median of the z-values of all points in each raster cell. [x,y]
nonGround_array : np.ndarray
Point cloud with non-ground points (x,y,z).
step : float
Size of a raster cell [m]
Returns
-------
np.ndarray
3D coordinates (x, y, z) [m]
"""
# Limits of the non-ground point cloud, since what matters in the end is its extent.
xLimits = np.round(
[np.min(nonGround_array[:, 0]), np.max(nonGround_array[:, 0])], 3
)
yLimits = np.round(
[np.min(nonGround_array[:, 1]), np.max(nonGround_array[:, 1])], 3
)
# Ground raster in x-y-layer
xRaster = np.round(
np.arange(xLimits[0], xLimits[1] + step, step), 3
) # arange excludes stop-value -> + step
yRaster = np.round(np.arange(yLimits[0], yLimits[1] + step, step), 3)
# initialize vector with zeros to save z-coords
z_coords = np.zeros((np.size(x_y_coords, axis=0), 1))
# iterate through all x- and y-values and get the fitting z-value from the ground grid
for i, (x, y) in enumerate(x_y_coords):
# search for x-value in xRaster to get the index, which indicates the fitting z-value
# location in the ground grid
idx_x = np.searchsorted(xRaster, x)
# same for y
idx_y = np.searchsorted(yRaster, y)
z_coords[i, 0] = ground_grid[idx_x, idx_y]
# return all three coords together [x, y, z]
coords = np.append(x_y_coords, z_coords, axis=1)
return coords