Developed with Giuseppe Marino
Given an RGB image, calculates its luma value Y (from YUV) as follows
Y = R .* 0.2989 + G .* 0.587 + B .* 0.114;
and then associates its level with the closest gray (or almost gray) color that you can produce, [y+r, y, y+b]
, where y
is a rounded from Y
, and r
and b
usually lie in the range of [-1, 1]
and [-2, 2]
(integers).
An approximate solution can be obtained by using a pre calculated look up table, indexed by the luma level in ascending order. This is similar to what is described in the bit stealing paper, although the look up table is slightly different.
The bitstealing
function requires two parameters and outputs the grayscale image.
out = bitstealing(img, l);
img
is the RGB imagel
is the level of variation allowed, which can be either1
, which makesb = [-1, 1]
, where colors are closer to pure gray (766 shades available)2
, which makesb = [-2, 2]
(1274 shades available)3
, which makesb = [-1, 1]
andr = [-1, 1]
(2294 shades available)4
, which makesb = [-2, 2]
andr = [-1, 1]
, where colors can be further from pure gray (3816 shades available)