You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_Returns a perspective transformation matrix from source points that transforms a quadrilateral._
8
+
9
+
`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).
10
+
The function also returns width and height of the new image.
11
+
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.
12
+
13
+
### Basic use case
14
+
15
+
```ts
16
+
const image =readSync('path/to/file.png');
17
+
// Define source corners (original image points) and destination image width and height.
18
+
// In this case they correspond to credit card's corner points.
19
+
const sourcePoints = [
20
+
[
21
+
{ column: 55, row: 140 },
22
+
{ column: 680, row: 38 },
23
+
{ column: 840, row: 340 },
24
+
{ column: 145, row: 460 },
25
+
],
26
+
{ width: 725, height: 425 },
27
+
];
28
+
29
+
// Get transformation matrix using 4 points and `getPerspectiveWarp` function.
Copy file name to clipboardExpand all lines: docs/Tutorials/Applying transform function on images.md
+79-6Lines changed: 79 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,72 @@ In a broader sense of the term, image transformation refers to the process of mo
2
2
3
3
In ImageJS, however, `transform()` function does transformations that can be accomplished through [matrix multiplication](https://en.wikipedia.org/wiki/Matrix_multiplication).
4
4
5
-
In this tutorial we will cover and explain basic transformation techniques as well as explain how `transform()` function allows us to modify images.
5
+
In this tutorial we will cover and explain basic geometric transformation techniques as well as explain how `transform()` function allows us to modify images.
6
+
7
+
## How image transformations work
8
+
9
+
[Geometric transformation](https://www.geeksforgeeks.org/electronics-engineering/geometric-transformation-in-image-processing-1/) modifies the location of pixels in an image. It consists of two main steps:
10
+
11
+
- Spatial transformation of pixels or coordinates and intensity interpolation.
12
+
- Intensity interpolation is used to assign the intensity value of pixels after spatial transformation.
13
+
14
+
In a geometric transformation a pixel at coordinate $$(x, y)$$ will be moved to coordinate $$(x', y')$$. That is the coordinate (x', y') of the output image which will have the intensity value of the coordinate (x, y) in the input image.
15
+
16
+
In this tutorial we will focus more on spatial transformation and talk more about interpolation another time. Basic geometric transformation is given by the equation in matrix form:
17
+
18
+
$$
19
+
\begin{bmatrix}
20
+
x' \\
21
+
y' \\
22
+
z'
23
+
\end{bmatrix} = A \begin{bmatrix}
24
+
x \\
25
+
y \\
26
+
z\end{bmatrix}
27
+
=\begin{bmatrix}
28
+
a & b & c\\
29
+
d & e & f \\
30
+
g & h & i\\
31
+
\end{bmatrix}
32
+
\begin{bmatrix}
33
+
x \\
34
+
y \\
35
+
z
36
+
\end{bmatrix}
37
+
$$
38
+
39
+
where
40
+
41
+
-$$(x, y,z)$$ is the input coordinate
42
+
-$$(x', y',z')$$ is the output coordinate
43
+
-$$A$$ is the affine transformation matrix
44
+
45
+
:::note
46
+
47
+
### Example of matrix multiplication
48
+
49
+
If you are interested to look a bit deeper into matrix multiplications, take a look at the [`ml-matrix`](https://mljs.github.io/matrix/index.html'link on ml-matrix api') package.
50
+
It facilitates basic matrix operations in ImageJS, as well as something more advanced like inverting a matrix.
51
+
Here is a basic demo on how to upscale a pixel coordinate by 2 using `ml-matrix` library.

226
300
227
301
:::note
228
-
Image-js also has `rotate()` and `transformRotate()` functions. `rotate()` function allows rotating an image by multiple of 90 degrees.
302
+
Image-js also has [`rotate()`](../Features/Geometry/Rotate.md) and [`transformRotate()`](../Features/Geometry/Transform%20and%20Rotate.md) functions. `rotate()` function allows rotating an image by multiple of 90 degrees.
229
303
`transformRotate()` allows rotating an image by any degree. It also allows choosing the axe of rotation. So, for rotation, you have other functions that allow you to perform it.
230
-
Current tutorial just demonstrates the basic principle behind transformation of such kind.
231
304
:::
232
305
233
306
## Projective Transformations
@@ -286,9 +359,9 @@ const sourcePoints = [
286
359
287
360
// Get transformation matrix using 4 points and `getPerspectiveWarp` function.
0 commit comments