View a YUYV encoded image in MATLAB
Show older comments
Hi all,
I am kinda new to handling YUYV encoded image in matlab. I used a sensor to record data and generate a *.bag file. The bad file contains color, depth and infrared channel. Using basic command I am able to read depth and infrared channels but the colour images are encoded with YUYV format.
The data screenshot below is also a row vector whereas most of the resources requires to be fed a (image.height, image.width,3) variable, for conversion to RGB image.
How should I proceed to view this image in matlab?

Accepted Answer
More Answers (1)
Hi Chhayank,
To convert a yuv encoded image to rgb you can first
- Reshape the YUYV data into a matrix with dimensions [height, width*2]. Each pixel in the YUYV format consists of two bytes, representing Y (luma) and U/V (chroma) values.
- Extract the Y (luma) and U/V (chroma) values from the reshaped matrix.
then use this function:
function images=yuv2rgb(YUV)
Y = double(YUV(:,:,1));
U = double(YUV(:,:,2));
V = double(YUV(:,:,3));
% Conversion Formula
R =uint8( 1 * Y + 0 * U + 1.4022* V);
G = uint8(1 * Y -0.3456 * U -0.7145* V);
B= uint8(1 * Y + 1.7710 * U + 0 * V );
image=cat(3,uint8(R),uint8(G),uint8(B));
images=ycbcr2rgb(YUV);
end
6 Comments
Walter Roberson
on 10 Jul 2023
The original data is not a 3d YUV array with at least 3 panes. The original data is a vector that represents YUYV data.
looking at the size of the vector 614400 and dividing by (640 * 480) leads us to the suspicion that each YUYV pixel occupies 2 bytes of data. That hints at 4 bits per field and a need to demosaic
Walter Roberson
on 10 Jul 2023
Edited: Walter Roberson
on 18 Jul 2023
I find a Mathworks page that says YUYV is also known as YCbCr 4:2:2
Aniketh
on 10 Jul 2023
The last line in the function I provided, did use ycbcr2rgb for the conversion, forgive me if that's wrong, but doesn't that do the same thing or is it different?
Chhayank Srivastava
on 10 Jul 2023
Edited: Chhayank Srivastava
on 10 Jul 2023
Chhayank Srivastava
on 10 Jul 2023
The given code attempts to do the conversion twice, neither of which will work directly.
- Directly applying the inverse transformation for YCbCr alone won't work without accounting for the chroma offsets.
- ycbcr2rgb() expects its inputs to be a MxNx3 image or a Mx3 color table. That's not the case until the data is split/replicated/reshaped/concatenated (which Aniketh did suggest).
In order to do all that reshaping, it's necessary to know for certain how the data is stored. At this point, we have three options:
- find some sort of documentation which specifies how the file is encoded
- work with a sample image to verify the encoding details
- keep guessing
If 1 or 2 are desirable, either attach a sample image or include some information about whether the originating device is documented. I don't know about Walter or Aniketh, but I don't have the Robotics Toolbox, so if the raw data can be exported as a .mat file, that would make it available to people who can't open the .bag file.
As for #3, my guess is included below.
Categories
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
