Skip to contents

Performs multiresolution image segmentation following the Baatz-Schape algorithm, commonly used in object-based image analysis (OBIA). The algorithm iteratively merges adjacent pixels or regions based on spectral heterogeneity and shape constraints.

Usage

baatz_segmenter(
  x,
  scale_param = 50,
  color_weight = 0.9,
  compactness = 0.5,
  band_weights = NULL,
  smooth = 0L,
  output_file = NULL,
  verbose = TRUE
)

Arguments

x

A `SpatRaster` object or a character string giving the path to a raster file readable by terra.

scale_param

Numeric scalar controlling the segmentation scale. Larger values result in larger segments. Roughly corresponds to the maximum allowed increase in heterogeneity when merging regions.

color_weight

Numeric value in \([0,1]\) giving the relative importance of spectral (color) heterogeneity versus shape. Higher values emphasize spectral similarity.

compactness

Numeric value in \([0,1]\) controlling the trade-off between compactness and smoothness in the shape criterion.

band_weights

Optional numeric vector of length equal to the number of bands in `x`, giving per-band weights for the spectral heterogeneity term. If `NULL`, all bands are weighted equally.

smooth

Integer \(\ge 0\). Optional mean filter window size (pixels) applied to the raster prior to mean-shift. If 0 (default), no pre-smoothing is applied. If > 0, a smooth x smooth mean filter is applied via terra::focal().

output_file

Optional character string. If provided, the resulting segmentation raster is written to this file using writeRaster.

verbose

Do progress messages? (default: TRUE)

Value

A single-layer `SpatRaster` with integer segment labels.

Details

This function is a high-level R wrapper around a lower-level Baatz-Schape segmentation implementation, handling raster I/O, preprocessing, and conversion of the output back to a `SpatRaster`.

Input rasters are internally converted to a matrix of \(n_{pixels} \times n_{bands}\). Pixels containing `NA` values in any band are replaced by the corresponding band mean prior to segmentation.

The segmentation labels are returned as integer region identifiers. Label values are arbitrary and should be treated as categorical.

This implementation is intended for small to medium-sized rasters. For very large images, consider tiling strategies or out-of-core processing.

References

Baatz, M. & Schape, A. (2000). Multiresolution Segmentation: An Optimization Approach for High Quality Multi-Scale Image Segmentation. In: Strobl et al. (eds), Angewandte Geographische Informationsverarbeitung XII.

Examples

if (FALSE) { # \dontrun{
library(terra)
r <- rast(system.file("extdata", "sample_raster.tif", package = "rsegm"))

seg <- baatz_segmenter(
  r,
  scale_param = 50,
  color_weight = 0.9,
  compactness = 0.5
)

plot(seg)
} # }