文中，利用Matlab(可能需要Image Tool Box的支持)，对利用RGB颜色空间保存的JPEG图片，进行了颜色空间的转换(rgb2ycbcr)与分离(3个颜色通道)，将实例图片的Y U V成分分别以灰度值及彩色(将其他两个通道成分的成分置为0，然后用ycbcr2rgb进行逆转换)的形式表现了出来，比较直观。
By RuanYuan.Net@admin 20120724
Color space is a complicated topic. Colors don’t really exist, like dust does. We human being use colors to describe what we see. The most common way to describe what we see in terms of color is using combination of red, green and blue, which is referred as RGB color space.
A color space is simply a model of representing what we see in tuples. YCbCr is one of the popular color space in computing. It represents colors in terms of one luminance component/luma (Y), and two chrominance components/chroma(Cb and Cr).
YUV, Y’UV, YCbCr, Y’CbCr, YPbPr… All these terms cause lots of confusion. YUV and Y’UV are traditionally for analog encoding of color information in television system, it uses different conversion constants from YCbCr; while YCbCr or Y’CbCr are used for digital encoding of color information in computing systems; and YPbPr is the analog counterpart of YCbCr. Nowadays, YUV is also used in the digital video context, in which case it is almost equivalent to YCbCr.
Here we negnect the terminologies and focuses on understanding the colorspace and its usage in the context of computing world. From this point onwards, we’ll call it YCbCr.
Why YCbCr Color Space?
Study shows human eyes are sensitive to luminance, but not so sensitive to chrominance. For example, given an image below,
Figure 1. A Color Image
One can use the matlab code below to display its Y, Cb, Cr component as color images or gray scale images.
%a program to display the image’s ycbcr component
function  = ycctest(imageName)
rgb = imread(imageName);
ycbcr = rgb2ycbcr(rgb); %convert to yuv space
%display the y component as a color image
ycbcry = ycbcr;
ycbcry(:,:,2) = 0;
ycbcry(:,:,3) = 0;
rgb1 = ycbcr2rgb(ycbcry);
% display the cb component as a color image
ycbcru = ycbcr;
ycbcru(:,:,1) = 0;
ycbcru(:,:,3) = 0;
rgb2 = ycbcr2rgb(ycbcru);
% display the cr component as a color image
ycbcrv = ycbcr;
ycbcrv(:,:,1) = 0;
ycbcrv(:,:,2) = 0;
rgb3 = ycbcr2rgb(ycbcrv);
%display the y, cb, cr component as gray scale image
Save the program as ycctest.m, and save the image as test.jpg. Then execute the program in matlab by typing command,
You’ll get the 3 color images for Y, Cb and Cr component from left to right as,
Figure 2. Color Images formed by Y, Cb and Cr Components
You’ll also get another 3 gray-scale images for Y, Cb and Cr component from left to right,
Figure 3. Gray-scale Images formed by Y, Cb and Cr Components
It’s not difficult to tell that our eyes perceive more info from the left most images in figure 2 and figure 3, which is formed by Y component of figure 1.
YCbCr color space makes use of this fact to achieve more efficient representation of scenes/images. It does so by separating the luminance and chrominance components of a scene, and use less bits for chrominance than luminance. The details of how to use less bits to represent chrominance is covered in Sub-sampling section below.
How does the Conversion Work?
The YCbCr image can converted to/from RGB image. There’re several standards defined for the conversion at different context. The conversion below is based on the conversion used in JPEG image compression.
The conversion can be expressed as equations below.
From 8-bit RGB to 8-bit YCbCr:
From 8-bit YCbCr to 8-bit RGB:
The representation of YCbCr separates the luminance and chrominance, so the computing system can encode the image in a way that less bits are allocated for chrominance . This is done through color subsampling, which simply encodes chrominance components with lower resolution.
Here we cover four commonly used subsampling schemes: 4:4:4, 4:2:2, 4:2:0, and 4:1:1.
These four schemes are illustrated by the figure below ,
Figure 4. Color Subsampling
The 4:4:4 is actually full resolution in both horizontal and vertical directions, there’s no subsampling done. 4:2:2 requires 1/2 resolution in horizontal direction; 4:1:1 requires 1/4 resolution in horizontal direction; and 4:2:0 means 1/2 resolution in both horizontal and vertical directions.
There’re also 4:4:0 and 4:1:0 schemes, interested readers could refer to reference 2.
Note that the sub-sampling process is lossy when comparing the processed image with the original image. The amount of info left can be calculated by summing up all components and divide by 12 (or 16, if alpha/transparency component exists). For the four subsampling schemes we mentioned, the amount of info left is 100%, 8/16=50%, 6/16 = 37.5%, 6/16 = 37.5%. In this way, the computing system could represent the image with less bits.
But how the image is reconstructed for display? Normally the image requires a Y, Cb, Cr component at every pixel. The reconstruction is done through interpolation or simply use the CbCr value of nearby pixel if the pixel doesn’t have CbCr values. The details are not covered in this post.
YCbCr is a commonly used color space in digital video domain. Because the representation makes it easy to get rid of some redundant color information, it is used in image and video compression standards like JPEG, MPEG1, MPEG2 and MPEG4.
Basics of Video: http://lea.hamradio.si/~s51kq/V-BAS.HTM
Chrominance Subsampling: http://dougkerr.net/pumpkin/articles/Subsampling.pdf
JPEG File Interchange Format: http://www.w3.org/Graphics/JPEG/jfif3.pdf