Upload PointcloudDownSampler.py
Browse files- PointCloudDownsampler.py +110 -0
PointCloudDownsampler.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import open3d as o3d
|
3 |
+
import numpy as np
|
4 |
+
import shutil
|
5 |
+
|
6 |
+
class PointCloudDownsampler:
|
7 |
+
def __init__(self, input_dir, output_dir, temp_dir, N, voxel_start=0.0001, voxel_step=0.0005):
|
8 |
+
self.input_dir = input_dir
|
9 |
+
self.output_dir = output_dir
|
10 |
+
self.temp_dir = temp_dir
|
11 |
+
self.N = N
|
12 |
+
self.voxel_start = voxel_start
|
13 |
+
self.voxel_step = voxel_step
|
14 |
+
|
15 |
+
# Ensure output and temp directories exist
|
16 |
+
if not os.path.exists(self.output_dir):
|
17 |
+
os.makedirs(self.output_dir)
|
18 |
+
if not os.path.exists(self.temp_dir):
|
19 |
+
os.makedirs(self.temp_dir)
|
20 |
+
|
21 |
+
def downsample_point_cloud(self, point_cloud, voxel_size):
|
22 |
+
return point_cloud.voxel_down_sample(voxel_size)
|
23 |
+
|
24 |
+
def process_point_clouds(self):
|
25 |
+
for filename in os.listdir(self.input_dir):
|
26 |
+
if filename.endswith(".pcd") or filename.endswith(".ply"):
|
27 |
+
file_path = os.path.join(self.input_dir, filename)
|
28 |
+
pcd = o3d.io.read_point_cloud(file_path)
|
29 |
+
num_points = len(pcd.points)
|
30 |
+
|
31 |
+
voxel_size = self.voxel_start
|
32 |
+
best_voxel_size = None
|
33 |
+
best_num_points = num_points
|
34 |
+
|
35 |
+
print(f"Processing {filename} with {num_points} points")
|
36 |
+
|
37 |
+
while True:
|
38 |
+
downsampled_pcd = self.downsample_point_cloud(pcd, voxel_size)
|
39 |
+
downsampled_num_points = len(downsampled_pcd.points)
|
40 |
+
|
41 |
+
print(f"Trying voxel size: {voxel_size:.5f} -> {downsampled_num_points} points")
|
42 |
+
|
43 |
+
if downsampled_num_points < self.N:
|
44 |
+
if best_num_points > self.N:
|
45 |
+
print(f"Found optimal voxel size: {best_voxel_size:.5f} with {best_num_points} points")
|
46 |
+
break
|
47 |
+
print(f"Breaking at voxel size {voxel_size:.5f} with {downsampled_num_points} points")
|
48 |
+
break
|
49 |
+
else:
|
50 |
+
best_voxel_size = voxel_size
|
51 |
+
best_num_points = downsampled_num_points
|
52 |
+
|
53 |
+
voxel_size += self.voxel_step
|
54 |
+
|
55 |
+
if best_voxel_size:
|
56 |
+
optimal_pcd = self.downsample_point_cloud(pcd, best_voxel_size)
|
57 |
+
temp_path = os.path.join(self.temp_dir, filename)
|
58 |
+
o3d.io.write_point_cloud(temp_path, optimal_pcd)
|
59 |
+
print(f"Temporarily saved {filename} with {len(optimal_pcd.points)} points to {self.temp_dir}")
|
60 |
+
|
61 |
+
print("-" * 50)
|
62 |
+
|
63 |
+
def random_downsample_point_cloud(self, pcd, target_size):
|
64 |
+
num_points = len(pcd.points)
|
65 |
+
if num_points <= target_size:
|
66 |
+
print(f"No downsampling needed. Point cloud has {num_points} points, which is less than or equal to {target_size}.")
|
67 |
+
return pcd
|
68 |
+
|
69 |
+
indices = np.random.choice(num_points, target_size, replace=False)
|
70 |
+
downsampled_pcd = pcd.select_by_index(indices)
|
71 |
+
|
72 |
+
return downsampled_pcd
|
73 |
+
|
74 |
+
def downsample_all_to_target_size(self):
|
75 |
+
for filename in os.listdir(self.temp_dir):
|
76 |
+
if filename.endswith(".pcd") or filename.endswith(".ply"):
|
77 |
+
file_path = os.path.join(self.temp_dir, filename)
|
78 |
+
try:
|
79 |
+
pcd = o3d.io.read_point_cloud(file_path)
|
80 |
+
original_size = len(pcd.points)
|
81 |
+
|
82 |
+
print(f"Processing {filename}: Original size = {original_size} points")
|
83 |
+
|
84 |
+
downsampled_pcd = self.random_downsample_point_cloud(pcd, self.N)
|
85 |
+
downsampled_size = len(downsampled_pcd.points)
|
86 |
+
|
87 |
+
output_path = os.path.join(self.output_dir, f"{filename}")
|
88 |
+
o3d.io.write_point_cloud(output_path, downsampled_pcd)
|
89 |
+
|
90 |
+
print(f"Downsampled {filename}: New size = {downsampled_size} points")
|
91 |
+
print(f"Saved downsampled point cloud to {output_path}")
|
92 |
+
print("-" * 50)
|
93 |
+
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Failed to process {filename}: {e}")
|
96 |
+
|
97 |
+
# Clean up the temp directory
|
98 |
+
shutil.rmtree(self.temp_dir)
|
99 |
+
print(f"Temporary files in {self.temp_dir} have been deleted.")
|
100 |
+
|
101 |
+
|
102 |
+
input_dir = "/path/to/input/directory"
|
103 |
+
output_dir = "/path/to/output/directory"
|
104 |
+
temp_dir = "/path/to/temp/directory"
|
105 |
+
N = 50000 # Target number of points
|
106 |
+
|
107 |
+
processor = PointCloudDownsampler(input_dir, output_dir, temp_dir, N)
|
108 |
+
processor.process_point_clouds()
|
109 |
+
processor.downsample_all_to_target_size()
|
110 |
+
|