binarize_with_thr: Binarize with known thresholds

binarize_with_thrR Documentation

Binarize with known thresholds

Description

Apply a threshold or a raster of thresholds to a grayscale image, producing a binary image.

Usage

binarize_with_thr(r, thr)

Arguments

r

numeric terra::SpatRaster with one layer.

thr

either a numeric vector of length one (for global thresholding) or a numeric terra::SpatRaster with one layer (for local thresholding).

Details

This function supports both global and pixel-wise thresholding. It is a wrapper around the > operator from the terra package. If a single numeric threshold is provided via thr, it is applied globally to all pixels in r. If instead a terra::SpatRaster object is provided, local thresholding is performed, where each pixel is compared to its corresponding threshold value.

This is useful after estimating thresholds using thr_twocorner(), thr_isodata(), or apply_by_direction(method = "thr_isodata"), among other posibilities.

Value

Logical terra::SpatRaster (TRUE for sky, FALSE for non-sky) with the same dimensions as r.

Note

For global thresholding, thr must be greater than or equal to the minimum value of r and lower than its maximum value.

Examples

r <- read_caim()
bin <- binarize_with_thr(r$Blue, thr_isodata(r$Blue[]))
plot(bin)

## Not run: 
# This function is also compatible with thresholds estimated using
# the 'autothresholdr' package:
require(autothresholdr)
r <- r$Blue
r <- normalize_minmax(r) %>% multiply_by(255) %>% round()
thr <- auto_thresh(r[], "IsoData")[1]
bin <- binarize_with_thr(r, thr)
plot(bin)

## End(Not run)

rcaiman documentation built on Sept. 9, 2025, 5:42 p.m.