Select Region

To cut a N dimensional part of an array, we use the following methods:

NDTools.select_regionFunction
select_region(src, [new_size]; new_size=size(mat), center=ft_center_diff(size(mat)).+1, 
                   pad_value=zero(eltype(mat)), dst_center = new_size .÷ 2 .+1,
                   M = nothing)

selects (extracts) a region of interest (ROI), defined by new_size and centered at center in the source image. Note that the number of dimensions can be smaller in new_size and center, in which case the default values will be insterted into the missing dimensions. new_size does not need to fit into the source array and missing values will be replaced with pad_value.

Arguments:

  • src. The array to extract the region from.
  • new_size. The size of the array view after the operation finished. By default the original size is assumed. As an alternative alias, the new_size can also be provided as a second argument.
  • center. Specifies the center of the new view in coordinates of the old view. By default an alignment of the Fourier-centers is assumed.
  • dst_center. defines the center coordinate in the destination array which should align with the above source center. If nothing is provided, the right center pixel of the dst array or new array is used.
  • pad_value. Specifies the value which is inserted in case the ROI extends to outside the source area.
  • M=nothing. Specifies a magnification, if !isnothing(M) then new_size = round.(Int, M .* size(src)) will be set.

The returned result is a newly allocated array of the same type as the src. This is currently faster that selectregionview().

See also

Examples

julia> select_region(ones(3,3),new_size=(7,7),center=(1,3))
7×7 Matrix{Float64}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  1.0  1.0  1.0  0.0  0.0  0.0
 0.0  1.0  1.0  1.0  0.0  0.0  0.0
 0.0  1.0  1.0  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0

julia> a = ones((3,3))
3×3 Matrix{Float64}:
 1.0  1.0  1.0
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> dst=select_region(a,new_size=(10,10), dst_center=(1,1)) # pad a with zeros to a size of (10,10), but place original center at the corner
10×10 Matrix{Float64}:
 1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
source
select_region(src::AbstractArray{T,N}, new_size; M=nothing, 
              center=size(src).÷2 .+1, pad_value=zero(eltype(src)), dst_center = new_size .÷ 2 .+1) where {T,N}

Alias to select_region(src; new_size=new_size, center=center, pad_value=pad_value, dst_center=dst_center)

source
NDTools.select_region!Function
select_region!(src::AbstractArray{T, N}, dst=nothing, [new_size]; 
                center=size(src).÷2 .+1, dst_center=nothing,
                new_size=2 .*(1 .+ abs.(dst_center.-(size(src).÷ 2 .+ 1))) .+ size(src),
                pad_value=zero(eltype(mat), operator!=assign_to!)) where {T,N}

selects (extracts, pads, shifts) a region of interest (ROI), defined by new_size and centered with the destination center aligned at the position center in the source image. Note that the number of dimensions in new_size, center and dst_center can be smaller , in which case default values (see below) will be insterted into the missing dimensions. new_size does not need to fit into the source array and missing values will be replaced with pad_value, if no dst is provided.

As opposed to select_region(), this version returns a copy rather than a view or, alternatively, also writes into a destination array dst (new_size is then interpreted to refer to the maximally assigned region). If nothing is provided for dst, a new array of size new_size is created.

Arguments:

  • src. The source array to select from.
  • dst. The destination array to write into, if provided. By default dst=nothing a new array is created. The dstarray (or new array) is returned.
  • new_size. The size of the array view after the operation finished. By default a maximally large destination size is chosen, which means that any overlap is copied. If you specify new_size, be aware that the center and dst_center specifications below really have to refer to centers to be copied! If new_size is not specified, a size to fully encompass the (potentially displaced) source array is automatically chosen. As an alternative alias, the new_size can also be provided as a third argument.
  • center. Specifies the center of the new view in coordinates of the old view. By default an alignment of the Fourier-center (right center) is assumed.
  • dst_center. defines the center coordinate in the destination array which should align with the above source center. If nothing is provided, the right center pixel of the dst array or new array is used.
  • pad_value. specifies the value which is inserted in case the ROI extends to outside the source area. This is only used, if no dst array is provided.
  • operator!. allows to provide a userdefined array assignment function. The function myop!(dst,src) should operator on array views and typically perform the assignment elementwise, overwriting the entries in dst. Five such functions are exported by NDTools: assign_to!, add_to!, sub_to!, mul_to!, div_to!, representing the operations .=, .+=, .-=, .*= and ./= respectively.

