
Calculate pixel value threshold of a single-band image via Otsu's method
Source:R/threshold_otsu.R
threshold_otsu.RdCalculate Otsu's threshold value that separates a single-band (e.g. greyscale) image into two distinct classes.
The threshold value is determined by minimizing the combined intra-class variance.
library(terra) is used to perform out-of-memory operations.
Arguments
- image
Single-band raster to be classified (
SpatRasterobject fromlibrary(terra)), or a file path to the image to be imported (terra::rast()will be used).- range
Numeric vector (of length 2) specifying the histogram range to be used for thresholding. Defaults to the minimum and maximum values of the imported raster.
- levels
Number of histogram bins used to calculate the threshold value, typically based on the bit depth of the image (e.g. 8-bit image has
2^8=256levels). Defaults to256.
Value
Otsu's threshold value for the image (a single number), which can subsequently be used for image classification (e.g. convert continuous raster to binary raster).
References
Otsu, N. (1979). A threshold selection method from gray-level histograms. IEEE transactions on systems, man, and cybernetics, 9(1), 62-66. Pau, G., Fuchs, F., Sklyar, O., Boutros, M., & Huber, W. (2010).
EBImage—an R package for image processing with applications to cellular phenotypes. Bioinformatics, 26(7), 979-981.
See also
classify_image_binary()to classify single-band image using Otsu's method. Callsthreshold_otsu()internally.
Examples
if (FALSE) {
ndvi_mosaic <- system.file("extdata", "ndvi_mosaic.tif",
package="biodivercity")
terra::plot(ndvi_mosaic) # examine raster data
threshold_otsu(image = ndvi_mosaic)
}