## Understanding Color Spaces and Color Space Conversion

The Image Processing Toolbox™ software typically represents colors as red, green, and blue (RGB) numeric values. However, there are other models besides RGB for representing colors numerically. The various models are referred to as color spaces because most of them can be mapped into a 2-D, 3-D, or 4-D coordinate system.

The various color spaces exist because they present color information in ways that make certain calculations more convenient or because they provide a way to identify colors that is more intuitive. For example, the RGB color space defines a color as the percentages of red, green, and blue hues mixed together. Other color models describe colors by their hue (shade of color), saturation (amount of gray or pure color), and luminance (intensity, or overall brightness).

The toolbox enables converting color data from one color space to another through mathematical transformations.

### RGB

The RGB color space represents images as an m-by-n-by-3 numeric array whose elements specify the intensity values of the red, green, and blue color channels. The range of numeric values depends on the data type of the image.

• For `single` or `double` arrays, RGB values range from [0, 1].

• For `uint8` arrays, RGB values range from [0, 255].

• For `uint16` arrays, RGB values range from [0, 65535].

The toolbox supports variations of the RGB color space.

RGB Color SpaceDescription
Linear RGBLinear RGB values are raw data obtained from a camera sensor. The value of R, G, and B are directly proportional to the amount of light that illuminates the sensor. Preprocessing of raw image data, such as white balance, color balance, and chromatic aberration compensation, are performed on linear RGB values.
sRGB

sRGB values apply a nonlinear function, called gamma correction, to linear RGB values. Images are frequently displayed in the sRGB color space because they appear brighter and colors are easier to distinguish. The parametric curve used to transform linear RGB values to the sRGB color space is:

f(u) = -f(-u),             u < 0

f(u) = c ⋅ u,             0 ≤ u < d

f(u) = a ⋅ uɣ + b,      u ≥ d,

where u represents one of the R, G, or B color values with these parameters:

a = 1.055

b = –0.055

c = 12.92

d = 0.0031308

ɣ = 1/2.4

Adobe RGB (1998) RGB values apply gamma correction to linear RGB values using a simple power function:

v = uɣ,           u ≥ 0

v = -(-u)ɣ,      u < 0,

with

ɣ = 1/2.19921875

### HSV

The HSV (Hue, Saturation, Value) color space corresponds better to how people experience color than the RGB color space does. For example, this color space is often used by people who are selecting colors, such as paint or ink color, from a color wheel or palette.

AttributeDescription
HHue, which corresponds to the color’s position on a color wheel. H is in the range [0, 1]. As H increases, colors transition from red to orange, yellow, green, cyan, blue, magenta, and finally back to red. Both 0 and 1 indicate red.
SSaturation, which is the amount of hue or departure from neutral. S is in the range [0, 1]. As S increases, colors vary from unsaturated (shades of gray) to fully saturated (no white component).
V

Value, which is the maximum value among the red, green, and blue components of a specific color. V is in the range [0, 1]. As V increases, the corresponding colors become increasingly brighter.

Illustration of the HSV Color Space

Note

MATLAB® and the Image Processing Toolbox software do not support the HSI color space (Hue, Saturation, Intensity). However, if you want to work with color data in terms of hue, saturation, and intensity, the HSV color space is very similar. Another option is to use the LCH color space (Luminosity, Chroma, and Hue), which is a polar transformation of the CIE L*a*b* color space — see Device-Independent Color Spaces.

Use the `rgb2hsv` and `hsv2rgb` functions to convert between the RGB and HSV color spaces.

### CIE 1976 XYZ and CIE 1976 L*a*b*

CIE 1976 XYZ and CIE 1976 L*a*b* are device-independent color spaces developed by the International Commission on Illumination, known by the acronym CIE. These color spaces model colors according to the typical sensitivity of the three types of cone cells in the human eye.

The XYZ color space is the original model developed by the CIE. The Y channel represents the luminance of a color. The Z channel approximately relates to the amount of blue in an image, but the value of Z in the XYZ color space is not identical to the value of B in the RGB color space. The X channel does not have a clear color analogy. However, if you consider the XYZ color space as a 3-D coordinate system, then X values lie along the axis that is orthogonal to the Y (luminance) axis and the Z axis.

