YUV420 to RGB Conversion Conversion

65 views (last 30 days)
Abdulllah on 29 Aug 2019
Commented: yang li on 7 Jan 2022
I am using this code to read YUV420 and convert to RGB. Because I have YUV420.YUV image file but that code is for video therefore, I only read 1frame. Then I get YUV as Y as the full size but U and V as half size as described at wikiepdia. I then resze the images to the full size of the image and apply YUV to RGB coversion. But the RGB images are not in correct colours. I have attached the files so that you can run and see what is the problem. Here is the YUV filetulips_yuv420_inter_planar_qcif.yuv
I have two more questions;
Firstly, The size of the "stream" for one frame should be equal to 1.5*the size of the Y but it is very large whether I use uint8 or uint16 to read the file.
Secondly, If I have 10bit YUV420 file how to I to modify this code to show correct RGB.
fname = 'tulips_yuv420_inter_planar_qcif.yuv';
width = 176;
height = 144;
fid = fopen(fname,'r'); % Open the video file
% stream = fread(fid,'uint16'); % uint16
stream = fread(fid); % uint8
length = 1.5 * width * height; % Length of a single frame
The full code is attached kindly help to sort out the issue.
  1 Comment
yang li
yang li on 7 Jan 2022
if you still neet this fuction,you can use this code:
filename = 'Y420AA00.yuv';
senser_data_y = (reshape(senser_data(1:1920*1080),1,1920,1080));
senser_data_uv = (reshape(senser_data(1920*1080+1:end),2,1920/2,1080/2));
senser_data_y = uint8(permute(senser_data_y ,[3,2,1]));
senser_data_uv = uint8(permute(senser_data_uv,[3,2,1]));
senser_data_u = imresize(senser_data_uv(:,:,1),size(senser_data_y),'bicubic');
senser_data_v = imresize(senser_data_uv(:,:,2),size(senser_data_y),'bicubic');
senser_data_2d_yuv = cat(3,senser_data_y,senser_data_u,senser_data_v);
senser_data_2d = ycbcr2rgb(senser_data_2d_yuv);

Sign in to comment.

Answers (1)

Pratheek Punchathody
Pratheek Punchathody on 7 Apr 2021
As per my understanding in case of YUV Color model, Y stands for the luminance component and U and V are the chrominance component. YUV is used for the specific analog encoding of color information and YCbCr is used for digital encoding of color information suited for video and still image compression and transmission.
In MATLAB, function called "ycbcr2rgb" which converts YCbCr color values to RGB color space. It allows to accept single | double | uint8 | uint16 data types of inputs. Converted RGB color values, returned as a numeric array of the same size as the input. The output data type is the same as the input data type.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by