lin2rgb
Apply gamma correction to linear RGB values
Description
Examples
Plot Gamma Curve of sRGB and Adobe RGB
Define a range of linear values. This vector defines 257 equally spaced points between 0 and 1.
lin = linspace(0,1,257);
Apply gamma correction to the linear values based on the sRGB standard. Then apply gamma correction to the linear values based on the Adobe RGB (1998) standard.
sRGB = lin2rgb(lin); adobeRGB = lin2rgb(lin,'ColorSpace','adobe-rgb-1998');
Plot the gamma-corrected curves.
figure plot(lin,sRGB,'b',lin,adobeRGB,'r') title('Gamma-Corrected vs. Linear Values') legend('sRGB','Adobe RGB (1998)','Location','southeast')
For an alternative visualization, plot color bars representing each color space.
cb_lin = ones(30,257) .* lin; cb_sRGB = ones(30,257) .* sRGB; cb_adobeRGB = ones(30,257) .* adobeRGB; figure subplot(3,1,1); imshow(cb_lin); title('Linear RGB') subplot(3,1,2); imshow(cb_sRGB); title('sRGB'); subplot(3,1,3); imshow(cb_adobeRGB); title('Adobe RGB (1998)');
The gamma-corrected color spaces get brighter more quickly than the linear color space, as expected.
Apply sRGB Gamma Correction to Linear RGB Image
Open an image file containing minimally processed linear RGB intensities.
A = imread('foosballraw.tiff');
The image data is the raw sensor data after correcting the black level and scaling to 16 bits per pixel. Interpolate the intensities to reconstruct color by using the demosaic
function. The color filter array pattern is RGGB.
A_demosaiced = demosaic(A,'rggb');
Display the image. To shrink the image so that it appears fully on the screen, set the optional initial magnification to a value less than 100.
figure imshow(A_demosaiced,'InitialMagnification',25) title('Sensor Data Without sRGB Gamma Correction')
The image appears dark because it is in the linear RGB color space. Apply gamma correction to the image according to the sRGB standard, storing the values in double precision.
A_sRGB = lin2rgb(A_demosaiced,'OutputType','double');
Display the gamma-corrected image, setting the optional magnification.
figure imshow(A_sRGB,'InitialMagnification',25) title('Sensor Data With sRGB Gamma Correction');
The gamma-corrected image looks brighter than the linear image, as expected.
Input Arguments
A
— Linear RGB color values
numeric array
Linear RGB color values, specified as a numeric array in one of the following formats.
c-by-3 colormap. Each row specifies one RGB color value.
m-by-n-by-3 image
m-by-n-by-3-by-p stack of images
Data Types: single
| double
| uint8
| uint16
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: B = lin2rgb(I,ColorSpace="adobe-rgb-1998")
applies
gamma correction to an image, I
, according to the Adobe RGB
(1998) standard.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: B = lin2rgb(I,"ColorSpace","adobe-rgb-1998")
applies
gamma correction to an image, I
, according to the Adobe RGB
(1998) standard.
ColorSpace
— Color space of output image
"srgb"
(default) | "adobe-rgb-1998"
| "prophoto-rgb"
Color space of the output image, specified as
"srgb"
, "adobe-rgb-1998"
, or
"prophoto-rgb"
.
Data Types: string
| char
OutputType
— Data type of output RGB values
"double"
| "single"
| "uint8"
| "uint16"
Data type of the output RGB values, specified as
"double"
, "single"
,
"uint8"
, or "uint16"
. By
default, the output data type is the same as the data type of
A
.
Data Types: string
| char
Output Arguments
B
— Gamma-corrected RGB image
numeric array
Gamma-corrected RGB image, returned as a numeric array of the same size as
the input A
.
Algorithms
Gamma Correction Using the sRGB Standard
The gamma correction to transform linear RGB tristimulus values into sRGB tristimulus values is defined by the following parametric curve:
f(u) = -f(-u), u < 0
f(u) = c ⋅ u, 0 ≤ u < d
f(u) = a ⋅ uɣ + b, u ≥ d,
where u represents a color value with these parameters:
a = 1.055
b = –0.055
c = 12.92
d = 0.0031308
ɣ = 1/2.4
Gamma Correction Using the Adobe RGB (1998) Standard
The gamma correction to transform linear RGB tristimulus values into Adobe RGB (1998) tristimulus values uses a simple power function [2]:
v = uɣ, u ≥ 0
v = -(-u)ɣ, u < 0,
with
ɣ = 1/2.19921875
Gamma Correction Using the ProPhoto (ROMM RGB) Standard
The gamma correction to transform linear RGB tristimulus values into ProPhoto tristimulus values is defined by the following parametric curve [3]:
f(u) = 0, u < 0
f(u) = u/16, 0 ≤ u < 16*Et
f(u) = u1/1.8 + b, 16*Et < u < 1,
f(u) = 1, u ≥ 1
with
Et = 1/512
References
[1] Ebner, Marc. "Gamma Correction." Color Constancy. Chichester, West Sussex: John Wiley & Sons, 2007.
[2] Adobe Systems Incorporated. "Inverting the color component transfer function." Adobe RGB (1998) Color Image Encoding. Section 4.3.5.2, May 2005, p.12.
[3] ISO 22028-2:2013 Photography and graphic technology — Extended colour encodings for digital image storage, manipulation and interchange — Part 2: Reference output medium metric RGB colour image encoding (ROMM RGB). https://www.iso.org/standard/56591.html
Version History
Introduced in R2017bR2022b: Support for ProPhoto (ROMM RGB) color space
lin2rgb
now supports the ProPhoto (ROMM RGB) color space,
which has a wider gamut than the sRGB and Adobe RGB 1998 color spaces. To use the
ProPhoto color space, specify the ColorSpace
name-value
argument as "prophoto-rgb"
.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)