|
| 1 | +--- |
| 2 | +sidebar_position: 41 |
| 3 | +--- |
| 4 | + |
| 5 | +# Get perspective warp matrix |
| 6 | + |
| 7 | +_Returns a perspective transformation matrix from source points that transforms a quadrilateral._ |
| 8 | + |
| 9 | +[Options and parameters of `getPerspectiveWarp` function](https://image-js.github.io/image-js-typescript/functions/getPerspectiveWarp.html) |
| 10 | + |
| 11 | +`getPerspectiveWarp` function takes 4 corner points (source quadrilateral) and then calculates the 3×3 perspective transformation matrix that allows removing [perspective distortion](https://en.wikipedia.org/wiki/Perspective_distortion). |
| 12 | +The function also returns width and height of the new image. |
| 13 | +If they were put as option's parameters it just returns indicated width and height, otherwise it calculates new width and height from given source points. |
| 14 | + |
| 15 | +### Basic use case |
| 16 | + |
| 17 | +```ts |
| 18 | +const image = readSync('path/to/file.png'); |
| 19 | +// Define source corners (original image points) and destination image width and height. |
| 20 | +// In this case they correspond to credit card's corner points. |
| 21 | +const sourcePoints = [ |
| 22 | + [ |
| 23 | + { column: 55, row: 140 }, |
| 24 | + { column: 680, row: 38 }, |
| 25 | + { column: 840, row: 340 }, |
| 26 | + { column: 145, row: 460 }, |
| 27 | + ], |
| 28 | + { width: 725, height: 425 }, |
| 29 | +]; |
| 30 | + |
| 31 | +// Get transformation matrix using 4 points and `getPerspectiveWarp` function. |
| 32 | +const projectionMatrix = getPerspectiveWarp(sourcePoints); |
| 33 | +const projectedImage = image.transform(matrix.matrix, { |
| 34 | + width: matrix.width, |
| 35 | + height: matrix.height, |
| 36 | + inverse: true, |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +### Parameters and default values |
| 43 | + |
| 44 | +- `pts` |
| 45 | + |
| 46 | +- `options` |
| 47 | + |
| 48 | +#### Options |
| 49 | + |
| 50 | +| Property | Required | Default value | |
| 51 | +| ----------------------------------------------------------------------------------------------------------- | -------- | ------------- | |
| 52 | +| [`width`](https://image-js.github.io/image-js-typescript/interfaces/GetPerspectiveWarpOptions.html#width) | no | - | |
| 53 | +| [`height`](https://image-js.github.io/image-js-typescript/interfaces/GetPerspectiveWarpOptions.html#height) | no | - | |
0 commit comments