|
1 | 1 | """
|
2 |
| - restrict(img[, region]) -> imgr |
| 2 | + restrict(img[, dims]) -> imgr |
3 | 3 |
|
4 | 4 | Reduce the size of `img` by approximately two-fold along the dimensions listed in
|
5 |
| -`region`, or all spatial coordinates if `region` is not specified. |
| 5 | +`dims`, or all spatial coordinates if `dims` is not specified. |
| 6 | +
|
| 7 | +# Output |
| 8 | +
|
| 9 | +The type of output array `imgr` depends on the input type: |
| 10 | +
|
| 11 | +- If `img` is not an `OffsetArray`, then output array `imgr` will be a typical `Array` type. |
| 12 | +- If `img` is an `OffsetArray`, then output array `imgr` will also be an `OffsetArray`. |
| 13 | +
|
| 14 | +The size of `imgr` is approximately `1/2` of the original size. More specifically: |
| 15 | +
|
| 16 | +- if `Nₖ = size(img, k)` is odd, then `size(imgr, k) == (Nₖ+1) ÷ 2`. |
| 17 | +- if `Nₖ = size(img, k)` is even, then `size(imgr, k) == (Nₖ÷2) + 1`. |
| 18 | +
|
| 19 | +# Examples |
| 20 | +
|
| 21 | +The optional argument `dims` can be a `Tuple` or `Integer`: |
| 22 | +
|
| 23 | +```julia |
| 24 | +A = rand(5, 5) # size: (5, 5) |
| 25 | +
|
| 26 | +restrict(A) # size: (3, 3) |
| 27 | +
|
| 28 | +restrict(A, 1) # size: (3, 5) |
| 29 | +restrict(A, 2) # size: (5, 3) |
| 30 | +
|
| 31 | +restrict(A, (1, )) # size: (3, 5) |
| 32 | +restrict(A, (1, 2)) # size: (3, 3) |
| 33 | +``` |
| 34 | +
|
| 35 | +Unless the input array is 1-based, the origin will be halfed: |
| 36 | +
|
| 37 | +```julia |
| 38 | +julia> using ImageUtils, OffsetArrays |
| 39 | +
|
| 40 | +julia> Ao = OffsetArray(rand(5, 4), 5, 6); |
| 41 | +
|
| 42 | +julia> Ar = restrict(Ao); |
| 43 | +
|
| 44 | +julia> axes(Ao) |
| 45 | +(OffsetArrays.IdOffsetRange(values=6:10, indices=6:10), OffsetArrays.IdOffsetRange(values=7:10, indices=7:10)) |
| 46 | +
|
| 47 | +julia> axes(Ar) |
| 48 | +(OffsetArrays.IdOffsetRange(values=3:5, indices=3:5), OffsetArrays.IdOffsetRange(values=4:6, indices=4:6)) |
| 49 | +``` |
| 50 | +
|
| 51 | +# Extended help |
| 52 | +
|
6 | 53 | The term `restrict` is taken from the coarsening operation of algebraic multigrid
|
7 | 54 | methods; it is the adjoint of "prolongation" (which is essentially interpolation).
|
8 | 55 | `restrict` anti-aliases the image as it goes, so is better than a naive summation
|
@@ -69,10 +116,9 @@ In some applications (e.g., image registration), you may find it useful to trim
|
69 | 116 | """
|
70 | 117 | restrict(img::AbstractArray, ::Tuple{}) = img
|
71 | 118 |
|
72 |
| -restrict(A::AbstractArray, region::Vector{Int}) = restrict(A, (region...,)) |
73 | 119 | restrict(A::AbstractArray) = restrict(A, coords_spatial(A))
|
74 |
| -function restrict(A::AbstractArray, region::Dims) |
75 |
| - restrict(restrict(A, region[1]), Base.tail(region)) |
| 120 | +function restrict(A::AbstractArray, dims::Dims) |
| 121 | + restrict(restrict(A, dims[1]), Base.tail(dims)) |
76 | 122 | end
|
77 | 123 |
|
78 | 124 | function restrict(A::AbstractArray{T,N}, dim::Integer) where {T,N}
|
|
0 commit comments