DistanceTransforms.boolean_indicator
— Methodboolean_indicator
boolean_indicator(f::AbstractArray)
boolean_indicator(f::AbstractGPUArray)
boolean_indicator(f::BitArray)
Create a float representation of a boolean indicator array where 0
represents the background and 1
represents the foreground. This function converts a logical array into a floating-point array where foreground values (logical 1
) are marked as 0.0f0
(float representation of 0
), and background values (logical 0
) are marked with a large float number 1.0f10
. This representation is useful in distance transform operations to differentiate between the foreground and the background.
Arguments
f
: An array of boolean values or anAbstractGPUArray
of boolean values, wheretrue
indicates the foreground andfalse
indicates the background.
Returns
- A floating-point array of the same dimensions as
f
, with foreground values set to0.0f0
and background values set to1.0f10
.
Performance
- If
f
is aBitArray
, the conversion uses LoopVectorization.jl for a potential speedup. The warning check arguments are disabled for performance reasons. - If
f
is anAbstractGPUArray
, the computation is offloaded to the GPU using a custom kernel,boolean_indicator_kernel
, which is expected to yield a significant speedup on compatible hardware.
Examples
f = BitArray([true, false, true, false])
f_float = boolean_indicator(f)
# f_float will be Float32[0.0f0, 1.0f10, 0.0f0, 1.0f10]
f_gpu = CUDA.zeros(Bool, 10) # assuming CUDA.jl is used for GPU arrays
f_gpu[5] = true
f_float_gpu = boolean_indicator(f_gpu)
# f_float_gpu will be a GPU array with the fifth element as 0.0f0 and others as 1.0f10
Notes
- The choice of
1.0f10
as the large number is arbitrary and can be adjusted if needed for specific applications. - The
@turbo
macro from LoopVectorization.jl is used whenf
is aBitArray
to unroll and vectorize the loop for optimal performance. - When
f
is anAbstractGPUArray
, theboolean_indicator_kernel
kernel function is used to perform the operation in parallel on the GPU. - The
KernelAbstractions.synchronize(backend)
call ensures that all GPU operations are completed before returning the result.
DistanceTransforms.transform!
— Methodtransform!
transform!(f::AbstractVector, output, v, z)
transform!(img::AbstractMatrix, output, v, z; threaded=true)
transform!(vol::AbstractArray{<:Real,3}, output, v, z, temp; threaded=true)
transform!(img::AbstractGPUMatrix, output, v, z)
In-place squared Euclidean distance transforms. These functions apply the transform to the input data and store the result in the output
argument.
- The first function operates on vectors.
- The second function operates on matrices with optional threading.
- The third function operates on 3D arrays with optional threading.
- The fourth function is specialized for GPU matrices.
The intermediate arrays v
and z
(and temp
for 3D arrays) are used for computation. The threaded
parameter enables parallel computation on the CPU.
Arguments
f
: Input vector, matrix, or 3D array.output
: Preallocated array to store the result.v
: Preallocated array for indices, matching the dimensions off
.z
: Preallocated array for intermediate values, one element larger thanf
.temp
: Preallocated array for intermediate values when transforming 3D arrays, matching the dimensions ofoutput
.
Examples
f = rand([0f0, 1f0], 10)
f_bool = boolean_indicator(f)
output = similar(f)
v = ones(Int32, size(f))
z = ones(eltype(f), size(f) .+ 1)
transform!(f_bool, output, v, z)
DistanceTransforms.transform
— Methodtransform
transform(f::AbstractVector)
transform(img::AbstractMatrix; threaded=true)
transform(vol::AbstractArray{<:Real,3}; threaded=true)
transform(img::AbstractGPUMatrix)
Non-in-place squared Euclidean distance transforms that return a new array with the result. They allocate necessary intermediate arrays internally.
- The first function operates on vectors.
- The second function operates on matrices with optional threading.
- The third function operates on 3D arrays with optional threading.
- The fourth function is specialized for GPU matrices.
The threaded
parameter can be used to enable or disable parallel computation on the CPU.
Arguments
f/img/vol
: Input vector, matrix, or 3D array to be transformed.
Examples
f = rand([0f0, 1f0], 10)
f_bool = boolean_indicator(f)
f_tfm = transform(f_bool)