Subversion Repositories seema-scanner

Rev

Rev 237 | Rev 241 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 jakw 1
function [alignment] = groupwiseOrthogonalProcrustes(P, initialAlign)
2
%groupwiseOrthogonalProcrustes Computes rototranslations to bring N
3
% matched point sets into alignment using non-linear optimization.
4
%
5
% Input:
6
%   P: (Np x Nc) cell array containing point coordinates in matched order.
7
%       empty cells indicate that the corresponding point set does not
8
%       include a point matched to that cluster.
9
%   initialAlignment: a N length struct with fields Rotation (3x3 matrix)  
10
%       and Translation (3-vector) for each of the point sets.
11
%
12
% Output:
13
%   alignment: a struct with fields Rotation (3x3 matrix) and Translation
14
%       (3-vector) for each of the point sets.
15
%
16
 
17
Np = size(P, 1);
18
assert(length(initialAlign) == Np);
19
 
20
xInit = zeros(6*Np, 1);
21
 
237 jakw 22
for k=1:Np
23
    [wi, thetai] = rmat2axis(initialAlign(k).Rotation);
24
    xInit((k-1)*6+1:(k-1)*6+3) = wi*thetai;
213 jakw 25
 
237 jakw 26
    xInit((k-1)*6+4:(k-1)*6+6) = initialAlign(k).Translation;
213 jakw 27
end
28
 
238 jakw 29
options = optimset('Algorithm', 'levenberg-marquardt', 'Display', 'iter-detailed', 'OutputFcn', @outfun, 'MaxIter', 100, 'TolFun', 0, 'TolX', 0);
213 jakw 30
[x, ~, ~] = lsqnonlin(@orthProcFun, xInit, [], [], options);
31
 
32
alignment = struct('Rotation', {}, 'Translation', {});
237 jakw 33
for k=1:Np
34
    wi = x((k-1)*6+1:(k-1)*6+3);
213 jakw 35
    Ri = axis2rmat(wi, norm(wi));
237 jakw 36
    Ti = x((k-1)*6+4:(k-1)*6+6);
37
    alignment(k).Rotation = Ri;
38
    alignment(k).Translation = Ti;
213 jakw 39
end
40
 
41
 
42
    % objective function
43
    function e = orthProcFun(x)
44
 
45
        Np = size(P, 1);
46
        Nc = size(P, 2);
47
 
48
        % transform all points according to current x
49
        Pbar = cell(size(P));
50
        for i=1:Np
51
            wi = x((i-1)*6+1:(i-1)*6+3);
52
            Ri = axis2rmat(wi, norm(wi));
53
            Ti = x((i-1)*6+4:(i-1)*6+6);
54
            for j=1:Nc
55
                if(not(isempty(P{i,j})))
56
                    Pbar{i,j} = P{i,j}*Ri' + Ti';
57
                end
58
            end
59
        end
60
 
61
        % include all pairwise distances, normalizing for each point sets
62
        e = [];
63
        for i=1:Np   
64
            % number of points in current point set
237 jakw 65
            %Npi = size(cat(1, Pbar{i,:}), 1);
213 jakw 66
            for j=1:Nc
219 jakw 67
                % number of points in current cluster
68
                Ncj = size(cat(1, Pbar{:,j}), 1);
213 jakw 69
                if(not(isempty(Pbar{i,j})))
238 jakw 70
                    for l=setxor(1:Np, i)
71
                        if(not(isempty(Pbar{l,j})))
219 jakw 72
                            %e = [e; 1/Npi * norm(Pbar{k,j} - Pbar{i,j})];
238 jakw 73
                            e = [e; sqrt(1/Ncj) * norm(Pbar{l,j} - Pbar{i,j})];
213 jakw 74
                            %e = [e; norm(Pbar{k,j} - Pbar{i,j})];
75
                        end      
76
                    end   
77
                end
78
            end
79
        end
80
 
81
    end
82
 
83
    % output function called at every iteration
84
    function stop = outfun(x, optimValues, ~)
85
        stop = false;
86
    end
87
 
88
end
89
 
90
function [w, theta] = rmat2axis(R)
91
 
219 jakw 92
w = zeros(3, 1);
237 jakw 93
%theta = zeros(1, 1);
213 jakw 94
 
95
[V,D] = eig(R);
96
[~,ix] = min(abs(diag(D)-1)); 
97
 
98
w(:) = V(:,ix); 
99
t = [R(3,2)-R(2,3),R(1,3)-R(3,1),R(2,1)-R(1,2)];
100
 
101
theta = atan2(t*w(:),trace(R(:,:))-1);
102
 
103
if theta<0
104
    theta = -theta(1); 
105
    w(:) = -w(:); 
106
end
107
end
108
 
109
function R = axis2rmat(w, theta)
110
 
111
if(theta > 0.0001)
218 jakw 112
    w = w./norm(w);
213 jakw 113
end
114
 
115
P = w*transpose(w);
116
Q = [0 -w(3) w(2);
117
     w(3) 0 -w(1);
118
     -w(2) w(1) 0];
119
 
120
% using Rodigues' rotation formula
121
R = P + (eye(3) - P)*cos(theta) + Q*sin(theta);
219 jakw 122
 
123
% ensure orthonormal matrix
124
[U,~,V] = svd(R);
125
R = U*V';
126
 
213 jakw 127
end
128
 
129