summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/toolbox/toolbox_basic/fact/factor.m
blob: 635c29a52aec1dbb30b0bc0afaa3d57e783f5486 (plain) (blame)
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
%
% Usage:
%
%    [R,t,S] = factor(W)
%
% Function to factor a matrix of input data (W) into the camera
% rotation matrix (R), translation (t), and the shape matrix (S).
% Three-dimensional version. Failure of normalization results in
% empty R and S.

function [R,t,S] = factor(W)

pts = size(W,2);
t = W*ones(pts,1)/pts;
W = W - t*ones(1,pts);

% Use SVD to factor W. 
   [a,b,c] = svd(W,0);
 
smallb = b(1:3,1:3);	% Since W is rank 3, b has only three meaningful values
sqrtb = sqrt(smallb);
Rhat = a(:,1:3) * sqrtb;
Shat = sqrtb * c(:,1:3)';

G = findG(Rhat);

if size(G,1) == 0,
R = [];
S = [];
else
  R = Rhat*G;
  S = inv(G)*Shat;

  % rotation matrix that aligns the reference frame with the first camera
  F = size(R,1)/2;
  R1 = R(1,:);
  R1 = R1/norm(R1);
  R2 = R(F+1,:);
  R2 = R2/norm(R2);
  R3 = cross(R1,R2);
  R3 = R3/norm(R3);
  P = [R1; R2; R3];
  P = P';

  R = R*P;
  S = inv(P)*S;
end