|
44 | 44 |
|
45 | 45 | It is worth mentioning that we will be working with 2D coordinates, so $$z$$ will be 1 in most cases.
|
46 | 46 |
|
47 |
| -:::note |
48 |
| - |
49 |
| -### Example of matrix multiplication |
50 |
| - |
51 |
| -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. |
52 |
| -It facilitates basic matrix operations in ImageJS, as well as something more advanced like inverting a matrix. |
53 |
| -Here is a basic demo on how to upscale a pixel coordinate by 2 using `ml-matrix` library. |
54 |
| - |
55 |
| -```ts |
56 |
| -import { Matrix } from 'ml-matrix'; |
57 |
| - |
58 |
| -const matrix = new Matrix([[10], [10], [1]]); |
59 |
| - |
60 |
| -const transform = new Matrix([ |
61 |
| - [2, 0, 0], |
62 |
| - [0, 2, 0], |
63 |
| - [0, 0, 1], |
64 |
| -]); |
65 |
| - |
66 |
| -const result = transform.mmul(matrix); |
67 |
| -/* |
68 |
| -gives [20,20,1] as a new coordinate of a pixel. |
69 |
| -*/ |
70 |
| -``` |
71 |
| - |
72 |
| -::: |
73 |
| - |
74 | 47 | ## Types of transformation
|
75 | 48 |
|
76 | 49 | Here, we distinguish between two primary types of transformations:
|
@@ -305,6 +278,58 @@ Image-js also has [`rotate()`](../Features/Geometry/Rotate.md) and [`transformRo
|
305 | 278 | `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.
|
306 | 279 | :::
|
307 | 280 |
|
| 281 | +<details> |
| 282 | +<summary> |
| 283 | +<b> Example of matrix multiplication</b> |
| 284 | +</summary> |
| 285 | +As mentioned previously, to rotate an image around its center, you need to translate your image to the origin, rotate it, and translate it back to its original position. |
| 286 | + |
| 287 | +Performing these operations separately would require three different matrix multiplications on your image, which is computationally expensive. The optimal solution is to combine all three transformations into a single matrix by multiplying the transformation matrices together first, then applying the result to your image, as was shown in the previous example. |
| 288 | + |
| 289 | +To accomplish this, you can use the [`ml-matrix`](https://mljs.github.io/matrix/index.html 'link on ml-matrix api') package. It facilitates basic matrix operations in ImageJS, as well as more advanced operations like matrix inversion. |
| 290 | + |
| 291 | +Here's a step-by-step code example showing how to create a complex transformation matrix: |
| 292 | + |
| 293 | +```ts |
| 294 | +import { Matrix } from 'ml-matrix'; |
| 295 | + |
| 296 | +const angle = Math.PI / 4; |
| 297 | +const center = image.getCoordinates('center'); |
| 298 | + |
| 299 | +const cos = Math.cos(angle); |
| 300 | +const sin = Math.sin(angle); |
| 301 | +// Step 1: Translate to center. |
| 302 | +const translateToOrigin = new Matrix([ |
| 303 | + [1, 0, center.column], |
| 304 | + [0, 1, center.row], |
| 305 | + [0, 0, 1], |
| 306 | +]); |
| 307 | + |
| 308 | +// Step 2: Rotation matrix |
| 309 | +const rotation = new Matrix([ |
| 310 | + [cos, -sin, 0], |
| 311 | + [sin, cos, 0], |
| 312 | + [0, 0, 1], |
| 313 | +]); |
| 314 | + |
| 315 | +// Step 3: Translate back |
| 316 | +const translateBack = new Matrix([ |
| 317 | + [1, 0, -center.column], |
| 318 | + [0, 1, -center.row], |
| 319 | + [0, 0, 1], |
| 320 | +]); |
| 321 | + |
| 322 | +const rotateAroundCenterMatrix = translateToOrigin |
| 323 | + .mmul(rotation) |
| 324 | + .mmul(translateBack); |
| 325 | + |
| 326 | +const rotateAroundCenterImage = image.transform( |
| 327 | + rotateAroundCenterMatrix.to2DArray(), |
| 328 | +); |
| 329 | +``` |
| 330 | + |
| 331 | +</details> |
| 332 | + |
308 | 333 | ## Projective Transformations
|
309 | 334 |
|
310 | 335 | Projective transformations use the full 3×3 matrix, including the bottom row parameters `g`, `h`, and `i`. These create perspective effects and can map rectangular images onto quadrilaterals.
|
|
0 commit comments