Skip to contents

Classify a single-band image such as a spectral index or greyscale raster derived from satellite data. If a threshold value is not specified, the Otsu's threshold can be calculated and used for classification.

Usage

classify_image_binary(
  image,
  threshold = "otsu",
  range = NULL,
  levels = 256,
  file = NULL,
  ...
)

Arguments

image

Single band raster to be classified (SpatRaster object from library(terra)), or a file path to the image to be imported (terra::rast() will be used).

threshold

Either a numeric value for a manually-specified threshold, or "otsu" to calculate the threshold via Otsu's method. Defaults to "otsu".

range

Numeric vector (of length 2) specifying the histogram range to be used for Otsu's thresholding (only relevant if threshold = "otsu"). Defaults to the minimum and maximum values of the imported raster.

levels

Number of histogram bins used to calculate the threshold value (only relevant if threshold = "otsu"). Typically based on the bit depth of the image (e.g. 8-bit image has 2^8 = 256 levels). Defaults to 256.

file

File path to export output raster (optional). Defaults to NULL.

...

Other arguments that may be supplied to terra::writeRaster().

Value

The classified raster (SpatRaster object).

See also

  • threshold_otsu() Internal function used to calculate threshold value via Otsu's method.

Examples

if (FALSE) {
  ndvi_mosaic <- system.file("extdata", "ndvi_mosaic.tif",
                              package="biodivercity")
  terra::plot(ndvi_mosaic) # examine continuous raster
  ndvi_classified <- classify_image_binary(image = ndvi_mosaic)
  terra::plot(ndvi) # examine classified raster
}