Skip to content

Commit ac88bd0

Browse files
docs: update documentation on transform function
2 parents 7be01a6 + 2712e93 commit ac88bd0

25 files changed

+484
-12
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ jobs:
1010
nodejs:
1111
uses: zakodium/workflows/.github/workflows/nodejs.yml@nodejs-v1
1212
with:
13+
node-version: 18.x
1314
lint-check-types: true
1415
npm-test-command: npm run build
1516
node-version-matrix: '[18]'
1617
upload-coverage: false
18+
disable-test-package: true

docs/Features/Geometry/Geometry.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ Geometric operations in image processing involve transforming the spatial coordi
88

99
### Methods
1010

11-
| Can be applied on | Images | Masks |
12-
| -------------------------------------------------------------------------------------------------------- | ------- | -------- |
13-
| [Flip(`flip`)](./Flip.md 'internal link on flip') | ✅ | ❌ |
14-
| [Resize(`resize`)](./Resize.md 'internal link on resize') | ✅ | ❌ |
15-
| [Rotate(`rotate`)](./Rotate.md 'internal link on rotate') | ✅ | ❌ |
16-
| [Transform(`transform`)](./Transform.md 'internal link on transform') | ✅ | ❌ |
17-
| [Transform and rotate(`transformRotate`)](./Transform%20and%20Rotate 'internal link on transformRotate') | ✅ | ❌ |
11+
| Can be applied on | Images | Masks |
12+
| --------------------------------------------------------------------------------------------------------------------------------- | ------- | -------- |
13+
| [Flip(`flip`)](./Flip.md 'internal link on flip') | ✅ | ❌ |
14+
| [Resize(`resize`)](./Resize.md 'internal link on resize') | ✅ | ❌ |
15+
| [Rotate(`rotate`)](./Rotate.md 'internal link on rotate') | ✅ | ❌ |
16+
| [Transform(`transform`)](./Transform.md 'internal link on transform') | ✅ | ❌ |
17+
| [Transform and rotate(`transformRotate`)](./Transform%20and%20Rotate 'internal link on transformRotate') | ✅ | ❌ |
18+
| [Get perspective warp matrix(`getPerspectiveWarp`)](./Get%20Perspective%20Warp%20Matrix.md 'internal link on getPerspectiveWarp') | - | - |
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
![Basic use case of perspectiveWarp](./images/cardPerspective.png)
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 | - |

docs/Features/Geometry/Transform.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,25 @@ _Applies linear transformations to an image, such as scaling, rotation, skewing,
1010

1111
[🖼️ Image options and parameters of `transform` method](https://image-js.github.io/image-js-typescript/classes/Image.html#transform 'github.io link')
1212

13-
`transform` method uses transformation matrix to rotate, translate, and/or scale the image. User needs to pass on the matrix that will be applied to the image. Matrix must have 2 rows and 3 columns:
13+
`transform` method uses transformation matrix to rotate, translate, and/or scale the image.
14+
User needs to pass on the matrix that will be applied to the image. Matrix must have either 2 rows and 3 columns or 3 rows and 3 columns:
1415

1516
$$
1617
\begin{bmatrix}
17-
1 & 2 & 3\\
18-
a & b & c
18+
a & b & c\\
19+
d & e & f \\
20+
g & h & i
1921
\end{bmatrix}
2022
$$
2123

22-
Where the first two columns are responsible for image [rotation](https://en.wikipedia.org/wiki/Rotation 'wikipedia link on rotation') and [shear](https://en.wikipedia.org/wiki/Shear_mapping 'wikipedia link on image shearing'), while last column is responsible for image [translation](<https://en.wikipedia.org/wiki/Translation_(geometry)#:~:text=In%20Euclidean%20geometry%2C%20a%20translation,origin%20of%20the%20coordinate%20system> 'wikipedia link on translation').
24+
If 2x3 matrix is passed, the algorithm will consider it as [affine transformation](https://en.wikipedia.org/wiki/Affine_transformation) so last row will be set as `[0,0,1]`;
25+
26+
- the first `a` and `e` are responsible for image scaling
27+
- `b` and `d` are responsible for [shear](https://en.wikipedia.org/wiki/Shear_mapping 'wikipedia link on image shearing'). Combination of these 4 variables allows [rotating](https://en.wikipedia.org/wiki/Rotations_and_reflections_in_two_dimensions 'wikipedia link on rotation'). `c` and `f` are responsible for image [translation](<https://en.wikipedia.org/wiki/Translation_(geometry)#:~:text=In%20Euclidean%20geometry%2C%20a%20translation,origin%20of%20the%20coordinate%20system> 'wikipedia link on translation').
28+
- `g` and `h` are used for an operation called projection. It allows changing image perspective.
29+
- `i` is a normalization factor. It acts like a "scaling factor" for the entire coordinate system. Think of it as a zoom lens setting.
30+
31+
For more information take a look at the tutorial about [image transformations](../../Tutorials/Applying%20transform%20function%20on%20images.md)
2332

2433
:::caution
2534
Matrix cannot be singular. Otherwise it cannot be inverted. Click [here](https://en.wikipedia.org/wiki/Invertible_matrix 'wikipedia link on invertible matrices') to learn more.
Loading

0 commit comments

Comments
 (0)