matlab - 10 位 YUV420 到 RGB 转换

标签 matlab image-processing video-processing yuv

我正在处理从 YUV420 到 RGB 的转换,但图像颜色生成效果不佳。原来我自己的文件是 10 位的。最初,我从 8 位文件开始。

我正在使用下面的代码读取 YUV420 图像并转换为 RGB。因为我有 YUV420.YUV 图像文件,但该代码用于视频,所以我只读取 1 帧。然后我得到 YUV 作为 Y 作为全尺寸,但 U 和 V 作为一半尺寸,如维基百科所述。然后,我将图像调整为图像的完整大小,并将 YUV 应用到 RGB 转换。但是 RGB 图像的颜色不正确。我已附上文件,以便您可以运行并查看问题所在。这是 YUV 文件 tulips_yuv420_inter_planar_qcif.yuv .

我还有两个问题;

首先,一帧“流”的大小应等于 1.5*Y 的大小,但无论我使用 uint8 还是 uint16 读取文件,它都非常大。

其次,如果我有 10 位 YUV420 文件,我该如何修改此代码以显示正确的 RGB。

fname = 'tulips_yuv420_inter_planar_qcif.yuv';
width  = 176;
height = 144;
nFrame=1;
fid = fopen(fname,'r');           % Open the video file
stream = fread(fid,'uint8');    % uint16
% stream = fread(fid);    % uint8

length = 1.5 * width * height;  % Length of a single frame

y = double(zeros(height,   width,   nFrame));
u = double(zeros(height/2, width/2, nFrame));
v = double(zeros(height/2, width/2, nFrame));

for iFrame = 1:nFrame

   frame = stream((iFrame-1)*length+1:iFrame*length);

   % Y component of the frame
   yImage = reshape(frame(1:width*height), width, height)';
   % U component of the frame
   uImage = reshape(frame(width*height+1:1.25*width*height), width/2, height/2)';
   % V component of the frame
   vImage = reshape(frame(1.25*width*height+1:1.5*width*height), width/2, height/2)';

   y(:,:,iFrame) = double(yImage);
   u(:,:,iFrame) = double(uImage);
   v(:,:,iFrame) = double(vImage);

end
u=imresize(u,size(y),'bicubic');
v=imresize(v,size(y),'bicubic');
yuv=cat(3,y,u,v);
T = [1,0,1.28033;1,-0.21482,-0.38059;1,2.12798,0];


RGB(:,:,1) = T(1)*yuv(:,:,1) + T(4)*yuv(:,:,2) + T(7)*yuv(:,:,3) ;
RGB(:,:,2) = T(2)*yuv(:,:,1) + T(5)*yuv(:,:,2) + T(8)*yuv(:,:,3) ;
RGB(:,:,3) = T(3)*yuv(:,:,1) + T(6)*yuv(:,:,2) + T(9)*yuv(:,:,3) ;

figure,imshow(uint8(RGB))

最佳答案

样本文件是8位的(不是10位的),存储格式比较棘手。

该工具允许您选择格式。
适合的格式如下:
enter image description here

帧分为两个场 - 上场和下场(隔行扫描格式)。
每个字段的分辨率为 176x72。
因为格式是YUV420,所以U和V字段的大小是88x36。

代码示例使用以下阶段:

  • 读取 Y、U 和 V 的高位字段(每个元素 8 位)。
  • 阅读 Y、U 和 V 的下栏。
  • 交错上下场。
  • 将 U 和 V 上采样到 Y 的大小。
  • 将 YUV 转换为 RGB(使用现有的 MATLAB 函数 ycbcr2rgb)。

以下代码示例读取第一帧并转换为 RGB:

fname = 'tulips_yuv420_inter_planar_qcif.yuv';
width  = 176;
height = 144;
fid = fopen(fname, 'r');           % Open the video file

Y0 = (fread(fid, [width, height/2], 'uint8'))';     %Read upper field of Y plane
U0 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read lower field of Y plane
V0 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read upper field of U plane

Y1 = (fread(fid, [width, height/2], 'uint8'))';     %Read upper field of Y plane
U1 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read lower field of U plane
V1 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read lower field of V plane

fclose(fid);

%Interleave upper and lower fields
Y = zeros(height, width);
Y(1:2:end, :) = Y0;
Y(2:2:end, :) = Y1;

U = zeros(height/2, width/2);
U(1:2:end, :) = U0;
U(2:2:end, :) = U1;

V = zeros(height/2, width/2);
V(1:2:end, :) = V0;
V(2:2:end, :) = V1;

U = imresize(U, size(Y), 'bicubic');
V = imresize(V, size(Y), 'bicubic');
YUV = cat(3, Y, U, V);

%Convert YUV to RGB (MATLAB function ycbcr2rgb uses BT.601 conversion formula).
RGB = ycbcr2rgb(uint8(YUV));

figure,imshow(RGB)

