RGB to YCbCr conversion (2024)

One important task in image processing applications is the color space conversion. Real-time images and videos are stored in RGB color space, because it is based on the sensitivity of color detection cells in the human visual system. In digital image processing the YCbCr color space is often used in order to take advantage of the lower resolution capability of the human visual system for color with respect to luminosity. Thus, RGB to YCbCr conversion is widely used in image and video processing [1].

Given a digital pixel represented in RGB format, 8 bits per sample, where 0 and 255 represents the black and white color, respectively, the YCbCr components can be obtained according to equations (1) to (3):

Equations (1) to (3): RGB to YCbCr conversion, source: [1]

Approximating the equations (1) to (3) to the nearest integer and replacing multiplication and division by shift registers, the equations (4) to (6) are obtained:

Equations (4) to (6)

Image and Video consumes a lot of data. One of the reasons is because they are represented in the RGB format. However, is not worth to store or transmit information in this color space representaion, once it has a large bandwidth. Thus all the pixels should be converted to YCbCr to accomplish that.

A practical example

To understand the effect of converting RGB to YCbCr, we will use the Figure (1).

Figure (1): rgb.png

Using Matlab or Octave can be done some processing on the image. For example, the code below reads the image file "rgb.png":

% RGB to YCbCr with MatlabI = imread('rgb.png');figure(1), imshow(I);

Matlab code to read an image

A color image has three channels (red, green and blue components). To access each component of the image, the Matlab code below can be used:

% RGB to YCbCr with MatlabR = I(:,:,1);G = I(:,:,2); B = I(:,:,3); figure(2), imshow(R), figure(3),imshow(G),figure(4), imshow(B);

Matlab code to access each channel of the image

Each component of the image can be seen in Figures (2) to (4). Note that each channel has only the each corresponding color.

Figure (2): the red channel Figure (3): the green channel Figure (4): the blue channel

The Matlab provides the function rgb2ycbcr that converts an image from RGB to YCbCr. The script to convert the image from one space to another and to access each component (Y, Cb and Cr) can be seen below:

% RGB to YCbCr with MatlabI2 = rgb2ycbcr(I);Y = I2(:,:,1); Cb = I2(:,:,2); Cr = I2(:,:,3); figure(5),imshow(I2), figure(6), imshow(Y), figure(7), imshow(Cb),figure(8), imshow(Cb);

Matlab code to convert from RGB to YCbCr and to access each channel of the converted image

The converted image in the YCbCr space can be seen in Figure (5). It has three components: the lumminance Y, the blue difference Cb and the red difference Cr.

Figure (5): the image in the YCbCr space

The Y component only filters the luminance (brightness) of the image (see Figure (6)); the Cb and Cr components subtract the red and blue colors, respectively, from the image (see Figures (7) and (8)).

Figure (6): the Y component Figure (7): the Cb component Figure (8): the Cr component

Hardware implementation

The hardware implementation of the RGB to YCbCr converter may be done using equations (4) to (6). The computation is done using only shift registers and adders. Each component is described in Figures (9) to (11).

Figure (9): RGB to Y block Figure (10): RGB to Cb block Figure (11): RGB to Cr block

The SystemVerilog implementation of the RGB2YCbCr converter can be seen below:

module RGB2YCbCr(rgb_if.in in, ycbcr_if.out out); always @(posedge in.clk)begin if(in.rst) begin out.Y <= 0; out.Cb <= 0; out.Cr <= 0; end else begin out.Y <= 16+(((in.R<<6)+(in.R<<1)+(in.G<<7)+in.G+(in.B<<4)+(in.B<<3)+in.B)>>8); out.Cb <= 128 + ((-((in.R<<5)+(in.R<<2)+(in.R<<1))-((in.G<<6)+(in.G<<3)+(in.G<<1))+(in.B<<7)-(in.B<<4))>>8); out.Cr <= 128 + (((in.R<<7)-(in.R<<4)-((in.G<<6)+(in.G<<5)-(in.G<<1))-((in.B<<4)+(in.B<<1)))>>8); end endendmodule : RGB2YCbCr

SystemVerilog code of the RGB2YCbCr converter (RGB2YCbCr.sv)

The module of the converter has two interfaces:

  • The RGB interface rgb_if: contains the three components of the RGB pixel, plus the clock and reset signals
  • The YCbCr interface ycbcr_if: contains the three components of the YCbCr pixel, plus the clock and reset signals
interface rgb_if(input logic clk, rst); logic [7:0] R, G, B; modport in(input clk, rst, R, G, B);endinterface

SystemVerilog code of the RGB interface (rgb_if.sv)

interface ycbcr_if(input clk, rst); logic [7:0] Y, Cb, Cr; modport out(input clk, rst, output Y, Cb, Cr);endinterface

SystemVerilog code of the YCbCr interface (ycbcr_if.sv)

The SystemVerilog implementation of the testbench of the RGB2YCbCr is shown below. Note that the ROM memory module provides the pixels to stimulate the RGB2YCbCr module.

