View Raw SPL
/*****************************************************************************
* *
* RGB2MONO.SPL Copyright (C) 2000, 2019 DSP Development Corporation *
* All Rights Reserved *
* *
* Author: Randy Race *
* *
* Synopsis: Converts an RGB image to monochrome *
* *
* Revisions: 3 May 2000 RRR Creation *
* 23 Apr 2019 RRR RGB coefficients *
* *
*****************************************************************************/
#if @HELP_RGB2MONO
RGB2MONO
Purpose: Converts an RGB image to 8 bit monochrome
Syntax: RGB2MONO(image, coef)
image - A series containing the source image.
coef - Optional. A scalar or series, the RGB scaling
coefficients:
0: {0.2989, 0.5870, 0.1140}, CCIR 601 coefficients
(default)
1: {0.2126, 0.7152, 0.0722}, BT 709 coefficients
Returns: An array, an 8 bit monochrome image
Example:
W1: density(spline2(ravel(gnorm(100,1),10), 8));rainbow()
W2: image24(W1);
W3: rgb2mono(W2)
W1 contains an image of a random surface shaded with
the colors of the spectrum ranging from red to blue. W2
converts the image into a 24 bit color image.
W3 contains the monochrome image displayed in the
current colormap. The monochrome image contains values
ranging from 0 to 255.
Example:
W1: density(spline2(ravel(gnorm(100,1),10), 8));rainbow()
W2: image24(W1);
W3: rgb2mono(W2, 1)
Same as above, except the BT 709 coefficients are used to scale
the monochrome image.
Remarks:
RGB2MONO converts the 24 bit image into 8 bit integer
values ranging from 0 to 255. The resulting image is
displayed using the current colormap.
By default, CCIR 601 coefficients are used to scale
each RGB pixel as follows:
mono = 0.2989 * r + 0.5870 * g + 0.1140 * b
where r, g, b are the red, green and blue components of the
original RGB image.
If COEF is a series, it must be of size 3x1.
See RGB2GRAY to convert an image to a 24 bit grayscale image.
See Also:
Density
Getcolormap
Getrgb
Image24
Iminterp
Rgb2gray
Rgbimage
Spline2
Interp2
#endif
/* convert RGB image to monochrome */
rgb2mono(a, coef)
{
local r, g, b, m;
if (argc < 2)
{
if (argc < 1) a = refwindow(w0);
coef = 0;
}
if (not(rgb2mono_isimage(a))) error("rgb2mono - image required");
/* RGB components */
(r, g, b) = getrgb(a);
/* scaling coefficients */
if (isscalar(coef))
{
coef = (coef > 0) ? {0.2126, 0.7152, 0.0722} : {0.2989, 0.5870, 0.1140};
}
/* convert to 0-255 monochrome */
m = int(255 * (coef[1] * r + coef[2] * g + coef[3] * b));
/* set to image (density) */
rgb2mono_setimage(m);
return(m);
}
/* true if input is an image */
rgb2mono_isimage(a)
{
getplottype(a) == 5;
}
/* sets to image */
rgb2mono_setimage(a)
{
setplottype(a, 5);
}