|
1 | 1 | # ImageTransformations.jl |
2 | 2 |
|
| 3 | +```@setup overview |
| 4 | +using ImageShow |
| 5 | +``` |
| 6 | + |
3 | 7 | This package provides support for image resizing, image rotation, and |
4 | 8 | other spatial transformations of arrays. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +ImageTransformations.jl consists of two sets of API: the low level warping operations, and the high-level operations that built on top of it. |
| 13 | + |
| 14 | +- Low-level warping API: |
| 15 | + - `warp`: backward-mode warping |
| 16 | + - `WarpedView`: the lazy view version of `warp` |
| 17 | + - `InvWarpedView`: the inverse of `WarpedView` |
| 18 | +- high-level spatial operations: |
| 19 | + - `imresize`: aspect adjustment |
| 20 | + - `restrict`: a much more efficient version of `imresize` that two-folds/down-samples image to approximate 1/2 size. (This is now provided by ImageBase.) |
| 21 | + - `imrotate`: rotation |
| 22 | + |
| 23 | +For detailed usage of these functions, please refer to [function references](@ref package_references) and [examples](@ref Examples). The following section explains |
| 24 | +the core concept image warping so that you can get a clear understanding about |
| 25 | +this package while using it. |
| 26 | + |
| 27 | +## [Image warping](@id index_image_warping) |
| 28 | + |
| 29 | +!!! info |
| 30 | + This is just a very simple explaination on the internal of ImageTransformations. For more information about image warping, you can take a look at [the Princeton Computer Graphics course for Image Warping (Fall 2000)](https://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/warp/warp.pdf) |
| 31 | + |
| 32 | +Most image spatial transformation operations (e.g., rotation, resizing, translation) fall into the category of warping operation. Mathematically, for given input image `X`, a (backward-mode) warping operation `f` consists of two functions: coordination map `ϕ` and intensity estimator `τ`. |
| 33 | + |
| 34 | +```math |
| 35 | +Y_{i,j} = f(X)_{i, j} = τ(X, ϕ(i, j)) |
| 36 | +``` |
| 37 | + |
| 38 | +Take the following resizing operation as an example, for every pixel position `p` in output image `Y`, we 1) use the backward coordinate map `ϕ` to get its corresponding pixel position `q` in original image `X`. Since `q` may not be on grid, we need to 2) estimate the value of `X` on position `q` using function `τ`, and finally 3) assign `X[q]` back to `Y[p]`. In Julia words, it is |
| 39 | + |
| 40 | +```julia |
| 41 | +for p in CartesianIndexes(Y) |
| 42 | + q = ϕ(p) # backward coordinate map |
| 43 | + v = τ(X, q) # estimate the value |
| 44 | + Y[p] = v # assign value back |
| 45 | +end |
| 46 | +``` |
| 47 | + |
| 48 | +As you may have notice, we use backward coordinate map because this is the simplest way to iterate every pixel of the output image. This is why it is called backward-mode warping. In some literature, it is also called reverse warping. |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +In ImageTransformations, the `warp`-based operation uses Interpolations as our intensity estimator `τ`: |
| 53 | + |
| 54 | +```@example overview |
| 55 | +using Interpolations, ImageCore, TestImages |
| 56 | +using ImageTransformations |
| 57 | +
|
| 58 | +X = imresize(testimage("cameraman"), (64, 64)) # use small image as an example |
| 59 | +
|
| 60 | +sz = (128, 128) |
| 61 | +Y = similar(X, sz...) |
| 62 | +
|
| 63 | +# intensity estimator using interpolation |
| 64 | +itp = interpolate(X, BSpline(Linear())) # bilinear interpolation |
| 65 | +τ(q) = itp(q...) |
| 66 | +
|
| 67 | +# A linear coordinate map that satisfies: |
| 68 | +# - `ϕ(1, 1) == (1, 1)` |
| 69 | +# - `ϕ(128, 128) == (64, 64)` |
| 70 | +K = (size(X) .- (1, 1))./(sz .- (1, 1)) |
| 71 | +b = (1, 1) .- K |
| 72 | +ϕ(p) = @. K*p + b |
| 73 | +
|
| 74 | +for p in CartesianIndices(Y) |
| 75 | + q = ϕ(p.I) |
| 76 | + Y[p] = τ(q) |
| 77 | +end |
| 78 | +
|
| 79 | +mosaic(X, Y; nrow=1) |
| 80 | +``` |
| 81 | + |
| 82 | +This is the internal of ImageTransformations. For common usage of ImageTransformations, you should use either the low-level API `warp` or |
| 83 | +high-level API `imresize` and others. |
0 commit comments