-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_pointcloud,py
More file actions
302 lines (233 loc) · 10.6 KB
/
Copy pathprocess_pointcloud,py
File metadata and controls
302 lines (233 loc) · 10.6 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
#!/usr/bin/env python3
import numpy as np
import argparse
from pathlib import Path
import open3d as o3d
def load_pointcloud(file_path):
"""
Load a pointcloud from a file based on its extension.
Supports .txt, .xyz, .csv, and .ply files.
"""
file_path = Path(file_path)
ext = file_path.suffix.lower()
if ext in ['.txt', '.xyz', '.csv']:
data = np.loadtxt(file_path, delimiter=' ')
points = data[:, :3] # Extract xyz coordinates
extra_data = data[:, 3:] if data.shape[1] > 3 else None
elif ext == '.ply':
try:
pcd = o3d.io.read_point_cloud(str(file_path))
points = np.asarray(pcd.points)
extra_data = np.asarray(pcd.colors) if pcd.has_colors() else None
except Exception as e:
print(f"Error loading PLY file: {e}")
exit(1)
elif ext == '.nvm':
try:
points, colors = parse_nvm_file(file_path)
extra_data = colors
except Exception as e:
print(f"Error loading NVM file: {e}")
exit(1)
else:
raise ValueError(f"Unsupported file extension: {ext}. Supported extensions are .txt, .xyz, .csv, .ply, and .nvm")
return points, extra_data
def parse_nvm_file(nvm_file_path):
"""
Parse a .nvm file and extract the point cloud data.
Args:
nvm_file_path (str): Path to the .nvm file
Returns:
np.ndarray: Array of 3D points
np.ndarray: Array of RGB colors for each point
"""
with open(nvm_file_path, 'r') as f:
lines = f.readlines()
# Check if the file is in NVM format
if not lines[0].strip().startswith('NVM_V3'):
raise ValueError("The file does not appear to be in NVM_V3 format")
# Find where the camera section ends and point data begins
line_idx = 2 # Skip header and empty line
# Handle empty lines or non-integer values
while line_idx < len(lines) and not lines[line_idx].strip():
line_idx += 1
if line_idx >= len(lines):
raise ValueError("File format error: unexpected end of file")
try:
num_cameras = int(lines[line_idx].strip())
except ValueError:
raise ValueError(f"Invalid number of cameras: '{lines[line_idx].strip()}'")
line_idx += num_cameras + 1 # Skip camera data
# Ensure we haven't gone past the end of the file
if line_idx >= len(lines):
raise ValueError("File format error: no point data found")
# Get number of points, handling empty lines
while line_idx < len(lines) and not lines[line_idx].strip():
line_idx += 1
if line_idx >= len(lines):
raise ValueError("File format error: no point count found")
try:
num_points = int(lines[line_idx].strip())
except ValueError:
raise ValueError(f"Invalid number of points: '{lines[line_idx].strip()}'")
line_idx += 1
points = []
colors = []
# Parse each point
for i in range(num_points):
if line_idx + i >= len(lines):
break
line = lines[line_idx + i].strip()
if not line:
continue
parts = line.split()
if len(parts) < 7: # At minimum we need x, y, z, r, g, b
continue
try:
# Extract position
x, y, z = float(parts[0]), float(parts[1]), float(parts[2])
# Extract color
r, g, b = int(parts[3]), int(parts[4]), int(parts[5])
points.append([x, y, z])
colors.append([r/255.0, g/255.0, b/255.0]) # Normalize to [0,1]
except (ValueError, IndexError) as e:
print(f"Warning: Skipping invalid point data: {line}")
continue
return np.array(points), np.array(colors)
def save_pointcloud(file_path, points, extra_data=None):
"""
Save a pointcloud to a file based on its extension.
Supports .txt, .xyz, .csv, and .ply files.
"""
file_path = Path(file_path)
ext = file_path.suffix.lower()
if ext in ['.txt', '.xyz', '.csv']:
if extra_data is not None:
data = np.hstack((points, extra_data))
else:
data = points
np.savetxt(file_path, data, delimiter=' ')
elif ext == '.ply':
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
# Add colors if available
if extra_data is not None and extra_data.shape[1] >= 3:
# Make sure colors are in range [0, 1]
colors = extra_data[:, :3]
if np.max(colors) > 1.0:
colors = colors / 255.0
pcd.colors = o3d.utility.Vector3dVector(colors)
# Save the point cloud
o3d.io.write_point_cloud(str(file_path), pcd)
else:
raise ValueError(f"Unsupported output file extension: {ext}. Supported extensions are .txt, .xyz, .csv, and .ply")
def remove_outliers_statistical(points, extra_data=None, nb_neighbors=20, std_ratio=2.0):
"""
Remove outliers using statistical outlier removal method.
Args:
points (np.ndarray): Array of 3D points
extra_data (np.ndarray): Additional data like colors
nb_neighbors (int): Number of neighbors to consider
std_ratio (float): Standard deviation ratio threshold
Returns:
np.ndarray: Filtered points
np.ndarray: Filtered extra data
"""
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
# Apply statistical outlier removal
cl, ind = pcd.remove_statistical_outlier(nb_neighbors=nb_neighbors, std_ratio=std_ratio)
# Extract inlier points
inlier_points = np.asarray(cl.points)
# Extract corresponding extra data if available
inlier_extra_data = None
if extra_data is not None:
inlier_extra_data = extra_data[ind]
return inlier_points, inlier_extra_data
def remove_outliers_radius(points, extra_data=None, radius=0.5, min_neighbors=2):
"""
Remove outliers using radius outlier removal method.
Args:
points (np.ndarray): Array of 3D points
extra_data (np.ndarray): Additional data like colors
radius (float): Radius of the sphere to search for neighbors
min_neighbors (int): Minimum number of neighbors required to be an inlier
Returns:
np.ndarray: Filtered points
np.ndarray: Filtered extra data
"""
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
# Apply radius outlier removal
cl, ind = pcd.remove_radius_outlier(nb_points=min_neighbors, radius=radius)
# Extract inlier points
inlier_points = np.asarray(cl.points)
# Extract corresponding extra data if available
inlier_extra_data = None
if extra_data is not None:
inlier_extra_data = extra_data[ind]
return inlier_points, inlier_extra_data
def visualize_point_cloud(points, colors=None):
"""
Visualize the point cloud using Open3D.
Args:
points (np.ndarray): Array of 3D points
colors (np.ndarray): Array of RGB colors for each point
"""
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
if colors is not None:
# Make sure colors are in range [0, 1]
if np.max(colors) > 1.0:
colors = colors / 255.0
pcd.colors = o3d.utility.Vector3dVector(colors)
# Create coordinate frame for reference
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0)
# Visualize
o3d.visualization.draw_geometries([pcd, coordinate_frame])
def main():
parser = argparse.ArgumentParser(description='Remove outliers from a point cloud')
parser.add_argument('input_file', help='Path to input point cloud file (.txt, .xyz, .csv, .ply, or .nvm)')
parser.add_argument('--output_file', help='Path to output point cloud file. If not specified, will use input_file_filtered[.ext]')
parser.add_argument('--method', choices=['statistical', 'radius'], default='statistical', help='Outlier removal method')
parser.add_argument('--nb_neighbors', type=int, default=20, help='Number of neighbors for statistical method')
parser.add_argument('--std_ratio', type=float, default=2.0, help='Standard deviation ratio for statistical method')
parser.add_argument('--radius', type=float, default=0.5, help='Radius for radius method')
parser.add_argument('--min_neighbors', type=int, default=2, help='Minimum neighbors for radius method')
parser.add_argument('--visualize', action='store_true', help='Visualize before and after filtering')
args = parser.parse_args()
input_path = Path(args.input_file)
if not input_path.exists():
print(f"Error: Input file {input_path} does not exist")
return
if args.output_file:
output_path = Path(args.output_file)
else:
output_path = input_path.with_name(f"{input_path.stem}_filtered{input_path.suffix}")
print(f"Loading point cloud from {input_path}")
points, extra_data = load_pointcloud(input_path)
print(f"Original point cloud has {len(points)} points")
if args.visualize:
print("Visualizing original point cloud...")
visualize_point_cloud(points, extra_data)
# Apply outlier removal
if args.method == 'statistical':
print(f"Removing outliers using statistical method (nb_neighbors={args.nb_neighbors}, std_ratio={args.std_ratio})...")
filtered_points, filtered_extra_data = remove_outliers_statistical(
points, extra_data, nb_neighbors=args.nb_neighbors, std_ratio=args.std_ratio
)
else: # radius method
print(f"Removing outliers using radius method (radius={args.radius}, min_neighbors={args.min_neighbors})...")
filtered_points, filtered_extra_data = remove_outliers_radius(
points, extra_data, radius=args.radius, min_neighbors=args.min_neighbors
)
print(f"Filtered point cloud has {len(filtered_points)} points")
print(f"Removed {len(points) - len(filtered_points)} outliers ({(len(points) - len(filtered_points)) / len(points) * 100:.2f}%)")
if args.visualize:
print("Visualizing filtered point cloud...")
visualize_point_cloud(filtered_points, filtered_extra_data)
print(f"Saving filtered point cloud to {output_path}")
save_pointcloud(output_path, filtered_points, filtered_extra_data)
print("Done")
if __name__ == "__main__":
main()