[37] | 1 | function [omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c),
|
---|
| 2 |
|
---|
| 3 | %compute_extrinsic
|
---|
| 4 | %
|
---|
| 5 | %[omckk,Tckk,Rckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c)
|
---|
| 6 | %
|
---|
| 7 | %Computes the extrinsic parameters attached to a 3D structure X_kk given its projection
|
---|
| 8 | %on the image plane x_kk and the intrinsic camera parameters fc, cc and kc.
|
---|
| 9 | %Works with planar and non-planar structures.
|
---|
| 10 | %
|
---|
| 11 | %INPUT: x_kk: Feature locations on the images
|
---|
| 12 | % X_kk: Corresponding grid coordinates
|
---|
| 13 | % fc: Camera focal length
|
---|
| 14 | % cc: Principal point coordinates
|
---|
| 15 | % kc: Distortion coefficients
|
---|
| 16 | % alpha_c: Skew coefficient
|
---|
| 17 | %
|
---|
| 18 | %OUTPUT: omckk: 3D rotation vector attached to the grid positions in space
|
---|
| 19 | % Tckk: 3D translation vector attached to the grid positions in space
|
---|
| 20 | % Rckk: 3D rotation matrices corresponding to the omc vectors
|
---|
| 21 | %
|
---|
| 22 | %Method: Computes the normalized point coordinates, then computes the 3D pose
|
---|
| 23 | %
|
---|
| 24 | %Important functions called within that program:
|
---|
| 25 | %
|
---|
| 26 | %normalize: Computes the normalize image point coordinates.
|
---|
| 27 | %
|
---|
| 28 | %pose3D: Computes the 3D pose of the structure given the normalized image projection.
|
---|
| 29 | %
|
---|
| 30 | %project_points.m: Computes the 2D image projections of a set of 3D points
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | if nargin < 6,
|
---|
| 35 | alpha_c = 0;
|
---|
| 36 | if nargin < 5,
|
---|
| 37 | kc = zeros(5,1);
|
---|
| 38 | if nargin < 4,
|
---|
| 39 | cc = zeros(2,1);
|
---|
| 40 | if nargin < 3,
|
---|
| 41 | fc = ones(2,1);
|
---|
| 42 | if nargin < 2,
|
---|
| 43 | error('Need 2D projections and 3D points (in compute_extrinsic.m)');
|
---|
| 44 | return;
|
---|
| 45 | end;
|
---|
| 46 | end;
|
---|
| 47 | end;
|
---|
| 48 | end;
|
---|
| 49 | end;
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | %keyboard;
|
---|
| 53 |
|
---|
| 54 | % Compute the normalized coordinates:
|
---|
| 55 |
|
---|
| 56 | xn = normalize(x_kk,fc,cc,kc,alpha_c);
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | Np = size(xn,2);
|
---|
| 61 |
|
---|
| 62 | %% Check for planarity of the structure:
|
---|
| 63 |
|
---|
| 64 | X_mean = mean(X_kk')';
|
---|
| 65 |
|
---|
| 66 | Y = X_kk - (X_mean*ones(1,Np));
|
---|
| 67 |
|
---|
| 68 | YY = Y*Y';
|
---|
| 69 |
|
---|
| 70 | [U,S,V] = svd(YY);
|
---|
| 71 |
|
---|
| 72 | r = S(3,3)/S(2,2);
|
---|
| 73 |
|
---|
| 74 | %keyboard;
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | if (r < 1e-3)|(Np < 5), %1e-3, %1e-4, %norm(X_kk(3,:)) < eps, % Test of planarity
|
---|
| 78 |
|
---|
| 79 | %fprintf(1,'Planar structure detected: r=%f\n',r);
|
---|
| 80 |
|
---|
| 81 | % Transform the plane to bring it in the Z=0 plane:
|
---|
| 82 |
|
---|
| 83 | R_transform = V';
|
---|
| 84 |
|
---|
| 85 | %norm(R_transform(1:2,3))
|
---|
| 86 |
|
---|
| 87 | if norm(R_transform(1:2,3)) < 1e-6,
|
---|
| 88 | R_transform = eye(3);
|
---|
| 89 | end;
|
---|
| 90 |
|
---|
| 91 | if det(R_transform) < 0, R_transform = -R_transform; end;
|
---|
| 92 |
|
---|
| 93 | T_transform = -(R_transform)*X_mean;
|
---|
| 94 |
|
---|
| 95 | X_new = R_transform*X_kk + T_transform*ones(1,Np);
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | % Compute the planar homography:
|
---|
| 99 |
|
---|
| 100 | H = compute_homography(xn,X_new(1:2,:));
|
---|
| 101 |
|
---|
| 102 | % De-embed the motion parameters from the homography:
|
---|
| 103 |
|
---|
| 104 | sc = mean([norm(H(:,1));norm(H(:,2))]);
|
---|
| 105 |
|
---|
| 106 | H = H/sc;
|
---|
| 107 |
|
---|
| 108 | % Extra normalization for some reasons...
|
---|
| 109 | %H(:,1) = H(:,1)/norm(H(:,1));
|
---|
| 110 | %H(:,2) = H(:,2)/norm(H(:,2));
|
---|
| 111 |
|
---|
| 112 | if 0, %%% Some tests for myself... the opposite sign solution leads to negative depth!!!
|
---|
| 113 |
|
---|
| 114 | % Case#1: no opposite sign:
|
---|
| 115 |
|
---|
| 116 | omckk1 = rodrigues([H(:,1:2) cross(H(:,1),H(:,2))]);
|
---|
| 117 | Rckk1 = rodrigues(omckk1);
|
---|
| 118 | Tckk1 = H(:,3);
|
---|
| 119 |
|
---|
| 120 | Hs1 = [Rckk1(:,1:2) Tckk1];
|
---|
| 121 | xn1 = Hs1*[X_new(1:2,:);ones(1,Np)];
|
---|
| 122 | xn1 = [xn1(1,:)./xn1(3,:) ; xn1(2,:)./xn1(3,:)];
|
---|
| 123 | e1 = xn1 - xn;
|
---|
| 124 |
|
---|
| 125 | % Case#2: opposite sign:
|
---|
| 126 |
|
---|
| 127 | omckk2 = rodrigues([-H(:,1:2) cross(H(:,1),H(:,2))]);
|
---|
| 128 | Rckk2 = rodrigues(omckk2);
|
---|
| 129 | Tckk2 = -H(:,3);
|
---|
| 130 |
|
---|
| 131 | Hs2 = [Rckk2(:,1:2) Tckk2];
|
---|
| 132 | xn2 = Hs2*[X_new(1:2,:);ones(1,Np)];
|
---|
| 133 | xn2 = [xn2(1,:)./xn2(3,:) ; xn2(2,:)./xn2(3,:)];
|
---|
| 134 | e2 = xn2 - xn;
|
---|
| 135 |
|
---|
| 136 | if 1, %norm(e1) < norm(e2),
|
---|
| 137 | omckk = omckk1;
|
---|
| 138 | Tckk = Tckk1;
|
---|
| 139 | Rckk = Rckk1;
|
---|
| 140 | else
|
---|
| 141 | omckk = omckk2;
|
---|
| 142 | Tckk = Tckk2;
|
---|
| 143 | Rckk = Rckk2;
|
---|
| 144 | end;
|
---|
| 145 |
|
---|
| 146 | else
|
---|
| 147 |
|
---|
| 148 | u1 = H(:,1);
|
---|
| 149 | u1 = u1 / norm(u1);
|
---|
| 150 | u2 = H(:,2) - dot(u1,H(:,2)) * u1;
|
---|
| 151 | u2 = u2 / norm(u2);
|
---|
| 152 | u3 = cross(u1,u2);
|
---|
| 153 | RRR = [u1 u2 u3];
|
---|
| 154 | omckk = rodrigues(RRR);
|
---|
| 155 |
|
---|
| 156 | %omckk = rodrigues([H(:,1:2) cross(H(:,1),H(:,2))]);
|
---|
| 157 | Rckk = rodrigues(omckk);
|
---|
| 158 | Tckk = H(:,3);
|
---|
| 159 |
|
---|
| 160 | end;
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | %If Xc = Rckk * X_new + Tckk, then Xc = Rckk * R_transform * X_kk + Tckk + T_transform
|
---|
| 165 |
|
---|
| 166 | Tckk = Tckk + Rckk* T_transform;
|
---|
| 167 | Rckk = Rckk * R_transform;
|
---|
| 168 |
|
---|
| 169 | omckk = rodrigues(Rckk);
|
---|
| 170 | Rckk = rodrigues(omckk);
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | else
|
---|
| 174 |
|
---|
| 175 | %fprintf(1,'Non planar structure detected: r=%f\n',r);
|
---|
| 176 |
|
---|
| 177 | % Computes an initial guess for extrinsic parameters (works for general 3d structure, not planar!!!):
|
---|
| 178 | % The DLT method is applied here!!
|
---|
| 179 |
|
---|
| 180 | J = zeros(2*Np,12);
|
---|
| 181 |
|
---|
| 182 | xX = (ones(3,1)*xn(1,:)).*X_kk;
|
---|
| 183 | yX = (ones(3,1)*xn(2,:)).*X_kk;
|
---|
| 184 |
|
---|
| 185 | J(1:2:end,[1 4 7]) = -X_kk';
|
---|
| 186 | J(2:2:end,[2 5 8]) = X_kk';
|
---|
| 187 | J(1:2:end,[3 6 9]) = xX';
|
---|
| 188 | J(2:2:end,[3 6 9]) = -yX';
|
---|
| 189 | J(1:2:end,12) = xn(1,:)';
|
---|
| 190 | J(2:2:end,12) = -xn(2,:)';
|
---|
| 191 | J(1:2:end,10) = -ones(Np,1);
|
---|
| 192 | J(2:2:end,11) = ones(Np,1);
|
---|
| 193 |
|
---|
| 194 | JJ = J'*J;
|
---|
| 195 | [U,S,V] = svd(JJ);
|
---|
| 196 |
|
---|
| 197 | RR = reshape(V(1:9,12),3,3);
|
---|
| 198 |
|
---|
| 199 | if det(RR) < 0,
|
---|
| 200 | V(:,12) = -V(:,12);
|
---|
| 201 | RR = -RR;
|
---|
| 202 | end;
|
---|
| 203 |
|
---|
| 204 | [Ur,Sr,Vr] = svd(RR);
|
---|
| 205 |
|
---|
| 206 | Rckk = Ur*Vr';
|
---|
| 207 |
|
---|
| 208 | sc = norm(V(1:9,12)) / norm(Rckk(:));
|
---|
| 209 | Tckk = V(10:12,12)/sc;
|
---|
| 210 |
|
---|
| 211 | omckk = rodrigues(Rckk);
|
---|
| 212 | Rckk = rodrigues(omckk);
|
---|
| 213 |
|
---|
| 214 | end;
|
---|