R/rasterEntropy.R

Defines functions rasterEntropy

Documented in rasterEntropy

#' Multi-layer Pixel Entropy
#' 
#' Shannon entropy is calculated for each pixel based on it's layer values.
#' To be used with categorical / integer valued rasters.
#' 
#' Entropy is calculated as -sum(p log(p)); p being the class frequency per pixel.
#' 
#' @param img SpatRaster
#' @param ... additional arguments passed to writeRaster
#' @return
#' SpatRaster "entropy"
#' @export 
#' @examples
#' re <- rasterEntropy(rlogo)
#' ggR(re, geom_raster = TRUE)
rasterEntropy <- function(img, ...){
	img <- .toTerra(img)
    if(nlyr(img) <= 1)
      stop("img must have at least two layers")
    out <- app(img, fun = entropyCpp, ...)
    out <- .updateLayerNames(out, "entropy")
    out
}

Try the RStoolbox package in your browser

Any scripts or data that you put into this service are public.

RStoolbox documentation built on May 29, 2024, 6:34 a.m.