R/imgnoise.R

Defines functions imgnoise

Documented in imgnoise

#' Takes an numeric array of any number of dimensions, and applies a form of random noise to it.
#'
#' @param image An array of numerics. If generating Salt and Pepper noise, all entries need to be between 0 and 1.
#' @param noise_type The type of noise to apply to the array - Gaussian, Salt and Pepper, Speckle, or Uniform noise.
#' @param mu If generating Gaussian noise - the mean of the normal distribution the random number is sampled from.
#' @param variance If generating Gaussian or Speckle noise - the variance of the distribution the random number is sampled from.
#' @param noise_density If generating Salt and Pepper noise - the approximate proportion (between 0 and 1) of entries in the array that will be changed to 0 or 1.
#' @param lower_bound If generating Uniform noise - the minimum possible value for the random number.
#' @param upper_bound If generating Uniform noise - the maximum possible value for the random number.
#'
#' @examples
#' #suppose Rlogo is an array generated by Rlogo = readPNG(system.file("img", "Rlogo.png", package = "png))
#' imgnoise(Rlogo, "gaussian", mu = 0.2, variance = 0.3)
#' imgnoise(Rlogo, "salt_and_pepper", noise_density = 0.8)
#' imgnoise(Rlogo, "speckle", variance = 0.15)
#' imgnoise(Rlogo, "uniform", lower_bound = -0.4, upper_bound = 0.9)
#' imgnoise(Rlogo, "gaussian")
#' imgnoise(Rlogo, "salt_and_pepper")
#' @export

imgnoise = function(image, noise_type, ...) {
  image = as.array(image)
  arr_dim = length(dim(image)) #image array dimensions (e.g. 3 for 2D-RGB)

  if (noise_type == "gaussian") {
    return(apply(image, 1:arr_dim, gaussian_noise, ...))
  }

  if (noise_type == "salt_and_pepper") {
    return(apply(image, 1:arr_dim, salt_and_pepper_noise, ...))
  }

  if (noise_type == "speckle") {
    return(apply(image, 1:arr_dim, speckle_noise, ...))
  }

  if (noise_type == "uniform") {
    return(apply(image, 1:arr_dim, uniform_noise, ...))
  }
}
rayheberer/imgnoise documentation built on Dec. 5, 2017, 1:34 a.m.