`include "RGB2YCbCr.sv"`include "rgb_if.sv"`include "ycbcr_if.sv"parameter N_ADDR = 8;module ROM(input logic [2:0]address, output logic [23:0]data); always_comb begin case (address) 00: data <= 0; 01: data <= (123<<16)+(88<<8)+60; 02: data <= (100<<16)+(200<<8)+110; 03: data <= (50<<16)+(50<<8)+10; 04: data <= (251<<16)+(135<<8)+160; 05: data <= (185<<16)+(69<<8)+45; 06: data <= (196<<16)+(188<<8)+201; 07: data <= (132<<16)+(168<<8)+74; endcase endendmodulemodule top; logic clk; logic rst; logic [2:0] addr; logic [23:0] data; int i; initial begin clk = 0; rst = 1; #22 rst = 0; for(i = 0; i <= N_ADDR; i++)begin @(posedge clk) addr <= i; in.R <= data[23:16]; in.G <= data[15:8]; in.B <= data[7:0]; $display("R = %d G = %d B = %d", in.R,in.G, in.B); @(posedge clk) $display("Y = %d Cb = %d Cr = %d", out.Y, out.Cb, out.Cr); end $finish(); end always #5 clk = !clk; rgb_if in(clk, rst); ycbcr_if out(clk, rst); RGB2YCbCr rtl(in, out); ROM image(.address(addr), .data(data)); endmodule

SystemVerilog code of the RGB2YCbCr testbench (top.sv)

Also available in GitHub.

References

[1] Keith Jack. Video demystified: a handbook for the digital engineer. Elsevier, 2011

RGB to YCbCr conversion (2024)

FAQs

What is the conversion formula for RGB to YCbCr? ›

Description. YCBCR = rgb2ycbcr( RGB ) converts the red, green, and blue values of an RGB image to luminance (Y) and chrominance (Cb and Cr) values of a YCbCr image.

Why do we have to convert RGB to YcrCb? ›

But medical research proved that the human eye has different sensitivity to colour and brightness. Thus there came about the transformation of RGB to YCbCr. Y: Luminance; Cb: Chrominance-Blue; and Cr: Chrominance-Red are the components. Luminance is very similar to the grayscale version of the original image.

How do you convert YCbCr to RGB? ›

RGB = ycbcr2rgb( YCBCR ) converts the luminance (Y) and chrominance (Cb and Cr) values of a YCbCr image to red, green, and blue values of an RGB image.

How to convert RGB image to YCbCr in Matlab? ›

Convert Image from RGB to YCbCr

RGB = imread("board. tif"); Convert the image to YCbCr. imshow(YCBCR); title("Image in YCbCr Color Space");

Why YCbCr is better than RGB? ›

YCbCr video is considered a form of lossless compression, as the elements of the original RGB signal that are removed in creating YCbCr are picture elements that humans aren't able to see at normal viewing distances.

Is YCbCr better than RGB high? ›

Video quality on Apple TV can be dramatically affected by the choice between YCbCr and RGB High. YCbCr is recommended for compatibility with most televisions and is associated with better color accuracy when properly calibrated.

Should I choose RGB or YCbCr? ›

RGB is the traditional computer format. One is not superior to the other because each has it's own strengths and weaknesses. YCbcr is preferred becuause it is the native format, However many displays (almost all DVI inputs) only except RGB.

Should I use YCbCr or RGB for gaming? ›

There's basically no difference between RGB 4:4:4 and YCbCr 4:4:4 IF the latter option supports full range. When it doesn't, you're limited to a color ramp of 16-235 vs 0-255. But, you'll always want to use RGB on computer monitors because it's been the standard since forever.

What's the difference between RGB and YCbCr? ›

One of two primary color spaces used to represent digital component video (the other is RGB). The difference between YCbCr and RGB is that YCbCr represents color as brightness and two color difference signals, while RGB represents color as red, green and blue.

What is the use of YCbCr? ›

YCbCr Color Model

YCbCr is an alternate color model that defines color data using a luminance component (Y) and two chrominance components (Cb and Cr). It is commonly used in digital imaging and video scenarios. The term YCbCr is often used interchangeably with YUV, although the two are technically distinct.

What is the YCbCr format? ›

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).

What is the value of YCbCr for white? ›

For example, the scaling and offset applied to the Y′ component per specification (e.g. MPEG-2) results in the value of 16 for black and the value of 235 for white when using an 8-bit representation. The standard has 8-bit digitized versions of CB and CR scaled to a different range of 16 to 240.

How to convert RGB to YCbCr in Python? ›

RGB to YcrCb conversion
  1. import cv2 import numpy as np from matplotlib import pyplot as plt.
  2. fn3= 'tria.jpg' img1 = cv2. imread(fn3, cv2. CV_LOAD_IMAGE_UNCHANGED)
  3. img2=np. zeros(img1. shape,np. uint8) img2[:,:,0]=img1[:,:,2] img2[:,:,1]=img1[:,:,1] img2[:,:,2]=img1[:,:,0] plt. ...
  4. transcol=cv2. cvtColor(img1, cv2. cv.

How to convert RGB image to black and white in Matlab? ›

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.

What is the range of YCbCr? ›

1. The Y value ranges from 16 to 235 (220 levels), with 16 being black. 2. The Cr and Cb values range from 16 to 240 with 128 as the mid-point.

How do you convert RGB to decimal? ›

RGB(A) is a 256-bit color, so by that logic then any channel can be 0 to 255. Knowing this, we can say a color is 255(r),127(g),25(b),255(a) you can do this: Color color = new Color(r/255.0F,g/255.0F,b/255.0F,a/255.0F); The equation is very basic math.

How is RGB to CMY conversion done? ›

So conversions between RGB and CMY are trivial; you just subtract the value from (1, 1, 1) to get the new value. E.g., (0.2, 0.7, 0,3) in one system is (0.8, 0.3, 0.7) in the other system.

How do I convert RGB mode? ›

Open the Image in Photoshop and select Image> Mode > RGB Color to convert it to RGB color mode. When converting an image to RGB, the actual colors in the Image will not change. The colors will be interpreted differently by Photoshop as a result.

Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6301

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.