@@ -3,9 +3,21 @@ import { Matrix, SingularValueDecomposition } from 'ml-matrix';
3
3
import type { Point } from '../utils/geometry/points.js' ;
4
4
5
5
interface GetPerspectiveWarpOptions {
6
+ /**
7
+ * The horizontal dimension (in pixels) of the final rectified rectangular image.
8
+ */
6
9
width ?: number ;
10
+ /**
11
+ * The vertical dimension (in pixels) of the final rectified rectangular image.
12
+ */
7
13
height ?: number ;
8
14
}
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
+ } ;
9
21
10
22
// REFERENCES :
11
23
// https://stackoverflow.com/questions/38285229/calculating-aspect-ratio-of-perspective-transform-destination-image/38402378#38402378
@@ -21,7 +33,7 @@ interface GetPerspectiveWarpOptions {
21
33
export default function getPerspectiveWarp (
22
34
pts : Point [ ] ,
23
35
options : GetPerspectiveWarpOptions = { } ,
24
- ) {
36
+ ) : GetPerspectiveWarpData {
25
37
if ( pts . length !== 4 ) {
26
38
throw new Error (
27
39
`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(
82
94
}
83
95
M . push ( row ) ;
84
96
}
85
- return M ;
97
+ return { matrix : M , width : widthRect , height : heightRect } ;
86
98
}
87
99
88
100
/**
89
101
* Sorts 4 points in order =>[top-left,top-right,bottom-right,bottom-left]. Input points must be in clockwise or counter-clockwise order.
90
102
* @param pts - Array of 4 points.
91
103
* @returns Sorted array of 4 points.
92
104
*/
93
- function order4Points ( pts : Point [ ] ) {
105
+ export function order4Points ( pts : Point [ ] ) {
94
106
let tl : Point ;
95
107
let tr : Point ;
96
108
let br : Point ;
0 commit comments