Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

RGB2gray

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

Tools for Assessment the Association Between Two Spatial Processes

v0.3-8196
GPL-3
Authors
Felipe Osorio [aut, cre] (<https://orcid.org/0000-0002-4675-5201>), Ronny Vallejos [aut] (<https://orcid.org/0000-0001-5519-0946>), Francisco Cuevas [ctb], Diego Mancilla [ctb]
Initial release
2020-09-21

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.