结果:
enter image description here


读取10位YUV420:

假设:

  • 每个 10 位组件存储在 2 个字节中(无“位打包”)。
  • 数据存储在每个字节的低位部分(每个 uint16 元素包含 [0, 1023] 范围内的值)。
  • 存储格式是与 uint8 示例相同的非标准 interlace 格式。

从 8 位样本构建 10 位 YUV420 样本文件(用于测试的单帧):
以下代码从 8 位样本构建一个 10 位样本(将范围从存储在 uint8 中的 8 位扩展到存储在 uint16 中的 10 位)。

fname = 'tulips_yuv420_inter_planar_qcif.yuv';
width  = 176;
height = 144;
fid = fopen(fname, 'r');           % Open the video file
Y0 = (fread(fid, [width, height/2], 'uint8'))';     %Read upper field of Y plane
U0 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read lower field of Y plane
V0 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read upper field of U plane
Y1 = (fread(fid, [width, height/2], 'uint8'))';     %Read upper field of Y plane
U1 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read lower field of U plane
V1 = (fread(fid, [width/2, height/4], 'uint8'))';   %Read lower field of V plane
fclose(fid);

fid = fopen('10bits__tulips_yuv420_inter_planar_qcif.yuv', 'w');           % Open for writing
fwrite(fid, uint16(Y0'*(1023/255)), 'uint16'); %1023 = 2^10-1, and 255 = 2^8-1
fwrite(fid, uint16(U0'*(1023/255)), 'uint16');
fwrite(fid, uint16(V0'*(1023/255)), 'uint16');
fwrite(fid, uint16(Y1'*(1023/255)), 'uint16');
fwrite(fid, uint16(U1'*(1023/255)), 'uint16');
fwrite(fid, uint16(V1'*(1023/255)), 'uint16');
fclose(fid);

读取10位YUV420
以下代码读取单帧10位YUV420(假设匹配列表):

fname = '10bits__tulips_yuv420_inter_planar_qcif.yuv';
width  = 176;
height = 144;
fid = fopen(fname, 'r');           % Open the video file

Y0 = (fread(fid, [width, height/2], 'uint16'))';     %Read upper field of Y plane
U0 = (fread(fid, [width/2, height/4], 'uint16'))';   %Read lower field of Y plane
V0 = (fread(fid, [width/2, height/4], 'uint16'))';   %Read upper field of U plane

Y1 = (fread(fid, [width, height/2], 'uint16'))';     %Read upper field of Y plane
U1 = (fread(fid, [width/2, height/4], 'uint16'))';   %Read lower field of U plane
V1 = (fread(fid, [width/2, height/4], 'uint16'))';   %Read lower field of V plane

fclose(fid);

%Interleave upper and lower fields
Y = zeros(height, width);
Y(1:2:end, :) = Y0;
Y(2:2:end, :) = Y1;

U = zeros(height/2, width/2);
U(1:2:end, :) = U0;
U(2:2:end, :) = U1;

V = zeros(height/2, width/2);
V(1:2:end, :) = V0;
V(2:2:end, :) = V1;

U = imresize(U, size(Y), 'bicubic');
V = imresize(V, size(Y), 'bicubic');
YUV = cat(3, Y, U, V);

%Convert elements range from [0, 1023] to range [0, 1] (MATLAB function ycbcr2rgb supports doubles in range [0, 1]).
YUV = YUV/1023; %1023 applies 10 bits range. 2^10-1 = 1023

%Convet YUV to RGB (MATLAB function ycbcr2rgb uses BT.601 conversion formula).
RGB = ycbcr2rgb(YUV);

%Convert from double to uint8 (from range [0, 1] to range [0, 255]).
RGB = im2uint8(RGB);

figure,imshow(RGB)

注意:
代码 YUV = YUV/1023 将“10 位”格式转换为 [0, 1] double 格式。
使用转换是因为 ycbcr2rgb 不支持 10 位输入。


计算文件大小:
你是对的:“一帧的大小等于 1.5*Y 的大小”。
假设10位分量存储在2个字节中,Y的大小为宽*高*2,一帧的大小为宽*高*3。

关于matlab - 10 位 YUV420 到 RGB 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57714808/

相关文章:

matlab - 阻止从文件流式传输时的音频失真( Octave )

android - 在将过滤器应用于图像时避免内存不足异常 (Android)

matlab - MPEG4 质量保存在 MATLAB 中

matlab - @文件夹和+文件夹

matlab - 定位和/或调用内置 MATLAB MEX 文件

matlab - 将 spfun 与两个相同顺序的稀疏矩阵一起使用

ios - 将线性转换为 Apple Display P3 色彩空间的方程式,反之亦然

opencv - 我可以将 openCV 库与 Catapult C 一起使用吗?

audio - FFmpeg 串联,最终输出中没有音频

memory - 如何在 spartan 3e fpga 中流式传输小视频?