View a YUYV encoded image in MATLAB

29 visualizaciones (últimos 30 días)
Chhayank Srivastava
Chhayank Srivastava el 10 de Jul. de 2023
Editada: Chhayank Srivastava el 18 de Jul. de 2023
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?
DATA

Respuesta aceptada

DGM
DGM el 10 de Jul. de 2023
Editada: DGM el 10 de Jul. de 2023
Here's my guess, assuming that everything is as simple as it appears. There's a good chance this isn't exactly right.
If there are issues with the way the data is written (striped/tiled), it's easiest to try to suss out the luma data first. This is a convenience because luma alone is a viewable image. If you can find luma, you can use that info to figure out where Cb,Cr are.
% i'm assuming that the stream has been read as a uint8 vector
yuyv = zeros(614400,1,'uint8'); % fake data
% this can probably be read programmatically
sz = [480 640];
% separate channels from stream
yuyv = reshape(yuyv,[],2); % rearrange [luma chroma]
Y = yuyv(:,1); % every other byte is luma
Cb = yuyv(1:2:end,2); % chroma elements alternate
Cr = yuyv(2:2:end,2);
% reshape into 2D
Y = reshape(Y,[],sz(1)).';
Cb = reshape(Cb,[],sz(1)).'; % half horizontal resolution
Cr = reshape(Cr,[],sz(1)).'; % half horizontal resolution
% deal with subsampled chroma
% i'm just going to replicate
Cb = imresize(Cb,sz,'nearest');
Cr = imresize(Cr,sz,'nearest');
% concatenate and convert
RGB = ycbcr2rgb(cat(3,Y,Cb,Cr));
  1 comentario
Chhayank Srivastava
Chhayank Srivastava el 18 de Jul. de 2023
Editada: Chhayank Srivastava el 18 de Jul. de 2023
Thanks you,
I got the hint from your previous response, your code with my dataset generates below image(maybe a peculiarity with my data) but your approach/hint certainly helped.
I read from the below sources and found them to be quite helpful (incase someone else reading it wants to read for themselves)
and made my own version of this.
Thanks once again for the hint.

Iniciar sesión para comentar.

Más respuestas (1)

Aniketh
Aniketh el 10 de Jul. de 2023
Editada: Aniketh el 10 de Jul. de 2023
Hi Chhayank,
To convert a yuv encoded image to rgb you can first
  1. 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.
  2. 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 comentarios
Chhayank Srivastava
Chhayank Srivastava el 10 de Jul. de 2023
@Walter Roberson Yes you are correct. There are 16 bits per pixel or 2 bytes per pixel. There is also 1280 stride in bytes (as shown in the step size). How can I use this information to convert this 1d array to 3d array with YUV channel?
DGM
DGM el 10 de Jul. de 2023
Editada: DGM el 10 de Jul. de 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:
  1. find some sort of documentation which specifies how the file is encoded
  2. work with a sample image to verify the encoding details
  3. 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by