The L*a*b* color space provides a more perceptually uniform color space than the XYZ model. Colors in the L*a*b* color space can exist outside the RGB gamut (the valid set of RGB colors). For example, when you convert the L*a*b* value [100, 100, 100] to the RGB color space, the returned value is [1.7682, 0.5746, 0.1940], which is not a valid RGB color. For more information, see Determine If L*a*b* Value Is in RGB Gamut.

AttributeDescription
L*Luminance or brightness of the image. Values are in the range [0, 100], where 0 specifies black and 100 specifies white. As L* increases, colors become brighter.
a*Amount of red or green tones in the image. A large positive a* value corresponds to red/magenta. A large negative a* value corresponds to green. Although there is no single range for a*, values commonly fall in the range [-100, 100] or [-128, 127).
b*Amount of yellow or blue tones in the image. A large positive b* value corresponds to yellow. A large negative b* value corresponds to blue. Although there is no single range for b*, values commonly fall in the range [-100, 100] or [-128, 127).

Device-independent color spaces include the effect of the illumination source, called the reference white point. The source imparts a color hue to the raw image data according to the color temperature of the illuminant. For example, sunlight during sunrise or sunset imparts a yellow hue to an image, whereas sunlight around noontime imparts a blue hue.

Use the `rgb2xyz` and `xyz2rgb` functions to convert between the RGB and XYZ color spaces. Use the `rgb2lab` and `lab2rgb` functions to convert between the RGB and L*a*b* color spaces.

The toolbox supports several related color space specifications that are better suited to some purposes than XYZ. For more information see Device-Independent Color Spaces.

### YCbCr

The YCbCr color space is widely used for digital video. In this format, luminance information is stored as a single component (Y) and chrominance information is stored as two color-difference components (Cb and Cr). Cb and Cr represent the difference between a reference value and the blue or red component, respectively. (YUV, another color space widely used for digital video, is very similar to YCbCr but not identical.)

AttributeDescription
YLuminance or brightness of the image. Colors increase in brightness as Y increases.
CbChrominance value that indicates the difference between the blue component and a reference value.
CrChrominance value that indicates the difference between the red component and a reference value.

The range of numeric values depends on the data type of the image. YCbCr does not use the full range of the image data type so that the video stream can include additional (non-image) information.

• For `single` or `double` arrays, Y is in the range [16/255, 235/255] and Cb and Cr are in the range [16/255, 240/255].

• For `uint8` arrays, Y is in the range [16, 235] and Cb and Cr are in the range [16, 240].

• For `uint16`, Y is in the range [4112, 60395] and Cb and Cr are in the range [4112, 61680].

Use the `rgb2ycbcr` and `ycbcr2rgb` functions to convert between the RGB and YCbCr color spaces.

### YIQ

The National Television Systems Committee (NTSC) defines a color space known as YIQ. This color space is used in televisions in the United States. This color space separates grayscale information from color data, so the same signal can be used for both color and black and white television sets.

AttributeDescription
YLuma, or brightness of the image. Values are in the range [0, 1], where 0 specifies black and 1 specifies white. Colors increase in brightness as Y increases.
IIn-phase, which is approximately the amount of blue or orange tones in the image. I in the range [-0.5959, 0.5959], where negative numbers indicate blue tones and positive numbers indicate orange tones. As the magnitude of I increases, the saturation of the color increases.
QQuadrature, which is approximately the amount of green or purple tones in the image. Q in the range [-0.5229, 0.5229], where negative numbers indicate green tones and positive numbers indicate purple tones. As the magnitude of Q increases, the saturation of the color increases.

Use the `rgb2ntsc` and `ntsc2rgb` functions to convert between the RGB and YIQ color spaces.

Because luminance is one of the components of the NTSC format, the RGB to NTSC conversion is also useful for isolating the gray level information in an image. In fact, the toolbox functions `rgb2gray` and `ind2gray` use the `rgb2ntsc` function to extract the grayscale information from a color image.