-
Notifications
You must be signed in to change notification settings - Fork 1
/
eigReg.m
182 lines (160 loc) · 6.28 KB
/
eigReg.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
function [T,src2tarEst] = eigReg(srcCloud,tarCloud,overlap,gridStep,Rho)
%This code is the Matlab implimentation of the paper,
%"Fast Descriptors and Correspondence Propagation for Robust Global Point Cloud Registration,"
%IEEE transactions on Image Processing, 2017.
%This code should be used only for academic research.
%any other useage of this code should not be allowed without Author agreement.
% If you have any problem or improvement idea about this code, please
% contact Huan LEI with [email protected].
%% parameter configuration for flann search
params.algorithm = 'kdtree';
params.trees = 8;
params.checks = 64;
srcData = srcCloud.Location';
tarData = tarCloud.Location';
radii = (0.5:0.5:2)*gridStep;
srcCloudDown = pcdownsample(srcCloud, 'gridAverage', Rho);
tarCloudDown = pcdownsample(tarCloud, 'gridAverage', Rho);
srcSeed = srcCloudDown.Location';
tarSeed = tarCloudDown.Location';
%% compute descriptors for seed points in the source point cloud
K = length(radii);
srcIdx = rangesearch(srcData',srcSeed',radii(1));
idxSz = cellfun(@length,srcIdx,'uni',true);
srcIdx = srcIdx(idxSz>10);
srcSeed = srcSeed(:,idxSz>10); %%过滤掉在第一层r里面小于10个点的seed
M = sum(idxSz>10);
idx = num2cell((1:M)');
[s,n] = cellfun(@(x,y)svdCov(x,y,srcData,srcSeed),srcIdx,idx,'uni',false);
s = cell2mat(s);
n = cell2mat(n);
for k = 2:K
srcIdx = rangesearch(srcData',srcSeed',radii(k));
[sk,nk] = cellfun(@(x,y)svdCov(x,y,srcData,srcSeed),srcIdx,idx,'uni',false);
s = [s cell2mat(sk)];
n = [n cell2mat(nk)];
end
s = s';
ds = diff(s); %%
srcDesp = reshape(ds,3*(K-1),[]); %%每一列 代表L-1层的 x,y,z数据
n = mat2cell(n,3*ones(M,1),K);
srcNorm = cellfun(@(x)reshape(x,[],1),n','uni',false);
srcNorm = cell2mat(srcNorm);
%% compute descriptors for seed points in the target point cloud
tarIdx = rangesearch(tarData',tarSeed',radii(1));
idxSz = cellfun(@length,tarIdx,'uni',true);
tarIdx = tarIdx(idxSz>10);
tarSeed = tarSeed(:,idxSz>10);
N = sum(idxSz>10);
idx = num2cell((1:N)');
[s,n] = cellfun(@(x,y)svdCov(x,y,tarData,tarSeed),tarIdx,idx,'uni',false);
s = cell2mat(s);
n = cell2mat(n);
for k = 2:K
tarIdx = rangesearch(tarData',tarSeed',radii(k));
[sk,nk] = cellfun(@(x,y)svdCov(x,y,tarData,tarSeed),tarIdx,idx,'uni',false);
s = [s cell2mat(sk)];
n = [n cell2mat(nk)];
end
s = s';
ds = diff(s);
tarDesp = reshape(ds,3*(K-1),[]);
n = mat2cell(n,3*ones(N,1),K);
tarNorm = cellfun(@(x)reshape(x,[],1),n','uni',false);
tarNorm = cell2mat(tarNorm);
[srcIdx,dist] = flann_search(srcDesp,tarDesp,1,params); % match with descriptors
%% aggregating each pair of correspondence for finding the best match
M = size(srcSeed,2);
N = size(tarSeed,2);
seedIdx = srcIdx;
Err = inf(N,1);
tform = cell(1,N);
ovNum = ceil(overlap*N);
distThr = 0.2/4*length(radii); %ε 3
thetaThr = 10; % 公式9 ε2
threshold = gridStep*gridStep;
for n = 1:N
seed = srcSeed(:,seedIdx(n));
seedNorm = srcNorm(:,seedIdx(n));
% source point cloud
d = bsxfun(@minus,srcSeed,seed);
d = sqrt(sum(d.^2,1)); % distance
inProd = bsxfun(@times,srcNorm,seedNorm);
inProd = inProd(1:3:end,:) + inProd(2:3:end,:) + inProd(3:3:end,:);
theta = real(acosd(inProd)); % inner product 4行 代表 4个范围的值 公式(6)
% target point cloud
r = bsxfun(@minus,tarSeed,tarSeed(:,n));
r = sqrt(sum(r.^2,1)); % distance
inProd = bsxfun(@times,tarNorm,tarNorm(:,n));
inProd = inProd(1:3:end,:) + inProd(2:3:end,:) + inProd(3:3:end,:);
alpha = real(acosd(inProd)); % inner product 公式(8)
IDX = rangesearch(r',d',gridStep/2,'distance','cityblock');
matches = [seedIdx(n) n];
for m = [1:seedIdx(n)-1 seedIdx(n)+1:M]
idx = IDX{m};%find(abs(r-d(m))<gridStep/2);%
if(isempty(idx))
continue;
end
dTheta = bsxfun(@minus,alpha(:,idx),theta(:,m));
dTheta = abs(dTheta);
Tab = dTheta<thetaThr; %%公式9
Tab = sum(Tab,1);
if(all(Tab<size(theta,1)))
continue;
end
sim = mean(dTheta,1);
sim(Tab<size(theta,1)) = inf;
[minSim,ol] = min(sim); % 返回最小值和位置
R = norm(srcDesp(:,m)-tarDesp(:,idx(ol)));
if(minSim<thetaThr && R<distThr)
matches = [matches; m idx(ol)];
end
end
if(size(matches,1)>10) % match到的个数超过10
match_srcSeed = srcSeed(:,matches(:,1));
match_tarSeed = tarSeed(:,matches(:,2));
CS = ransac(double(match_srcSeed),double(match_tarSeed),threshold);
if(sum(CS)<3)
continue;
end
match_srcSeed = match_srcSeed(:,CS);
match_tarSeed = match_tarSeed(:,CS);
[T, Eps] = estimateRigidTransform(match_tarSeed, match_srcSeed);
tarEst = T*[srcSeed;ones(1,M)];
tarEst = tarEst(1:3,:);
tform{n} = T;
[index,dist] = flann_search(tarEst,tarSeed,1,params); % 在tarEst找离tarSeed最近的点? 返回匹配到tarEst的index。 和 距离(平方)
[dist,ind] = sort(dist);
Err(n) = sum( sum( (tarEst(:,index(ind(1:ovNum)))-tarSeed(:,ind(1:ovNum))).^2 ) ); % 寻找匹配的点 (基于ovNum 两个点云有多少重叠)
end
end
[v,idx] = min(Err);
T = tform{idx};
tarEst = T*[srcData;ones(1,length(srcData))];
tarEst = tarEst(1:3,:);
%% trimmed-icp part
%--------------------------------------------------------------------------
[index,dist] = flann_search(tarEst,tarData,1,params);
[dist,ind] = sort(dist);
ovNum = ceil(overlap*length(dist));
tmp = mean(sum((tarEst(:,index(ind(1:ovNum)))-tarData(:,ind(1:ovNum))).^2));
rmsE(1) = sqrt(tmp);
match_srcData = srcData(:,index(ind(1:ovNum)));
match_tarData = tarData(:,ind(1:ovNum));
maxIter = 100; dE = inf; iter = 1;
errThr = 1e-4; rmseThr = 0.001;
while(dE>errThr && iter<maxIter && rmsE(iter)>rmseThr)
[T, Eps] = estimateRigidTransform(match_tarData, match_srcData);
tarEst = T*[srcData;ones(1,length(srcData))];
tarEst = tarEst(1:3,:);
iter = iter + 1;
[index,dist] = flann_search(tarEst,tarData,1,params);
[dist,ind] = sort(dist);
tmp = mean(sum((tarEst(:,index(ind(1:ovNum)))-tarData(:,ind(1:ovNum))).^2));
rmsE(iter) = sqrt(tmp);
match_srcData = srcData(:,index(ind(1:ovNum)));
match_tarData = tarData(:,ind(1:ovNum));
dE = rmsE(iter - 1) - rmsE(iter);
end
%--------------------------------------------------------------------------
src2tarEst = tarEst;