-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsamplePointCloudGranual.py
More file actions
219 lines (190 loc) · 7.97 KB
/
subsamplePointCloudGranual.py
File metadata and controls
219 lines (190 loc) · 7.97 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
import numpy as np
import graphfilter
import open3d as o3d
import argparse
from utils import read_ply, write_ply
import time
import ctypes
from numba import njit, prange
# 定义并行化函数
@njit(parallel=True)
def compute_score_wrapper(cloud):
lib = ctypes.CDLL(
'/DATA_EDS2/zhuzx/Work/3dgs-downsample-backbone/FRPC/graphScore/graphfilter.so')
# 定义C++函数的参数和返回值类型
lib.ctypes_computeScore.argtypes = [ctypes.py_object]
lib.ctypes_computeScore.restype = ctypes.POINTER(ctypes.c_double)
result = np.empty(cloud.shape[0], dtype=np.float64)
for i in prange(cloud.shape[0]):
result[i] = lib.ctypes_computeScore(cloud[i])
# lib.free(result)
return result
def testNumbaJit(points=None,
downSampleRate=0.1,
weightGraph=0.8):
'''
Args:
points: np.array (N,m)
downSampleRate: float, if downSampleRate is below 0.001, use uniform strategy
weightGraph: float
'''
if points is None:
raise ValueError("points must not be None")
if points.shape[0] == 0:
raise ValueError("points must not be None")
szOrg = points.shape[0]
if downSampleRate == 1:
return np.arange(points.shape[0])
else:
approach = 'uniform' if downSampleRate < 0.001 else 'graph'
if approach == 'graph':
t0 = time.time()
# 调用numba进行加速
result = compute_score_wrapper(points[None, :, :3])
# 将返回的Python对象转换为NumPy数组
scores = np.ctypeslib.as_array(result[0], shape=(points.shape[0],))
# scores = graphfilter.computeScore(points[:, :3])
print('graphfilter.computeScore cost: ', time.time() - t0, 'seconds')
score = scores / np.sum(scores)
score = weightGraph * score + (1 - weightGraph) * np.ones(szOrg) / szOrg
idx = np.random.choice(szOrg,
int(szOrg * downSampleRate),
replace=False,
p=score)
else:
idx = np.random.choice(szOrg, int(szOrg * downSampleRate), replace=False)
return idx
'''
graphfilter.computeScore API:
Args:
points: np.array (N,m), m >=3,
make sure xyz coordinates is the first three elements
'''
def getSampledIndex(points=None,
downSampleRate=0.1,
weightGraph=0.8):
'''
Args:
points: np.array (N,m)
downSampleRate: float, if downSampleRate is below 0.001, use uniform strategy
weightGraph: float
'''
if points is None:
raise ValueError("points must not be None")
if points.shape[0] == 0:
raise ValueError("points must not be None")
szOrg = points.shape[0]
if downSampleRate == 1:
return np.arange(points.shape[0])
else:
approach = 'uniform' if downSampleRate < 0.001 else 'graph'
if approach == 'graph':
# 加载共享库
lib = ctypes.CDLL(
'/DATA_EDS2/zhuzx/Work/3dgs-downsample-backbone/FRPC/graphScore/graphfilter.so')
# import pdb; pdb.set_trace()
# 定义C++函数的参数和返回值类型
lib.ctypes_computeScore.argtypes = [ctypes.py_object]
lib.ctypes_computeScore.restype = ctypes.POINTER(ctypes.c_double)
t0 = time.time()
# 调用C++函数
# points = points.astype(np.float64)
result = lib.ctypes_computeScore(points[:, :3])
# 将返回的Python对象转换为NumPy数组
scores = np.ctypeslib.as_array(result, shape=(points.shape[0],))
# scores = graphfilter.computeScore(points[:, :3])
print('graphfilter.computeScore cost: ', time.time() - t0, 'seconds')
import pdb; pdb.set_trace()
score = scores / np.sum(scores)
score = weightGraph * score + (1 - weightGraph) * np.ones(szOrg) / szOrg
idx = np.random.choice(szOrg,
int(szOrg * downSampleRate),
replace=False,
p=score)
lib.free(result)
else:
idx = np.random.choice(szOrg, int(szOrg * downSampleRate), replace=False)
return idx
def subsamplePointCloudGranual(coords, attIn, normalIn, density_steps, density_idx, weightGraph):
output = []
attOut = []
normalOut = []
szOrg = coords.shape[0]
if szOrg == 0:
return output, attOut, normalOut
approach = 'uniform' if weightGraph < 0.001 else 'graph'
density_steps = np.array([0] + density_steps)
density_idx += 1
density_steps = np.floor(density_steps * szOrg).astype(int)
if approach == 'graph':
score = graphfilter.computeScore(coords)
score = score / np.sum(score)
print(score)
score = weightGraph * score + (1 - weightGraph) * np.ones(szOrg) / szOrg
idxPool = np.arange(szOrg)
idxSampled = []
for i in range(1, density_idx+1):
szPool = idxPool.size
if density_steps[i] - density_steps[i - 1] == szPool:
idx = np.arange(szPool)
else:
if approach == 'uniform':
idx = np.random.choice(szPool, density_steps[i] - density_steps[i-1], replace=False)
elif approach == 'graph':
idx = np.random.choice(szPool, density_steps[i] - density_steps[i-1], replace=False, p=score)
else:
print(f'The approach {approach} is not supported')
idx = []
idxSampled.extend(idxPool[idx])
idxPool = np.delete(idxPool, idx)
if approach == 'graph':
score = np.delete(score, idx)
score = score / np.sum(score)
idxSampled = np.sort(idxSampled)
output = coords[idxSampled, :]
if isinstance(attIn, list):
if len(attIn) != 0:
attOut = attIn[idxSampled, :]
else:
attOut = None
if isinstance(normalIn, list):
if len(normalIn) != 0:
normalOut = normalIn[idxSampled, :]
else:
normalOut = None
return output, attOut, normalOut
def visual(pc, sampled_pc):
# set differnet colors for sampled points
pc = np.concatenate((pc, np.zeros((pc.shape[0], 1))), axis=1)
sampled_pc = np.concatenate((sampled_pc, np.ones((sampled_pc.shape[0], 1))), axis=1)
pc = np.concatenate((pc, sampled_pc), axis=0)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(pc[:, :3])
pcd.colors = o3d.utility.Vector3dVector(pc[:, 3:])
o3d.visualization.draw_geometries([pcd])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("path_ply", help="path to ply file")
parser.add_argument("-r", "--sample_rate", default=0.1, type=float, help="number of samples")
parser.add_argument("-s", "--save", default="", help="path to save output ply file")
parser.add_argument("-m", "--mode", default="FPRC", help="sparse or dense")
args = parser.parse_args()
t1 = time.time()
pcd_origin, pc = read_ply(args.path_ply)
print('Time taken to read ply: ', time.time() - t1, 'seconds')
num_pts = pc.shape[0]
sample_rate = args.sample_rate
print('Processing {} points with sample rate of {}'.format(num_pts, sample_rate))
t1 = time.time()
if args.mode == "FPRC":
idxs = getSampledIndex(pc, sample_rate, 1.0)
print('Time taken to sample: ', time.time() - t1, 'seconds')
sampled_pc = pcd_origin[idxs]
elif args.mode == "random":
sampled_pc = pcd_origin[np.random.choice(num_pts, int(np.floor(sample_rate*num_pts)), replace=False)]
print('Time taken to sample: ', time.time() - t1, 'seconds')
if args.save != "":
write_ply(args.save, sampled_pc)
print('Write to {} successfully!'.format(args.save))
else:
visual(pc, sampled_pc.cpu().numpy())