選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

camera-model.ts 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * camera-model.ts
  20. * Camera model
  21. */
  22. import Speedy from 'speedy-vision';
  23. import { SpeedySize } from 'speedy-vision/types/core/speedy-size';
  24. import { SpeedyMatrix } from 'speedy-vision/types/core/speedy-matrix';
  25. import { SpeedyPoint2 } from 'speedy-vision/types/core/speedy-point';
  26. import { SpeedyVector2 } from 'speedy-vision/types/core/speedy-vector';
  27. import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
  28. import { Nullable, Utils } from '../utils/utils';
  29. import { Settings } from '../core/settings';
  30. import { PoseFilter } from './pose-filter';
  31. import { NumericalError } from '../utils/errors';
  32. /** A guess of the horizontal field-of-view of a typical camera, in degrees */
  33. const HFOV_GUESS = 60; // https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Cameras/Cameras.html
  34. /** The default scale of the image plane. The scale affects the focal length */
  35. const DEFAULT_SCALE = 2; // the length of the [-1,+1] interval
  36. /** Convert degrees to radians */
  37. const DEG2RAD = 0.017453292519943295; // pi / 180
  38. /** Convert radians to degrees */
  39. const RAD2DEG = 57.29577951308232; // 180 / pi
  40. /** Numerical tolerance */
  41. const EPSILON = 1e-6;
  42. /** Index of the horizontal focal length in the camera intrinsics matrix (column-major format) */
  43. const FX = 0;
  44. /** Index of the vertical focal length in the camera intrinsics matrix */
  45. const FY = 4;
  46. /** Index of the horizontal position of the principal point in the camera intrinsics matrix */
  47. const U0 = 6;
  48. /** Index of the vertical position of the principal point in the camera intrinsics matrix */
  49. const V0 = 7;
  50. /** Number of iterations used to refine the estimated pose */
  51. const POSE_REFINEMENT_ITERATIONS = 30;
  52. /** Maximum number of iterations used when refining the translation vector */
  53. const TRANSLATION_REFINEMENT_ITERATIONS = 15;
  54. /** Tolerance used to exit early when refining the translation vector */
  55. const TRANSLATION_REFINEMENT_TOLERANCE = DEFAULT_SCALE * 0.01;
  56. /** Size of the grid used to refine the translation vector */
  57. const TRANSLATION_REFINEMENT_GRIDSIZE = 5; //3;
  58. /**
  59. * Camera model
  60. */
  61. export class CameraModel
  62. {
  63. /** size of the image plane */
  64. private _imageSize: SpeedySize;
  65. /** 3x4 camera matrix */
  66. private _matrix: SpeedyMatrix;
  67. /** a helper to switch the handedness of a coordinate system */
  68. private _flipZ: SpeedyMatrix;
  69. /** entries of the intrinsics matrix in column-major format */
  70. private _intrinsics: number[];
  71. /** entries of the extrinsics matrix in column-major format */
  72. private _extrinsics: number[];
  73. /** smoothing filter */
  74. private _filter: PoseFilter;
  75. /**
  76. * Constructor
  77. */
  78. constructor()
  79. {
  80. this._imageSize = Speedy.Size(0, 0);
  81. this._matrix = Speedy.Matrix.Eye(3, 4);
  82. this._intrinsics = [1,0,0,0,1,0,0,0,1]; // 3x3 identity matrix
  83. this._extrinsics = [1,0,0,0,1,0,0,0,1,0,0,0]; // 3x4 matrix [ R | t ] = [ I | 0 ] no rotation & no translation
  84. this._filter = new PoseFilter();
  85. this._flipZ = Speedy.Matrix(4, 4, [
  86. 1, 0, 0, 0,
  87. 0, 1, 0, 0,
  88. 0, 0,-1, 0,
  89. 0, 0, 0, 1
  90. ]);
  91. }
  92. /**
  93. * Initialize the model
  94. * @param aspectRatio aspect ratio of the image plane
  95. * @param scale optional scale factor of the image plane
  96. */
  97. init(aspectRatio: number, scale: number = DEFAULT_SCALE): void
  98. {
  99. // log
  100. Utils.log(`Initializing the camera model...`);
  101. Utils.assert(aspectRatio > 0 && scale > 1e-5);
  102. // set the size of the image plane
  103. // this rule is conceived so that min(w,h) = s and w/h = a
  104. if(aspectRatio >= 1) {
  105. this._imageSize.width = aspectRatio * scale;
  106. this._imageSize.height = scale;
  107. }
  108. else {
  109. this._imageSize.width = scale;
  110. this._imageSize.height = scale / aspectRatio;
  111. }
  112. // reset the model
  113. this.reset();
  114. }
  115. /**
  116. * Release the model
  117. */
  118. release(): null
  119. {
  120. this.reset();
  121. return null;
  122. }
  123. /**
  124. * Update the camera model
  125. * @param homographyNDC 3x3 perspective transform
  126. * @returns a promise that resolves to a camera matrix
  127. */
  128. update(homographyNDC: SpeedyMatrix): SpeedyPromise<SpeedyMatrix>
  129. {
  130. Utils.assert(homographyNDC.rows == 3 && homographyNDC.columns == 3);
  131. // convert to image space
  132. const homography = this._convertToImageSpace(homographyNDC);
  133. // read the entries of the homography
  134. const h = homography.read();
  135. const h11 = h[0], h12 = h[3], h13 = h[6],
  136. h21 = h[1], h22 = h[4], h23 = h[7],
  137. h31 = h[2], h32 = h[5], h33 = h[8];
  138. // validate the homography (homography matrices aren't singular)
  139. const det = h13 * (h21 * h32 - h22 * h31) - h23 * (h11 * h32 - h12 * h31) + h33 * (h11 * h22 - h12 * h21);
  140. if(Math.abs(det) < EPSILON || Number.isNaN(det))
  141. return Speedy.Promise.reject(new NumericalError(`Can't update the camera model using an invalid homography matrix`));
  142. // estimate the pose
  143. const pose = this._estimatePose(homography);
  144. if(this._filter.feed(pose))
  145. this._extrinsics = this._filter.output().read();
  146. // compute the camera matrix
  147. const Z = this._flipZ; // switch to a right handed system
  148. const K = Speedy.Matrix(3, 3, this._intrinsics);
  149. const E = Speedy.Matrix(3, 4, this._extrinsics);
  150. this._matrix.setToSync(K.times(E).times(Z));
  151. /*
  152. // test
  153. console.log("homography ------------", homography.toString());
  154. console.log("intrinsics ------------", K.toString());
  155. console.log("extrinsics ------------", E.toString());
  156. console.log("extrinsicsINV ---------", Speedy.Matrix(this.computeViewMatrix().inverse()).toString());
  157. console.log("matrix ----------------", this._matrix.toString());
  158. console.log("projectionMatrix ----- ", this.computeProjectionMatrix(0.1,100).toString());
  159. */
  160. // done!
  161. return Speedy.Promise.resolve(this._matrix);
  162. }
  163. /**
  164. * Reset the camera model
  165. */
  166. reset(): void
  167. {
  168. this._resetIntrinsics();
  169. this._resetExtrinsics();
  170. }
  171. /**
  172. * The 3x4 camera matrix
  173. */
  174. get matrix(): SpeedyMatrix
  175. {
  176. return this._matrix;
  177. }
  178. /**
  179. * The size of the image plane
  180. */
  181. get imageSize(): SpeedySize
  182. {
  183. return this._imageSize;
  184. }
  185. /**
  186. * The aspect ratio of the image
  187. */
  188. get aspectRatio(): number
  189. {
  190. return this._imageSize.width / this._imageSize.height;
  191. }
  192. /**
  193. * Focal length in "pixels" (projection distance in the pinhole camera model)
  194. * same as (focal length in mm) * (number of "pixels" per world unit in "pixels"/mm)
  195. * "pixels" means image plane units
  196. */
  197. get focalLength(): number
  198. {
  199. return this._intrinsics[FX]; // fx == fy
  200. }
  201. /**
  202. * Horizontal field-of-view, given in radians
  203. */
  204. get fovx(): number
  205. {
  206. const halfWidth = this._imageSize.width / 2;
  207. return 2 * Math.atan(halfWidth / this._intrinsics[FX]);
  208. }
  209. /**
  210. * Vertical field-of-view, given in radians
  211. */
  212. get fovy(): number
  213. {
  214. const halfHeight = this._imageSize.height / 2;
  215. return 2 * Math.atan(halfHeight / this._intrinsics[FY]);
  216. }
  217. /**
  218. * Compute the view matrix. This 4x4 matrix moves 3D points from
  219. * world space to view space. We want the camera looking in the
  220. * direction of the negative z-axis (WebGL-friendly)
  221. * @returns a view matrix
  222. */
  223. computeViewMatrix(): SpeedyMatrix
  224. {
  225. const E = this._extrinsics;
  226. // We augment the 3x4 extrinsics matrix E with the [ 0 0 0 1 ] row
  227. // and get E+. Let Z be 4x4 flipZ, the identity matrix with the third
  228. // column negated. The following matrix is View = Z * E+ * Z. We get
  229. // the camera looking in the direction of the negative z-axis in a
  230. // right handed system!
  231. return Speedy.Matrix(4, 4, [
  232. E[0], E[1],-E[2], 0, // r1
  233. E[3], E[4],-E[5], 0, // r2
  234. -E[6],-E[7],+E[8], 0, // r3
  235. E[9], E[10],-E[11], 1 // t
  236. ]);
  237. }
  238. /**
  239. * Compute a perspective projection matrix for WebGL
  240. * @param near distance of the near plane
  241. * @param far distance of the far plane
  242. */
  243. computeProjectionMatrix(near: number, far: number): SpeedyMatrix
  244. {
  245. const fx = this._intrinsics[FX];
  246. const fy = this._intrinsics[FY];
  247. const halfWidth = this._imageSize.width / 2;
  248. const halfHeight = this._imageSize.height / 2;
  249. // we assume that the principal point is at the center of the image plane
  250. const right = near * (halfWidth / fx);
  251. const top = near * (halfHeight / fy);
  252. //const top = right * (halfHeight / halfWidth); // same thing
  253. const bottom = -top, left = -right; // symmetric frustum
  254. // a derivation of this projection matrix can be found at
  255. // https://www.songho.ca/opengl/gl_projectionmatrix.html
  256. // http://learnwebgl.brown37.net/08_projections/projections_perspective.html
  257. return Speedy.Matrix(4, 4, [
  258. 2 * near / (right - left), 0, 0, 0,
  259. 0, 2 * near / (top - bottom), 0, 0,
  260. (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1,
  261. 0, 0, -2 * far * near / (far - near), 0
  262. ]);
  263. }
  264. /**
  265. * Reset camera extrinsics
  266. */
  267. private _resetExtrinsics(): void
  268. {
  269. // set the rotation matrix to the identity
  270. this._extrinsics.fill(0);
  271. this._extrinsics[0] = this._extrinsics[4] = this._extrinsics[8] = 1;
  272. // reset filter
  273. this._filter.reset();
  274. }
  275. /**
  276. * Reset camera intrinsics
  277. */
  278. private _resetIntrinsics(): void
  279. {
  280. const cameraWidth = Math.max(this._imageSize.width, this._imageSize.height); // portrait or landscape?
  281. const u0 = 0; // principal point at the center of the image plane
  282. const v0 = 0;
  283. const fx = (cameraWidth / 2) / Math.tan(DEG2RAD * HFOV_GUESS / 2);
  284. const fy = fx;
  285. this._intrinsics[FX] = fx;
  286. this._intrinsics[FY] = fy;
  287. this._intrinsics[U0] = u0;
  288. this._intrinsics[V0] = v0;
  289. }
  290. /**
  291. * Convert a homography from NDC to image space
  292. * @param homographyNDC
  293. * @returns a new homography
  294. */
  295. private _convertToImageSpace(homographyNDC: SpeedyMatrix): SpeedyMatrix
  296. {
  297. const w = this._imageSize.width / 2;
  298. const h = this._imageSize.height / 2;
  299. // fromNDC converts points from NDC to image space
  300. const fromNDC = Speedy.Matrix(3, 3, [
  301. w, 0, 0,
  302. 0, h, 0,
  303. 0, 0, 1
  304. ]);
  305. /*
  306. // make h33 = 1 (wanted?)
  307. const data = homographyNDC.read();
  308. const h33 = data[8];
  309. const hom = homographyNDC.times(1/h33);
  310. */
  311. // convert homography
  312. return Speedy.Matrix(fromNDC.times(homographyNDC));
  313. }
  314. /**
  315. * Compute a normalized homography H^ = K^(-1) * H for an
  316. * ideal pinhole with f = 1 and principal point = (0,0)
  317. * @param homography homography H to be normalized
  318. * @returns normalized homography H^
  319. */
  320. private _normalizeHomography(homography: SpeedyMatrix): SpeedyMatrix
  321. {
  322. const u0 = this._intrinsics[U0];
  323. const v0 = this._intrinsics[V0];
  324. const fx = this._intrinsics[FX];
  325. const fy = this._intrinsics[FY];
  326. const u0fx = u0 / fx;
  327. const v0fy = v0 / fy;
  328. const h = homography.read();
  329. const h11 = h[0] / fx - u0fx * h[2], h12 = h[3] / fx - u0fx * h[5], h13 = h[6] / fx - u0fx * h[8];
  330. const h21 = h[1] / fy - v0fy * h[2], h22 = h[4] / fy - v0fy * h[5], h23 = h[7] / fy - v0fy * h[8];
  331. const h31 = h[2], h32 = h[5], h33 = h[8];
  332. /*console.log([
  333. h11, h21, h31,
  334. h12, h22, h32,
  335. h13, h23, h33,
  336. ]);*/
  337. return Speedy.Matrix(3, 3, [
  338. h11, h21, h31,
  339. h12, h22, h32,
  340. h13, h23, h33,
  341. ]);
  342. }
  343. /**
  344. * Estimate [ r1 | r2 | t ], where r1, r2 are orthonormal and t is a translation vector
  345. * @param normalizedHomography based on the ideal pinhole (where calibration K = I)
  346. * @returns a 3x3 matrix
  347. */
  348. private _estimatePartialPose(normalizedHomography: SpeedyMatrix): SpeedyMatrix
  349. {
  350. const h = normalizedHomography.read();
  351. const h11 = h[0], h12 = h[3], h13 = h[6];
  352. const h21 = h[1], h22 = h[4], h23 = h[7];
  353. const h31 = h[2], h32 = h[5], h33 = h[8];
  354. const h1norm2 = h11 * h11 + h21 * h21 + h31 * h31;
  355. const h2norm2 = h12 * h12 + h22 * h22 + h32 * h32;
  356. const h1norm = Math.sqrt(h1norm2);
  357. const h2norm = Math.sqrt(h2norm2);
  358. //const hnorm = (h1norm + h2norm) / 2;
  359. //const hnorm = Math.sqrt(h1norm * h2norm);
  360. const hnorm = Math.max(h1norm, h2norm); // this seems to work. why?
  361. // we expect h1norm to be approximately h2norm, but sometimes there is a lot of noise
  362. // if h1norm is not approximately h2norm, it means that the first two columns of
  363. // the normalized homography are not really encoding a rotation (up to a scale)
  364. //console.log("h1,h2",h1norm,h2norm);
  365. //console.log(normalizedHomography.toString());
  366. // compute a rough estimate for the scale factor
  367. // select the sign so that t3 = tz > 0
  368. const sign = h33 >= 0 ? 1 : -1;
  369. let scale = sign / hnorm;
  370. // sanity check
  371. if(Number.isNaN(scale))
  372. return Speedy.Matrix(3, 3, (new Array<number>(9)).fill(Number.NaN));
  373. // recover the rotation
  374. let r = new Array<number>(6);
  375. r[0] = scale * h11;
  376. r[1] = scale * h21;
  377. r[2] = scale * h31;
  378. r[3] = scale * h12;
  379. r[4] = scale * h22;
  380. r[5] = scale * h32;
  381. // refine the rotation (r is initially noisy)
  382. r = this._refineRotation(r);
  383. /*
  384. After refining the rotation vectors, let's adjust the scale factor as
  385. follows:
  386. We know that [ r1 | r2 | t ] is equal to the normalized homography H up
  387. to a non-zero scale factor s, i.e., [ r1 | r2 | t ] = s H. Let's call M
  388. the first two columns of H, i.e., M = [ h1 | h2 ], and R = [ r1 | r2 ].
  389. It follows that R = s M, meaning that M'R = s M'M. The trace of 2x2 M'R
  390. is such that tr(M'R) = tr(s M'M) = s tr(M'M), which means:
  391. s = tr(M'R) / tr(M'M) = (r1'h1 + r2'h2) / (h1'h1 + h2'h2)
  392. (also: s^2 = det(M'R) / det(M'M))
  393. */
  394. // adjust the scale factor
  395. scale = r[0] * h11 + r[1] * h21 + r[2] * h31;
  396. scale += r[3] * h12 + r[4] * h22 + r[5] * h32;
  397. scale /= h1norm2 + h2norm2;
  398. // recover the translation
  399. let t = new Array<number>(3);
  400. t[0] = scale * h13;
  401. t[1] = scale * h23;
  402. t[2] = scale * h33;
  403. // done!
  404. return Speedy.Matrix(3, 3, r.concat(t));
  405. }
  406. /**
  407. * Make two non-zero and non-parallel input vectors, r1 and r2, orthonormal
  408. * @param rot rotation vectors [ r1 | r2 ] in column-major format
  409. * @returns a 3x2 matrix R such that R'R = I (column-major format)
  410. */
  411. private _refineRotation(rot: number[]): number[]
  412. {
  413. const [r11, r21, r31, r12, r22, r32] = rot;
  414. /*
  415. A little technique I figured out to correct the rotation vectors
  416. ----------------------------------------------------------------
  417. We are given two 3x1 column-vectors r1 and r2 as input in a 3x2 matrix
  418. R = [ r1 | r2 ]. We would like that R'R = I, but that won't be the case
  419. because vectors r1 and r2 are not perfectly orthonormal due to noise.
  420. Let's first notice that R'R is symmetric. You can easily check that its
  421. two eigenvalues are both real and positive (as long as r1, r2 != 0 and
  422. r1 is not parallel to r2, but we never take such vectors as input).
  423. R'R = [ r1'r1 r1'r2 ] is of rank 2, positive-definite
  424. [ r1'r2 r2'r2 ]
  425. We proceed by computing an eigendecomposition Q D Q' of R'R, where Q is
  426. chosen to be orthogonal and D is a diagonal matrix whose entries are
  427. the eigenvalues of R'R.
  428. Let LL' be the Cholesky decomposition of D. Such decomposition exists
  429. and is trivially computed: just take the square roots of the entries of
  430. D. Since L is diagonal, we have L = L'. Its inverse is also trivially
  431. computed - call it Linv.
  432. Now, define a 2x2 correction matrix C as follows:
  433. C = Q * Linv * Q'
  434. This matrix rotates the input vector, scales it by some amount, and
  435. then rotates it back to where it was (i.e., Q'Q = Q Q' = I).
  436. We compute RC in order to correct the rotation vectors. We take its
  437. two columns as the corrected vectors.
  438. In order to show that the two columns of RC are orthonormal, we can
  439. show that (RC)'(RC) = I. Indeed, noticing that C is symmetric, let's
  440. expand the expression:
  441. (RC)'(RC) = C'R'R C = C R'R C = (Q Linv Q') (Q D Q') (Q Linv Q') =
  442. Q Linv (Q'Q) D (Q'Q) Linv Q' = Q Linv D Linv Q' =
  443. Q Linv (L L) Linv Q' = Q (Linv L) (L Linv) Q' = Q Q' = I
  444. I have provided below a closed formula to correct the rotation vectors.
  445. What C does to R is very interesting: it makes the singular values
  446. become 1. If U S V' is a SVD of R, then R'R = V S^2 V'. The singular
  447. values of R are the square roots of the eigenvalues of R'R. Letting
  448. S = L and V = Q, it follows that RC = U S V' V Linv V' = U V'. This
  449. means that RC is equivalent to the correction "trick" using the SVD
  450. found in the computer vision literature (i.e., compute the SVD and
  451. return U V'). That "trick" is known to return the rotation matrix that
  452. minimizes the Frobenius norm of the difference between the input and
  453. the output. Consequently, the technique I have just presented is also
  454. optimal in that sense!
  455. By the way, the input matrix R does not need to be 3x2.
  456. */
  457. // compute the entries of R'R
  458. const r1tr1 = r11*r11 + r21*r21 + r31*r31;
  459. const r2tr2 = r12*r12 + r22*r22 + r32*r32;
  460. const r1tr2 = r11*r12 + r21*r22 + r31*r32;
  461. // compute the two real eigenvalues of R'R
  462. const delta = (r1tr1 - r2tr2) * (r1tr1 - r2tr2) + 4 * r1tr2 * r1tr2;
  463. const sqrt = Math.sqrt(delta); // delta >= 0 always
  464. const eigval1 = (r1tr1 + r2tr2 + sqrt) / 2;
  465. const eigval2 = (r1tr1 + r2tr2 - sqrt) / 2;
  466. // compute two unit eigenvectors qi = (xi,yi) of R'R
  467. const alpha1 = (r2tr2 - eigval1) - r1tr2 * (1 + r1tr2) / (r1tr1 - eigval1);
  468. const x1 = Math.sqrt((alpha1 * alpha1) / (1 + alpha1 * alpha1));
  469. const y1 = x1 / alpha1;
  470. const alpha2 = (r2tr2 - eigval2) - r1tr2 * (1 + r1tr2) / (r1tr1 - eigval2);
  471. const x2 = Math.sqrt((alpha2 * alpha2) / (1 + alpha2 * alpha2));
  472. const y2 = x2 / alpha2;
  473. // compute the Cholesky decomposition LL' of the diagonal matrix D
  474. // whose entries are the two eigenvalues of R'R and then invert L
  475. const s1 = Math.sqrt(eigval1), s2 = Math.sqrt(eigval2); // singular values of R (pick s1 >= s2)
  476. /*
  477. const Linv = Speedy.Matrix(2, 2, [1/s1, 0, 0, 1/s2]); // L inverse
  478. // compute the correction matrix C = Q * Linv * Q', where Q = [q1|q2]
  479. // is orthogonal and Linv is computed as above
  480. const Q = Speedy.Matrix(2, 2, [x1, y1, x2, y2]);
  481. const Qt = Speedy.Matrix(2, 2, [x1, x2, y1, y2]);
  482. const C = Q.times(Linv).times(Qt);
  483. // correct the rotation vectors r1 and r2 using C
  484. const R = Speedy.Matrix(3, 2, [r11, r21, r31, r12, r22, r32]);
  485. return Speedy.Matrix(R.times(C)).read();
  486. */
  487. // find C = Q * Linv * Q' manually
  488. // [ a b ] is symmetric
  489. // [ b c ]
  490. const a = x1*x1/s1 + x2*x2/s2;
  491. const b = x1*y1/s1 + x2*y2/s2;
  492. const c = y1*y1/s1 + y2*y2/s2;
  493. // find RC manually
  494. return [
  495. a*r11 + b*r12,
  496. a*r21 + b*r22,
  497. a*r31 + b*r32,
  498. b*r11 + c*r12,
  499. b*r21 + c*r22,
  500. b*r31 + c*r32
  501. ];
  502. }
  503. /**
  504. * Compute a refined translation vector
  505. * @param normalizedHomography ideal pinhole K = I
  506. * @param rot rotation vectors [ r1 | r2 ] in column-major format
  507. * @param t0 initial estimate for the translation vector
  508. * @returns 3x1 translation vector in column-major format
  509. */
  510. private _refineTranslation(normalizedHomography: SpeedyMatrix, rot: number[], t0: number[]): number[]
  511. {
  512. /*
  513. Given a normalized homography H, the rotation vectors r1, r2, and a
  514. translation vector t, we know that [ r1 | r2 | t ] = s H for a non-zero
  515. scale factor s.
  516. If we take a homogeneous vector u = [ x y w ]' (i.e., w = 1), then
  517. [ r1 | r2 | t ] u is parallel to H u, which means that their cross
  518. product is zero:
  519. [ r1 | r2 | t ] u x H u = ( x r1 + y r2 + w t ) x H u = 0
  520. The following code finds an optimal translation vector t based on the
  521. above observation. H, r1, r2 are known.
  522. */
  523. const h = normalizedHomography.read();
  524. const h11 = h[0], h12 = h[3], h13 = h[6];
  525. const h21 = h[1], h22 = h[4], h23 = h[7];
  526. const h31 = h[2], h32 = h[5], h33 = h[8];
  527. const r11 = rot[0], r12 = rot[3];
  528. const r21 = rot[1], r22 = rot[4];
  529. const r31 = rot[2], r32 = rot[5];
  530. // generate a grid of sample points [ xi yi ]' in the image
  531. //const x = [ 0, -1, +1, +1, -1 ];
  532. //const y = [ 0, -1, -1, +1, +1 ];
  533. const g = TRANSLATION_REFINEMENT_GRIDSIZE;
  534. const x = new Array<number>(g*g);
  535. const y = new Array<number>(g*g);
  536. const halfWidth = this._imageSize.width / 2;
  537. const halfHeight = this._imageSize.height / 2;
  538. for(let k = 0, i = 0; i < g; i++) {
  539. for(let j = 0; j < g; j++, k++) {
  540. // in [-1,+1]
  541. x[k] = (i/(g-1)) * 2 - 1;
  542. y[k] = (j/(g-1)) * 2 - 1;
  543. // in [-s/2,+s/2], where s = w,h
  544. x[k] *= halfWidth;
  545. y[k] *= halfHeight;
  546. }
  547. }
  548. //console.log(x.toString(), y.toString());
  549. // set auxiliary values: ai = H [ xi yi 1 ]'
  550. const n = x.length;
  551. const a1 = new Array<number>(n);
  552. const a2 = new Array<number>(n);
  553. const a3 = new Array<number>(n);
  554. for(let i = 0; i < n; i++) {
  555. a1[i] = x[i] * h11 + y[i] * h12 + h13;
  556. a2[i] = x[i] * h21 + y[i] * h22 + h23;
  557. a3[i] = x[i] * h31 + y[i] * h32 + h33;
  558. }
  559. // we'll solve M t = v for t with linear least squares
  560. // M: 3n x 3, v: 3n x 1, t: 3 x 1
  561. const n3 = 3*n;
  562. const m = new Array<number>(n3 * 3);
  563. const v = new Array<number>(n3);
  564. for(let i = 0, k = 0; k < n; i += 3, k++) {
  565. m[i] = m[i+n3+1] = m[i+n3+n3+2] = 0;
  566. m[i+n3] = -(m[i+1] = a3[k]);
  567. m[i+2] = -(m[i+n3+n3] = a2[k]);
  568. m[i+n3+n3+1] = -(m[i+n3+2] = a1[k]);
  569. v[i] = a3[k] * (x[k] * r21 + y[k] * r22) - a2[k] * (x[k] * r31 + y[k] * r32);
  570. v[i+1] = -a3[k] * (x[k] * r11 + y[k] * r12) + a1[k] * (x[k] * r31 + y[k] * r32);
  571. v[i+2] = a2[k] * (x[k] * r11 + y[k] * r12) - a1[k] * (x[k] * r21 + y[k] * r22);
  572. }
  573. /*
  574. // this works, but I want more lightweight
  575. const M = Speedy.Matrix(n3, 3, m);
  576. const v_ = Speedy.Matrix(n3, 1, v);
  577. return Speedy.Matrix(M.ldiv(v_)).read();
  578. */
  579. /*
  580. Gradient descent with optimal step size / learning rate
  581. -------------------------------------------------------
  582. Let's find the column-vector x that minimizes the error function
  583. E(x) = r'r, where r = Ax - b, using gradient descent. This is linear
  584. least squares. We want to find x easily, QUICKLY and iteratively.
  585. The update rule of gradient descent is set to:
  586. x := x - w * grad(E)
  587. where w is the learning rate and grad(E) is the gradient of E(x):
  588. grad(E) = 2 A'r = 2 A'(Ax - b) = 2 A'A x - 2 A'b
  589. Let's adjust w to make x "converge quickly". Define function S(w) as:
  590. S(w) = x - w * grad(E) (step)
  591. and another function F(w) as:
  592. F(w) = E(S(w))
  593. which is the error of the step. We minimize F by setting its derivative
  594. to zero:
  595. 0 = dF = dF dS
  596. dw dS dw
  597. What follows is a fair amount of algebra. Do the math and you'll find
  598. the following optimal update rule:
  599. (c'c)
  600. x := x - --------- c
  601. (Ac)'(Ac)
  602. where c = A'r = A'(Ax - b)
  603. */
  604. // gradient descent: super lightweight implementation
  605. const r = new Array<number>(3*n);
  606. const c = new Array<number>(3);
  607. const Mc = new Array<number>(3*n);
  608. // initial guess
  609. const t = new Array<number>(3);
  610. t[0] = t0[0];
  611. t[1] = t0[1];
  612. t[2] = t0[2];
  613. // iterate
  614. for(let it = 0; it < TRANSLATION_REFINEMENT_ITERATIONS; it++) {
  615. //console.log("it",it+1);
  616. // compute residual r = Mt - v
  617. for(let i = 0; i < n3; i++) {
  618. r[i] = 0;
  619. for(let j = 0; j < 3; j++)
  620. r[i] += m[j*n3 + i] * t[j];
  621. r[i] -= v[i];
  622. }
  623. // compute c = M'r
  624. for(let i = 0; i < 3; i++) {
  625. c[i] = 0;
  626. for(let j = 0; j < n3; j++)
  627. c[i] += m[i*n3 + j] * r[j];
  628. }
  629. // compute Mc
  630. for(let i = 0; i < n3; i++) {
  631. Mc[i] = 0;
  632. for(let j = 0; j < 3; j++)
  633. Mc[i] += m[j*n3 + i] * c[j];
  634. }
  635. // compute c'c
  636. let num = 0;
  637. for(let i = 0; i < 3; i++)
  638. num += c[i] * c[i];
  639. //console.log("c'c=",num," at #",it+1);
  640. if(num < TRANSLATION_REFINEMENT_TOLERANCE)
  641. break;
  642. // compute (Mc)'(Mc)
  643. let den = 0;
  644. for(let i = 0; i < n3; i++)
  645. den += Mc[i] * Mc[i];
  646. // compute frc = c'c / (Mc)'(Mc)
  647. const frc = num / den;
  648. if(Number.isNaN(frc)) // this shouldn't happen
  649. break;
  650. // iterate: t = t - frc * c
  651. for(let i = 0; i < 3; i++)
  652. t[i] -= frc * c[i];
  653. }
  654. //console.log("OLD t:\n\n",t0.join('\n'));
  655. //console.log("new t:\n\n",t.join('\n'));
  656. // done!
  657. return t;
  658. }
  659. /**
  660. * Find a 3x3 rotation matrix R given two orthonormal vectors [ r1 | r2 ]
  661. * @param partialRotation partial rotation matrix [ r1 | r2 ] in column-major format
  662. * @returns a rotation matrix R in column-major format
  663. */
  664. private _computeFullRotation(partialRotation: number[]): number[]
  665. {
  666. const r11 = partialRotation[0], r12 = partialRotation[3];
  667. const r21 = partialRotation[1], r22 = partialRotation[4];
  668. const r31 = partialRotation[2], r32 = partialRotation[5];
  669. // r3 = +- ( r1 x r2 )
  670. let r13 = r21 * r32 - r31 * r22;
  671. let r23 = r31 * r12 - r11 * r32;
  672. let r33 = r11 * r22 - r21 * r12;
  673. // let's make sure that det R = +1 (keep the orientation)
  674. const det = r11 * (r22 * r33 - r23 * r32) - r21 * (r12 * r33 - r13 * r32) + r31 * (r12 * r23 - r13 * r22);
  675. if(det < 0) {
  676. r13 = -r13;
  677. r23 = -r23;
  678. r33 = -r33;
  679. }
  680. // done!
  681. return [
  682. r11, r21, r31,
  683. r12, r22, r32,
  684. r13, r23, r33
  685. ];
  686. }
  687. /**
  688. * Estimate the pose [ R | t ] given a homography in sensor space
  689. * @param homography must be valid
  690. * @returns 3x4 matrix
  691. */
  692. private _estimatePose(homography: SpeedyMatrix): SpeedyMatrix
  693. {
  694. const normalizedHomography = this._normalizeHomography(homography);
  695. const partialPose = Speedy.Matrix.Eye(3);
  696. // we want the estimated partial pose [ r1 | r2 | t ] to be as close
  697. // as possible to the normalized homography, up to a scale factor;
  698. // i.e., H * [ r1 | r2 | t ]^(-1) = s * I for a non-zero scalar s
  699. // it won't be a perfect equality due to noise in the homography.
  700. // remark: composition of homographies
  701. const residual = Speedy.Matrix(normalizedHomography);
  702. for(let k = 0; k < POSE_REFINEMENT_ITERATIONS; k++) {
  703. // incrementally improve the partial pose
  704. const rt = this._estimatePartialPose(residual); // rt should converge to the identity matrix
  705. partialPose.setToSync(rt.times(partialPose));
  706. residual.setToSync(residual.times(rt.inverse()));
  707. //console.log("rt",rt.toString());
  708. //console.log("residual",residual.toString());
  709. }
  710. //console.log('-----------');
  711. // read the partial pose
  712. const mat = partialPose.read();
  713. const r0 = mat.slice(0, 6);
  714. const t0 = mat.slice(6, 9);
  715. // refine the translation vector and compute the full rotation matrix
  716. const t = this._refineTranslation(normalizedHomography, r0, t0);
  717. const r = this._computeFullRotation(r0);
  718. // done!
  719. return Speedy.Matrix(3, 4, r.concat(t));
  720. }
  721. }