test_calib3d.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. if (typeof module !== 'undefined' && module.exports) {
  5. // The environment is Node.js
  6. var cv = require('./opencv.js'); // eslint-disable-line no-var
  7. }
  8. QUnit.module('Camera Calibration and 3D Reconstruction', {});
  9. QUnit.test('constants', function(assert) {
  10. assert.strictEqual(typeof cv.LMEDS, 'number');
  11. assert.strictEqual(typeof cv.RANSAC, 'number');
  12. assert.strictEqual(typeof cv.RHO, 'number');
  13. });
  14. QUnit.test('findHomography', function(assert) {
  15. let srcPoints = cv.matFromArray(4, 1, cv.CV_32FC2, [
  16. 56,
  17. 65,
  18. 368,
  19. 52,
  20. 28,
  21. 387,
  22. 389,
  23. 390,
  24. ]);
  25. let dstPoints = cv.matFromArray(4, 1, cv.CV_32FC2, [
  26. 0,
  27. 0,
  28. 300,
  29. 0,
  30. 0,
  31. 300,
  32. 300,
  33. 300,
  34. ]);
  35. const mat = cv.findHomography(srcPoints, dstPoints);
  36. assert.ok(mat instanceof cv.Mat);
  37. });
  38. QUnit.test('Rodrigues', function(assert) {
  39. // Converts a rotation matrix to a rotation vector and vice versa
  40. // data64F is the output array
  41. const rvec0 = cv.matFromArray(1, 3, cv.CV_64F, [1,1,1]);
  42. let rMat0 = new cv.Mat();
  43. let rvec1 = new cv.Mat();
  44. // Args: input Mat, output Mat. The function mutates the output Mat, so the function does not return anything.
  45. // cv.Rodrigues (InputArray=src, OutputArray=dst, jacobian=0)
  46. // https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#void%20Rodrigues(InputArray%20src,%20OutputArray%20dst,%20OutputArray%20jacobian)
  47. // vec to Mat, starting number is 3 long and each element is 1.
  48. cv.Rodrigues(rvec0, rMat0);
  49. assert.ok(rMat0.data64F.length == 9);
  50. assert.ok(0.23 > rMat0.data64F[0] > 0.22);
  51. // convert Mat to Vec, should be same as what we started with, 3 long and each item should be a 1.
  52. cv.Rodrigues(rMat0, rvec1);
  53. assert.ok(rvec1.data64F.length == 3);
  54. assert.ok(1.01 > rvec1.data64F[0] > 0.9);
  55. // Answer should be around 1: 0.9999999999999999
  56. });
  57. QUnit.test('estimateAffine2D', function(assert) {
  58. const inputs = cv.matFromArray(4, 1, cv.CV_32FC2, [
  59. 1, 1,
  60. 80, 0,
  61. 0, 80,
  62. 80, 80
  63. ]);
  64. const outputs = cv.matFromArray(4, 1, cv.CV_32FC2, [
  65. 21, 51,
  66. 70, 77,
  67. 40, 40,
  68. 10, 70
  69. ]);
  70. const M = cv.estimateAffine2D(inputs, outputs);
  71. assert.ok(M instanceof cv.Mat);
  72. assert.deepEqual(Array.from(M.data), [
  73. 23, 55, 97, 126, 87, 139, 227, 63, 0, 0,
  74. 0, 0, 0, 0, 232, 191, 71, 246, 12, 68,
  75. 165, 35, 53, 64, 99, 56, 27, 66, 14, 254,
  76. 212, 63, 103, 102, 102, 102, 102, 102, 182, 191,
  77. 195, 252, 174, 22, 55, 97, 73, 64
  78. ]);
  79. });