View a YUYV encoded image in MATLAB
21 ビュー (過去 30 日間)
古いコメントを表示
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1431098/DATA.jpeg)
0 件のコメント
採用された回答
DGM
2023 年 7 月 10 日
編集済み: DGM
2023 年 7 月 10 日
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 件のコメント
その他の回答 (1 件)
Aniketh
2023 年 7 月 10 日
編集済み: Aniketh
2023 年 7 月 10 日
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 件のコメント
DGM
2023 年 7 月 10 日
編集済み: DGM
2023 年 7 月 10 日
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.
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!