The returned results is the destination (or newly created) array. Note that this version is rather fast, since it consists of only a sinlge sub-array assigment on views, avoiding copy operations.

See also

Examples:

julia> a = ones(5,6);

julia> dst=select_region(a,new_size=(10,10), dst_center=(1,1)) # pad a with zeros to a size of (10,10), but place original center at the corner
10×10 Matrix{Float64}:
 1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0

 julia> select_region!(2 .*a,dst, dst_center=size(dst)) # write a doubled version into the bottom right corner
10×10 Matrix{Float64}:
 1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0
 0.0  0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0
 0.0  0.0  0.0  0.0  0.0  0.0  2.0  2.0  2.0  2.0
source
function select_region!(src::AbstractArray{T, N}, dst, new_size; center=size(src).÷2 .+1, dst_center=size(dst).÷ 2 .+1, operator! =assign_to!) where {T,N}

alias to selectregion!(src, dst; newsize=newsize, center=center, dstcenter=dst_center, operator!=operator!)

source
NDTools.select_region_viewFunction
select_region_view(src, [new_size]; new_size=size(src), center=ft_center_diff(size(src)).+1, dst_center=ft_center_diff(new_size), pad_value=zero(eltype(src)))

selects (extracts) a region of interest (ROI), defined by new_size and centered at center in the source image. Note that the number of dimensions can be smaller in new_size and center, in which case the default values will be insterted into the missing dimensions. new_size does not need to fit into the source array and missing values will be replaced with pad_value.

Arguments:

  • src. The source array to select from.
  • new_size. The size of the array view after the operation finished. By default the original size is assumed. As an alternative aliaws, the new_size can also be provided as a second argument.
  • center. Specifies the center of the new view in coordinates of the old view. By default an alignment of the Fourier-centers is assumed.
  • dst_center. Specifies the destination center of the new view to be mapped to the source center as given by center.
  • pad_value. Specifies the value which is inserted in case the ROI extends to outside the source area.

The returned results is a mutable view, which allows this method to also be used for writing into a ROI

See also

Examples

julia> select_region_view(ones(3,3),new_size=(7,7),center=(1,3))
7×7 NDTools.MutablePaddedView{Float64, 2, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}}, OffsetArrays.OffsetMatrix{Float64, Matrix{Float64}}}:
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  1.0  1.0  1.0  0.0  0.0  0.0
 0.0  1.0  1.0  1.0  0.0  0.0  0.0
 0.0  1.0  1.0  1.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0
source
select_region_view(src::Array{T,N}, new_size; center=ft_center_diff(size(src)).+1, dst_center=ft_center_diff(Tuple(expand_size(new_size, size(src)))).+1, pad_value=zero(eltype(src))) where {T,N}

alias to selectregionview(src; newsize=newsize, center=center, dstcenter=dstcenter, padvalue=padvalue)

source

Slice

NDTools.sliceFunction
slice(arr, dim, index)

Return a N dimensional slice (where one dimensions has size 1) of the N-dimensional arr at the index position index in the dim dimension of the array. It holds size(out)[dim] == 1.

Examples

julia> x = [1 2 3; 4 5 6; 7 8 9]
3×3 Matrix{Int64}:
 1  2  3
 4  5  6
 7  8  9

julia> NDTools.slice(x, 1, 1)
1×3 view(::Matrix{Int64}, 1:1, 1:3) with eltype Int64:
 1  2  3
source
NDTools.slice_indicesFunction
slice_indices(a, dim, index)

Arguments:

a should be the axes obtained by axes(arr) of an array. dim is the dimension to be selected and index the index of it.

Examples

julia> NDTools.slice_indices((1:10, 1:20, 1:12, 1:33), 1, 3)
(3:3, 1:20, 1:12, 1:33)
source