Click here to Skip to main content
Click here to Skip to main content

RGB to YUV conversion with different chroma sampling using C++,

, 10 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
RGB to YUV conversion with different chroma sampling using C++.

Introduction      

 

This article is for beginners in image processing especially those who like to do in C++. Here I am try to describes the logic of RGB to YCbCr conversion and Chroma sampling. We can find many articles over the internet, which explains the logic and theories behind this conversion. But it is hard to find a C++ code that uses different Chroma sampling schemes. One more thing, this is not a robust code, if you want to use it in your project please re-write it according to your skill.

According to wiki “YUV is a color space typically used as part of a color image pipeline. It encodes a color image or video taking human perception into account, allowing reduced bandwidth for chrominance components, thereby typically enabling transmission errors or compression artifacts to be more efficiently masked by the human perception than using a "direct" RGB-representation…. “You can read more from http://en.wikipedia.org/wiki/YUV.

In simple sentence, YUV is an image compression technique or part of an image compression technique which omits some details from the image, which cannot be detected by human eye. In YUV, Y represents luminance or light intensity of the image; U and V are the color components. During image compression we cannot omit any luminance[Y] information but we can omit some color or chrominance components based on Chroma sub sampling schemas.

Chroma sub sampling is a measure of YUV compression. We can select different Chroma sampling schemes based on our requirements. http://en.wikipedia.org/wiki/Chroma_sampling.

Before going to the  next line you should ensure that you have a better understanding about the YUV color space.

How it Works

It’s a simple arithmetic to convert RGB to YUV.  The formula is based on the relative contributions that red, green, and blue make to the luminance and chrominance factors. There are several different formulas in use depending on the target monitor.

Here I am chooses ITU-R version formula

RGB  to YUV

Y = 0.299 * R + 0.587 * G + 0.114 * B

U = -0.1687 * R – 0.3313* G + 0.5 * B +        128

V = 0.5 * R – 0.4187 * G – 0.813 * B + 128  

  

YUV to RGB 

R = Y+ 0 * U + 1.13983 * V

G = Y+ -0.39465 * U + -0.58060 * V

B = Y+ -0.03211 * U + 0 * V

 

Now we have the YUV data and it’s time to apply Chroma sampling, all about Chroma sampling is shown in the below image.

The available chroma samplings are given below 

Chroma Sampling

Output Y size corresponding to a 4x4 block

4:4:4

Y - for each pixel, Cr and Cb-For each pixel

4:4: 0

Y - for each pixel, Cr and Cr For each pixel of alternate rows

4:2:2

Y - for each pixel, Cr and Cr For each pixel of alternate columns

4:2: 0

Y - for each pixel, Cr and Cr For each pixel of alternate columns and rows

4:1:1

Y - for each pixel, Cr and Cr For each pixel of every 4th columns

4:1: 0

Y - for each pixel, Cr and Cr For each pixel of every 4th columns and rows

For a more detailed look please read the below link, 

402391/2.gif

This image is taken from the article “Chrominance Sub sampling in Digital Images” by Douglas A. Kerr

You can select different chroma sampling and image views from corresponding combo boxes in the GUI as shown below

          

Depending upon the chroma sampling the reconstructed image may have some small errors like figure below figure[it uses chroma sampling 4:1: 0 ], we can apply some filtering methods to enhance the image. 

  

Using the code   

The RGB to YUV conversion takes place in the RGBtoYUV class. The methods available in the  RGBtoYUV  class are given below.

bool CovertRGBtoYUV( byte* pbyData_i, int nWidth_i, int nHeight_i, CString csChromaSampling_i );
byte* GetYData()
byte* GetCbData();
byte* GetCrData();
byte* ConvertYUVtoRGB( ) 

For displaying the image all output images are converted to gray scale by filling the three channels [RGB] with same data.  

Points of Interest

If you understood the above concept, you are not far from a JPEG encoder. 

 

History 

  • 06/12/2012 

    First release.  
  • 07/11/2012

     Second release.  

     

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Binu MD
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionMy vote of 5 PinprofessionalMichael N. Haephrati10-Jul-14 14:00 
Questionabout save the file Pinmemberliaoyuandeyehuo20-Apr-13 18:00 
QuestionMay I ask about executable (windows x86) binary that can output a jpg in yv12 or yuv colorspace. PinmemberBenjamin Netayanhu9-Feb-13 21:46 
GeneralMy vote of 4 [modified] PinmemberKnightxie10-Jan-13 20:18 
GeneralMy vote of 5 Pinmemberandy1985110516-Dec-12 19:16 
GeneralMy vote of 5 Pinmemberasunbb21-Jul-12 4:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 11 Jul 2012
Article Copyright 2012 by Binu MD
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid