R/normalize.R

Defines functions normalize_glcm

Documented in normalize_glcm

#' Normalize a GLCM
#'
#' Function that normalizes a gray-level co-occurrence matrix (GLCM) so that the
#' sum of all the elements equals unity. This has the added benefit of
#' converting the GLCM to a probability distribution.
#'
#' @param mat gray-level co-occurrence matrix
#' @return normalized GLCM as a numeric matrix
#' @export
#' @examples
#' # normalize an arbitrary matrix
#' a <- matrix(1:9, nrow = 3)
#' n_a <- normalize_glcm(a)
#'
#' print(a)
#' print(n_a)
normalize_glcm <- function(mat) {
  # ensure valid GLCM is passed in mat
  stopifnot(is.numeric(mat))
  stopifnot(sum(is.na(mat)) == 0)
  stopifnot(nrow(mat) == ncol(mat))

  # normalize and return
  mat / sum(mat)
}
sbarkerclarke-phd/CoOccurR documentation built on April 5, 2024, 1:48 p.m.