-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperquadricSegment.m
277 lines (242 loc) · 7.82 KB
/
superquadricSegment.m
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
function [theta,sigma,Z, scale] = superquadricSegment(point, mergeThre)
% Written by Yuwei Wu @ NUS
% Weixiao Liu @ JHU, NUS
% -------------------------------------------------------------------------
% DESCRIPTION: This algorithm solves for the shape abstraction of a given
% point cloud via nonparametric Bayesian model. By modeling
% the generation of points as observations sampled from an
% infinite mixture of Gaussian Superquadric Taper Models (GSTM).
% Our approach formulates the abstraction as a clustering
% problem, in which: 1) each point is assigned to a cluster via
% the Chinese Restaurant Process (CRP); 2) a primitive representation
% is optimized for each cluster, and 3) a merging post-process
% is incorporated to provide a concise representation.
%
% INPUT: point - point cloud array (3 x N)
%
% OUTPUT: theta - fitted superquadrics parameters
% sigma - noise factor of each fitted superquadric
% Z - cluster assignment of each point
% -------------------------------------------------------------------------
%% default parameter
p0 = 0.2;
alpha = 0.5;
K = 30;
T = 30;
if nargin == 1
mergeThre = 5e-3;
end
para.iterMax = 4;
para.iterMin = 2;
para.tolerance = 5e-3;
para.relative_tolerance = 0.1;
para.iterLSQ_max = 2;
para.max_switch = 2;
para.adaptive_upper = 1;
% change to true if want to visualize each iteration
para.realtime_rendering = false;
%% setup
% scale point cloud
idx = randperm(length(point));
point = point(:,idx);
V = (max(point(1, :)) - min(point(1, :))) * (max(point(2, :)) ...
- min(point(2, :))) * (max(point(3, :)) - min(point( 3, :)));
scale = (200/V)^(1/3);
point = point * scale;
% average distance
point = unique(point', 'rows');
[~, averageDist] = knnsearch(point,point,'K',6);
averageDist = averageDist(:,2:end);
averageDist = mean(averageDist,'all');
point = point';
% init
N = size(point,2);
Z = zeros(N,K);
fixedZ = zeros(N,K);
threshold = 20;
%% initilization
sigma = zeros(1,K);
theta = zeros(K,13);
cost = inf(1,K);
Ik = eye(K);
% Firstly segment the point cloud into K clusters by K-means
rng(1)
pIdx = kmeans(point',K, 'Start','uniform', 'Distance','cityblock');
for i = 1:K
idx = pIdx == i;
Z(idx,:) = repmat(Ik(i,:),sum(idx),1);
sigma(i) = rand;
p = point(:,idx');
theta(i,:) = superquadricInit(p,ones(1,size(p,2)));
end
%% optimization-based Gibbs sampling
for iter = 1:T
%% sample Z
[cost, theta, sigma, Z, fixedZ, K, n, Ik] = ...
split_separate(point, cost, theta, sigma, Z, fixedZ, iter, K, averageDist, threshold, Ik, para);
flag = 0;
[cor,~] = correspondence_new(point, theta, sigma.^2);
P = point;
while ~isempty(P)
n = sum(Z);
p = P(:,1);
pp = find(sum(abs(point-p))==0);
assert(length(pp)==1)
nj = Z(pp,:);
nj_fixed = fixedZ(pp,:);
if sum(nj_fixed) > 0
assert(sum(nj_fixed)==1)
Z(pp,:) = fixedZ(pp,:);
P(:,1) = [];
continue
end
if flag == 0
weight = [(n-nj)/(alpha+N-1).*cor(pp,:) alpha/(alpha+N-1)*p0];
else
weight = [(n(1:end-1)-nj(1:end-1))/(alpha+N-1).*cor(pp,:) (alpha+n(end))/(alpha+N-1)*p0];
end
z = randsample(K+1,1,true,weight);
P(:,1) = [];
if z == K+1 && flag == 0
Z = [Z zeros(N,1)];
fixedZ = [fixedZ zeros(N,1)];
Ik = eye(K+1);
flag = 1;
theta = [theta;zeros(1,13)];
sigma = [sigma rand];
cost = [cost inf];
end
Z(pp,:) = Ik(z,:);
if flag == 1
newPoint = point(:,(Z(:,K+1)==1)');
if length(newPoint) > threshold
[labels,~] = pcsegdist(pointCloud(newPoint'), averageDist*2);
noCluster = mode(labels);
cIdx = labels == noCluster;
if sum(cIdx) > threshold
K = K + 1;
p = newPoint(:,cIdx');
theta(K,:) = superquadricInit(p,ones(1,size(p,2)));
flag = 0;
[cor_new,~] = correspondence_new(point, theta(K,:), sigma(K)^2);
cor = [cor cor_new];
np = newPoint(:,~cIdx');
P = [P, np];
end
end
end
end
n = sum(Z);
assert(sum(n)==N)
K = size(Z,2);
if i <= 10
A = [];
for j = length(n):-1:1
if n(j) < 13
theta(j,:) = [];
sigma(j) = [];
cost(j) = [];
Z(:,j) = [];
fixedZ(:,j) = [];
K = K - 1;
A = [A,j];
end
end
for j = A
n(j) = [];
end
K = size(Z,2);
Ik = eye(K);
else
P = [];
A = [];
NIdx = [];
for j = 1:K
clusterPoint = point(:,(Z(:,j)==1)');
[labels,~] = pcsegdist(pointCloud(clusterPoint'), averageDist*2);
noCluster = mode(labels);
cIdx = labels == noCluster;
if sum(cIdx) <= threshold
P = [P,clusterPoint];
A = [A,j];
else
P = [P,clusterPoint(:,~cIdx)];
end
end
for j = flip(A)
n(j) = [];
theta(j,:) = [];
sigma(j) = [];
cost(j) = [];
Z(:,j) = [];
fixedZ(:,j) = [];
K = K - 1;
end
Ik = eye(K);
for j = 1:size(P,2)
[cor,~] = correspondence_new(P, theta, sigma.^2);
n = sum(Z);
p = P(:,j);
pp = find(sum(abs(point-p))==0);
nj = Z(j,:);
nj_fixed = fixedZ(pp,:);
if sum(nj_fixed) > 0
assert(sum(nj_fixed)==1)
Z(pp,:) = fixedZ(pp,:);
continue
end
weight = (n-nj)/(alpha+N-1).*cor(j,:);
if sum(weight) == 0
continue
end
z = randsample(K,1,true,weight);
Z(pp,:) = Ik(z,:);
end
end
%% optimize parameters of each superquadric
for j = 1:K
if iter > 15
idx = Z(:,j) == 1;
p = point(:,idx');
[labels,~] = pcsegdist(pointCloud(p'), averageDist*2);
noCluster = mode(labels);
cIdx = labels == noCluster;
p = p(:,cIdx');
x0 = superquadricInit(p, ones(1,sum(cIdx)));
para.iterMax = floor(iter/2);
[x, D, ~] = superquadricFitting(p, para, x0);
theta(j,:) = x;
cost(1,j) = D;
notp = find(idx ==1);
Z(notp(~cIdx),j) = 0;
fixedZ(notp(~cIdx),j) = 0;
else
idx = Z(:,j) == 1;
x0 = superquadricInit(point(:,idx'), ones(1,sum(idx)));
[x, D, ~] = superquadricFitting(point(:,idx'), para, x0);
theta(j,:) = x;
cost(1,j) = D;
end
end
c = cost ./ sum(Z);
for fixedIdx = find(c<=5e-3)
fixedZ(:,fixedIdx) = Z(:,fixedIdx);
end
%% sample sigma
for j = 1:K
a = (n(j)-1)/2;
b = 2/cost(j);
r = gamrnd(a,b);
sigma(1,j) = sqrt(1/r);
end
%% merge
if mod(iter,15) == 0
[n,Z,fixedZ,theta,sigma,K,Ik,cost] = superquadricMerge(Z,iter,fixedZ,K,theta,sigma,cost,point,para,mergeThre);
end
if para.realtime_rendering
visualization(Z, K, theta, point, 2, 3, [0 90])
end
end
[n,Z,fixedZ,theta,sigma,K,Ik,cost] = superquadricMerge(Z,iter,fixedZ,K,theta,sigma,cost,point,para, mergeThre);
% visualize result
visualization(Z, K, theta, point, 2, 3, [0 90])