UNPKG

simple-homography

Version:

Calculates a homography matrix using exact point correspondences

29 lines (28 loc) 1.4 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var Math = __importStar(require("mathjs")); //See https://math.stackexchange.com/a/2619023 function calculateHomography(a1, b1, a2, b2, a3, b3, a4, b4) { var points = Math.matrix([ [-a1[0], -a1[1], -1, 0, 0, 0, a1[0] * b1[0], a1[1] * b1[0], b1[0]], [0, 0, 0, -a1[0], -a1[1], -1, a1[0] * b1[1], a1[1] * b1[1], b1[1]], [-a2[0], -a2[1], -1, 0, 0, 0, a2[0] * b2[0], a2[1] * b2[0], b2[0]], [0, 0, 0, -a2[0], -a2[1], -1, a2[0] * b2[1], a2[1] * b2[1], b2[1]], [-a3[0], -a3[1], -1, 0, 0, 0, a3[0] * b3[0], a3[1] * b3[0], b3[0]], [0, 0, 0, -a3[0], -a3[1], -1, a3[0] * b3[1], a3[1] * b3[1], b3[1]], [-a4[0], -a4[1], -1, 0, 0, 0, a4[0] * b4[0], a4[1] * b4[0], b4[0]], [0, 0, 0, -a4[0], -a4[1], -1, a4[0] * b4[1], a4[1] * b4[1], b4[1]], [0, 0, 0, 0, 0, 0, 0, 0, 1] ]); var pointsInv = Math.inv(points); var hnum = (Math.multiply(pointsInv, [0, 0, 0, 0, 0, 0, 0, 0, 1])); return Math.reshape(hnum, [3, 3]); } exports.calculateHomography = calculateHomography;