@@ -3,9 +3,21 @@ import { Matrix, SingularValueDecomposition } from 'ml-matrix';
33import type { Point } from '../utils/geometry/points.js' ;
44
55interface GetPerspectiveWarpOptions {
6+ /**
7+ * The horizontal dimension (in pixels) of the final rectified rectangular image.
8+ */
69 width ?: number ;
10+ /**
11+ * The vertical dimension (in pixels) of the final rectified rectangular image.
12+ */
713 height ?: number ;
814}
15+ /**
16+ * Returns result matrix along with vertical and horizontal dimensions for the rectangular image.
17+ */
18+ type GetPerspectiveWarpData = Required < GetPerspectiveWarpOptions > & {
19+ matrix : number [ ] [ ] ;
20+ } ;
921
1022// REFERENCES :
1123// https://stackoverflow.com/questions/38285229/calculating-aspect-ratio-of-perspective-transform-destination-image/38402378#38402378
@@ -21,7 +33,7 @@ interface GetPerspectiveWarpOptions {
2133export default function getPerspectiveWarp (
2234 pts : Point [ ] ,
2335 options : GetPerspectiveWarpOptions = { } ,
24- ) {
36+ ) : GetPerspectiveWarpData {
2537 if ( pts . length !== 4 ) {
2638 throw new Error (
2739 `The array pts must have four elements, which are the four corners. Currently, pts have ${ pts . length } elements` ,
@@ -82,15 +94,15 @@ export default function getPerspectiveWarp(
8294 }
8395 M . push ( row ) ;
8496 }
85- return M ;
97+ return { matrix : M , width : widthRect , height : heightRect } ;
8698}
8799
88100/**
89101 * Sorts 4 points in order =>[top-left,top-right,bottom-right,bottom-left]. Input points must be in clockwise or counter-clockwise order.
90102 * @param pts - Array of 4 points.
91103 * @returns Sorted array of 4 points.
92104 */
93- function order4Points ( pts : Point [ ] ) {
105+ export function order4Points ( pts : Point [ ] ) {
94106 let tl : Point ;
95107 let tr : Point ;
96108 let br : Point ;
0 commit comments