source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/BlueCCal/MultiCamSelfCal/CoreFunctions/euclidize.m @ 86

Last change on this file since 86 was 37, checked in by (none), 15 years ago

Added original make3d

File size: 4.5 KB
Line 
1% euclidize ... perform euclidian reconstruction
2%               under assumption of unknown focal lengths, const. principal points = 0,
3%               and aspect ratio = 1
4%
5% [Pe,Xe,C,Rot] = euclidize(Ws,Lambda,P,X,config)
6%
7% n is the number of cameras and m is the number of points
8%
9% Ws ....... 3*nxm measurement matrix
10% Lambda ... nxm matrix containing the projective depths
11% P ........ 3*nx4 projective motion matrix
12% X ........ 4xm projective shape matrix
13% config ... see the CONFIGDATA
14%            .cal.pp and .cal.SQUARE_PIX are expected
15%
16% Pe ....... 3*nx4 euclidian motion matrix
17% Xe ....... 4xm euclidian shape matrix
18% C ........ 4xn matrix containg the camera centers
19% Rot ...... 3*nx3 matrix containing the camera rotation matrices
20
21% $Author: svoboda $
22% $Revision: 2.1 $
23% $Id: euclidize.m,v 2.1 2003/07/09 14:40:48 svoboda Exp $
24% $State: Exp $
25
26function [Pe,Xe,C,Rot] = euclidize(Ws,Lambda,P,X,config)
27n = size(Ws,1)/3; % number of cameras
28m = size(Ws,2);   % number of points
29
30% compute B
31a = []; b = []; c = [];
32for i = 1:n
33  a = [a; sum(Ws(3*i-2,:).*Lambda(i,:))];
34  b = [b; sum(Ws(3*i-1,:).*Lambda(i,:))];
35  c = [c; sum(Lambda(i,:))];
36end
37TempA = -P(3:3:3*n, :);
38TempB = -P(3:3:3*n, :);
39for i = 1:n
40  TempA(i, :) = TempA(i, :)*a(i)/c(i);
41  TempB(i, :) = TempB(i, :)*b(i)/c(i);
42end
43TempA = TempA + P(1:3:3*n, :);
44TempB = TempB + P(2:3:3*n, :);
45Temp = [TempA; TempB];
46[U,S,V] = svd(Temp,0);
47B = V(:,4); % least square solution (of Temp*B == 0)
48
49% compute A
50%
51% M * M^T == P * Q *P^T, thus
52%
53% ( m_x )                  ( P1 )
54% ( m_y )*(m_x m_y m_z) == ( P2 ) * Q * (P1 P2 P3)   (let Pi denote the i-th row of P), thus
55% ( m_z )                  ( P3 )
56%
57% ( |m_x|^2  m_x*m_y  m_x*m_z )    ( P1*Q*P1^T P1*Q*P2^T P1*Q*P3^T )
58% (  .       |m_y|^2  m_y*m_z ) == ( .         P2*Q*P2^T P2*Q*P3^T )
59% (  .       .......  |m_z|^2 )    ( .         ......... P3*Q*P3^T )
60%
61Temp = []; b = [];
62for i = 1:n
63  P1 = P(3*i-2,:); % 1st row of i-th camera
64  P2 = P(3*i-1,:); % 2nd row of i-th camera
65  P3 = P(3*i,  :); % 3rd row of i-th camera
66  u = P1; v = P2;
67  Temp = [Temp; u(1)*v(1) u(1)*v(2)+u(2)*v(1) u(3)*v(1)+u(1)*v(3) u(1)*v(4)+u(4)*v(1) u(2)*v(2) u(2)*v(3)+u(3)*v(2) u(2)*v(4)+u(4)*v(2) u(3)*v(3) u(4)*v(3)+u(3)*v(4) u(4)*v(4)];
68  if config.cal.SQUARE_PIX
69          Temp = [Temp; u(1)^2-v(1)^2 2*u(1)*u(2)-2*v(1)*v(2) 2*u(1)*u(3)-2*v(1)*v(3) 2*u(1)*u(4)-2*v(1)*v(4) u(2)^2-v(2)^2 2*u(2)*u(3)-2*v(2)*v(3) 2*u(2)*u(4)-2*v(2)*v(4) u(3)^2-v(3)^2 2*u(3)*u(4)-2*v(3)*v(4) u(4)^2-v(4)^2];
70  end
71  u = P1; v = P3;
72  Temp = [Temp; u(1)*v(1) u(1)*v(2)+u(2)*v(1) u(3)*v(1)+u(1)*v(3) u(1)*v(4)+u(4)*v(1) u(2)*v(2) u(2)*v(3)+u(3)*v(2) u(2)*v(4)+u(4)*v(2) u(3)*v(3) u(4)*v(3)+u(3)*v(4) u(4)*v(4)];
73  u = P2; v = P3;
74  Temp = [Temp; u(1)*v(1) u(1)*v(2)+u(2)*v(1) u(3)*v(1)+u(1)*v(3) u(1)*v(4)+u(4)*v(1) u(2)*v(2) u(2)*v(3)+u(3)*v(2) u(2)*v(4)+u(4)*v(2) u(3)*v(3) u(4)*v(3)+u(3)*v(4) u(4)*v(4)];
75end
76% one additional equation only if needed
77if n<4 & ~config.cal.SQUARE_PIX
78        u = P(3,:);
79        Temp = [Temp; u(1)^2 2*u(1)*u(2) 2*u(1)*u(3) 2*u(1)*u(4) u(2)^2 2*u(2)*u(3) 2*u(2)*u(4) u(3)^2 2*u(3)*u(4) u(4)^2];
80        b = [zeros(size(Temp(1:end-1,1)));1];
81        % TLS solution of Temp*q=b
82        [U,S,V] = svd([Temp,b],0);
83        q = -(1/V(11,end))*V(1:10,end);
84else
85        [U,S,V] = svd(Temp,0);
86        q = -V(:,size(V,2));
87end
88
89Q = [
90    q(1) q(2) q(3) q(4)
91    q(2) q(5) q(6) q(7)
92    q(3) q(6) q(8) q(9)
93    q(4) q(7) q(9) q(10)
94];
95% test which solution to take for q (-V or V)
96% diagonal entries of M_M should be positive
97M_M = P(1:3,:)*Q*P(1:3,:)';
98if (M_M(1,1)<=0)
99    q = -q; % V(:,size(V,2));
100    Q = [
101    q(1) q(2) q(3) q(4)
102    q(2) q(5) q(6) q(7)
103    q(3) q(6) q(8) q(9)
104    q(4) q(7) q(9) q(10)
105    ];
106end
107
108[U,S,V] = svd(Q,0);
109A = U(:,1:3)*sqrt(S(1:3,1:3));
110
111H = [A, B];
112
113% euclidian motion and shape
114Pe = P*H;
115Xe = inv(H)*X;
116
117% normalize coordiates
118Xe = Xe./repmat(Xe(4,:),4,1);
119
120PeRT = [];
121Rot      = [];
122if 1
123  Rot = [];
124  for i=1:n,
125        sc = norm(Pe(i*3,1:3),'fro');
126        % first normalize the Projection matrices to get normalized pixel points
127        Pe(i*3-2:i*3,:) = Pe(i*3-2:i*3,:)./sc;
128        % correct it of points behind the camera
129        xe = Pe(i*3-2:i*3,:)*Xe;
130        if sum(xe(3,:)<0),
131          Pe(i*3-2:i*3,:) = -Pe(i*3-2:i*3,:);
132        end
133
134        % decompose the matrix by using rq decomposition 
135        [K,R] = rq(Pe(i*3-2:i*3,1:3));
136        Cc      = -R'*inv(K)*Pe(i*3-2:i*3,4);% camera center
137        % Stephi calib params
138        Pst(i*3-2:i*3,:) = R'*inv(K);
139        Cst(i,:)                   = Cc';
140        % modify the Kalibaration matrix to get consistent
141        % euclidean motion Pe
142        K(1,3) = K(1,3)-config.cal.pp(i,1);
143        K(2,3) = K(2,3)-config.cal.pp(i,2);
144        PeRT = [PeRT; K*[R,-R*Cc]];
145        Rot      = [Rot;R];
146  end
147  Pe = PeRT;
148  C = Cst';
149end
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Note: See TracBrowser for help on using the repository browser.