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 | |
---|
26 | function [Pe,Xe,C,Rot] = euclidize(Ws,Lambda,P,X,config) |
---|
27 | n = size(Ws,1)/3; % number of cameras |
---|
28 | m = size(Ws,2); % number of points |
---|
29 | |
---|
30 | % compute B |
---|
31 | a = []; b = []; c = []; |
---|
32 | for 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,:))]; |
---|
36 | end |
---|
37 | TempA = -P(3:3:3*n, :); |
---|
38 | TempB = -P(3:3:3*n, :); |
---|
39 | for i = 1:n |
---|
40 | TempA(i, :) = TempA(i, :)*a(i)/c(i); |
---|
41 | TempB(i, :) = TempB(i, :)*b(i)/c(i); |
---|
42 | end |
---|
43 | TempA = TempA + P(1:3:3*n, :); |
---|
44 | TempB = TempB + P(2:3:3*n, :); |
---|
45 | Temp = [TempA; TempB]; |
---|
46 | [U,S,V] = svd(Temp,0); |
---|
47 | B = 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 | % |
---|
61 | Temp = []; b = []; |
---|
62 | for 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)]; |
---|
75 | end |
---|
76 | % one additional equation only if needed |
---|
77 | if 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); |
---|
84 | else |
---|
85 | [U,S,V] = svd(Temp,0); |
---|
86 | q = -V(:,size(V,2)); |
---|
87 | end |
---|
88 | |
---|
89 | Q = [ |
---|
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 |
---|
97 | M_M = P(1:3,:)*Q*P(1:3,:)'; |
---|
98 | if (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 | ]; |
---|
106 | end |
---|
107 | |
---|
108 | [U,S,V] = svd(Q,0); |
---|
109 | A = U(:,1:3)*sqrt(S(1:3,1:3)); |
---|
110 | |
---|
111 | H = [A, B]; |
---|
112 | |
---|
113 | % euclidian motion and shape |
---|
114 | Pe = P*H; |
---|
115 | Xe = inv(H)*X; |
---|
116 | |
---|
117 | % normalize coordiates |
---|
118 | Xe = Xe./repmat(Xe(4,:),4,1); |
---|
119 | |
---|
120 | PeRT = []; |
---|
121 | Rot = []; |
---|
122 | if 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'; |
---|
149 | end |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|