-
Notifications
You must be signed in to change notification settings - Fork 0
/
regsac.m
55 lines (46 loc) · 1.29 KB
/
regsac.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
% regsac is short for point cloud registration using RANSAC
% R: ratation matrix
% t: translation vector
% X1: 3D coordinates of tie points from point cloud 1
% X2: 3D coordinates of tie points from point cloud 2
% nmatches: number of matches
% nsamp: number of samples
% T: inlier threshold
function [R, t, resx, resy, resz, stdx, stdy, stdz] = regsac(X1, X2, nmatches, nsamp, T)
nmax = 0;
inliers = zeros(nmatches,1);
for(i = 1:nsamp)
choice = randperm(nmatches);
TX1 = X1(:, choice(1:3));
TX2 = X2(:, choice(1:3));
[c, TR, Tt] = ralign(TX1, TX2);
Tinliers = zeros(nmatches,1);
Tinliers(1:3) = choice(1:3);
n = 0;
for j = 4:nmatches
if(norm(TR*X1(:,choice(j))+Tt - X2(:,choice(j))) < T)
n = n + 1;
Tinliers(j) = choice(j);
end
end
if(n > nmax)
nmax = n;
inliers = Tinliers;
end
end
inliers = inliers(inliers > 0);
FX1 = X1(:, inliers);
FX2 = X2(:, inliers);
[c, R, t] = ralign(FX1, FX2);
% residual
resmax = zeros(size(FX2));
for j = 1:size(FX2,2)
resmax(:,j) = R*FX1(:,j) + t - FX2(:,j);
end
resx = resmax(1,:);
resy = resmax(2,:);
resz = resmax(3,:);
% std
stdx = sqrt((resx * resx')/length(resx));
stdy = sqrt((resy * resy')/length(resy));
stdz = sqrt((resz * resz')/length(resz));