This function serves as a wrapper for applying different skeletonization methods to a binary image, including the Zhang-Suen, Guo-Hall, and Medial Axis Transform (MAT) algorithms.
Usage
skeletonize_image(
img,
methods = c("ZhangSuen", "GuoHall", "MAT", "SteepestAscend"),
verbose = TRUE,
select.layer = NULL
)
Arguments
- img
A matrix, data frame, or `SpatRaster` object representing the binary image to be skeletonized.
- methods
A character vector specifying the skeletonization methods to apply. Valid options are
"ZhangSuen"
,"GuoHall"
, and"MAT"
. Defaults to all three methods.- verbose
Logical. If
TRUE
, displays progress and diagnostic messages during processing. Defaults toTRUE
.- select.layer
Integer specifying the layer to use if
img
is a multi-layer `SpatRaster`. Defaults to 2.
Value
If a single method is selected, the function returns a `SpatRaster` object representing the skeletonized image. If multiple methods are selected, a named list of `SpatRaster` objects is returned.
Details
This function allows for flexible and streamlined skeletonization of binary images using one or more supported algorithms:
"ZhangSuen"
: Implements the Zhang-Suen thinning algorithm."GuoHall"
: Implements the Guo-Hall thinning algorithm."MAT"
: Computes the Medial Axis Transform to extract the skeleton.
The function processes the input image with the specified methods and returns the results. If multiple methods are chosen, the results are returned as a named list, with each element corresponding to a method.
Examples
# Load a binary image as a SpatRaster
binary_image <- terra::rast(matrix(c(0, 1, 1, 0, 0, 1, 1, 0,0), nrow = 3))
# Apply all skeletonization methods
skeletons <- skeletonize_image(binary_image, verbose = TRUE)
#>
#> Applying method: ZhangSuen
#> Image dimensions: 3 x 3
#> Initial foreground pixels: 4
#> Final foreground pixels: 4
#> Total iterations: 1
#>
#> Applying method: GuoHall
#> Image dimensions: 3 x 3
#> Initial foreground pixels: 4
#> Final foreground pixels: 4
#> Total iterations: 1
#>
#> Applying method: MAT
#>
#> Distance transform computed
#> Warning: No skeleton points detected in the result
#>
#> Applying method: SteepestAscend
#> Steepest ascend ridge-based skeletonization complete. Skeleton pixels: 0