RGB2gray: Convert RGB image or colormap to grayscale image

View source: R/image.R

RGB2grayR Documentation

Convert RGB image or colormap to grayscale image

Description

This function converts color image to gray image.

Usage

RGB2gray(img, method = "average", weights = NULL)

Arguments

img

target image, specified as an nrow-by-ncol-by-3 numeric array.

method

character, procedure for converting color to grayscale. Available methods are "average", "BT240", "brighter" (or maximum decomposition), "darker" (or minimu decomposition), "ITU" (or BT.709), "lightness" (or desaturation), "LUMA" (or BT.601), "RMY" and "weighted" (user provided).

weights

weights for red (R), green (G), and blue (B) channels. Required if method = "weighted".

Value

Grayscale image, returned as an nrow-by-ncol numeric matrix with values in the range [0,1].

RGB2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B channels.

Examples

  data(twelve)
  par(pty = "s", mfrow = c(1,3))
  plot(as.raster(twelve)) # in RGB
  title(main = "original", font.main = 1)

  x <- RGB2gray(twelve, method = "RMY")
  plot(as.raster(x)) # in grayscale
  title(main = "RMY", font.main = 1)

  x <- RGB2gray(twelve, method = "ITU")
  plot(as.raster(x)) # OMG! 12 is gone...
  title(main = "ITU", font.main = 1)

SpatialPack documentation built on Sept. 29, 2024, 5:06 p.m.