如何使用 Matlab 从给定的 yuv 文件中提取 Y、U 和 V 分量?每个组件都用于进一步的像素级操作

发布于 2024-09-26 18:29:15 字数 2165 浏览 6 评论 0原文

嘿伙计们。我目前正在使用 YUV 文件。您对如何从 yuv 视频中提取 y,u,v 分量有什么建议吗?我找到程序片段如下所示。但我不知道哪一部分是我想要的有效组件。谢谢。

% function mov = loadFileYuv(fileName, width, height, idxFrame)

function [mov,imgRgb] = loadFileYuv(fileName, width, height, idxFrame)
% load RGB movie [0, 255] from YUV 4:2:0 file

fileId = fopen(fileName, 'r');

subSampleMat = [1, 1; 1, 1];
nrFrame = length(idxFrame);


for f = 1 : 1 : nrFrame

    % search fileId position
    sizeFrame = 1.5 * width * height;
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');

    % read Y component
    buf = fread(fileId, width * height, 'uchar');
    imgYuv(:, :, 1) = reshape(buf, width, height).'; % reshape RESHAPE(X,M,N) returns the M-by-N matrix 
                                                     %whose elements are taken columnwise from X.  
                                                     %An error results if X does not have M*N elements

    % read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 2) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 3) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % normalize YUV values
    % imgYuv = imgYuv / 255;

    % convert YUV to RGB
    imgRgb = reshape(convertYuvToRgb(reshape(imgYuv, height * width, 3)), height, width, 3);
    % imgRgb = ycbcr2rgb(imgYuv);
    %imwrite(imgRgb,'ActualBackground.bmp','bmp');
    mov(f) = im2frame(imgRgb);
    %   mov(f).cdata = uint8(imgRgb);
    %   mov(f).colormap =  [];
    %     imwrite(imgRgb,'ActualBackground.bmp','bmp');

    %figure, imshow(imgRgb);
    %name = 'ActualBackground.bmp';
    %Image = imread(name, 'bmp');
    %figure, imshow(Image);
end
fclose(fileId);

Hey guys. I'm currently playing with YUV file. Do you have any suggestion on how to extract y,u,v components from a yuv video? I found a piece of program which was shown below. But I don't know which part is the valid components that I want. Thanks.

% function mov = loadFileYuv(fileName, width, height, idxFrame)

function [mov,imgRgb] = loadFileYuv(fileName, width, height, idxFrame)
% load RGB movie [0, 255] from YUV 4:2:0 file

fileId = fopen(fileName, 'r');

subSampleMat = [1, 1; 1, 1];
nrFrame = length(idxFrame);


for f = 1 : 1 : nrFrame

    % search fileId position
    sizeFrame = 1.5 * width * height;
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');

    % read Y component
    buf = fread(fileId, width * height, 'uchar');
    imgYuv(:, :, 1) = reshape(buf, width, height).'; % reshape RESHAPE(X,M,N) returns the M-by-N matrix 
                                                     %whose elements are taken columnwise from X.  
                                                     %An error results if X does not have M*N elements

    % read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 2) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 3) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % normalize YUV values
    % imgYuv = imgYuv / 255;

    % convert YUV to RGB
    imgRgb = reshape(convertYuvToRgb(reshape(imgYuv, height * width, 3)), height, width, 3);
    % imgRgb = ycbcr2rgb(imgYuv);
    %imwrite(imgRgb,'ActualBackground.bmp','bmp');
    mov(f) = im2frame(imgRgb);
    %   mov(f).cdata = uint8(imgRgb);
    %   mov(f).colormap =  [];
    %     imwrite(imgRgb,'ActualBackground.bmp','bmp');

    %figure, imshow(imgRgb);
    %name = 'ActualBackground.bmp';
    %Image = imread(name, 'bmp');
    %figure, imshow(Image);
end
fclose(fileId);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2024-10-03 18:29:15

不确定我是否对 YUV 文件有一些根本性的误解,但如果您像下面那样编辑函数,您就会在名为 imgYUV 的单个变量中获得每个帧的 YUV 分量。请注意,您可能会在此过程中耗尽内存,您可能不想一次性加载电影的所有帧。

function imgYUV = loadFileYuv(fileName, width, height, idxFrame)
% load YUV data from YUV 4:2:0 file

fileId = fopen(fileName, 'r');

subSampleMat = [1, 1; 1, 1];
nrFrame = length(idxFrame);

%# preassign imgYUV. In case we can't keep everything in RAM,
%# it is better that the function crashes here, rather than after
%# having wasted time slowly filling up the memory.
%# Since the images are likely to be of class 'uint8', 
%# you can save on a lot of memory by initializing
%# imgYUV as zeros(width/2,height/2,3,nrFrame,'uint8');
imgYUV = zeros(width/2 height/2, 3, nrFrame);

for f = 1 : 1 : nrFrame

    %# search fileId position
    sizeFrame = 1.5 * width * height;
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');

    %# read Y component
    buf = fread(fileId, width * height, 'uchar');
    imgYuv(:, :, 1, f) = reshape(buf, width, height).'; 

    %# read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 2, f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 3, f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

end
fclose(fileId);

Not sure whether I have some fundamental misunderstanding about YUV files, but if you edit the function like I did below, you have the YUV components for every frame in a single variable called imgYUV. Note that you may run out of memory in the process, you maybe you don't want to load all the frames of the movie in one go.

function imgYUV = loadFileYuv(fileName, width, height, idxFrame)
% load YUV data from YUV 4:2:0 file

fileId = fopen(fileName, 'r');

subSampleMat = [1, 1; 1, 1];
nrFrame = length(idxFrame);

%# preassign imgYUV. In case we can't keep everything in RAM,
%# it is better that the function crashes here, rather than after
%# having wasted time slowly filling up the memory.
%# Since the images are likely to be of class 'uint8', 
%# you can save on a lot of memory by initializing
%# imgYUV as zeros(width/2,height/2,3,nrFrame,'uint8');
imgYUV = zeros(width/2 height/2, 3, nrFrame);

for f = 1 : 1 : nrFrame

    %# search fileId position
    sizeFrame = 1.5 * width * height;
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');

    %# read Y component
    buf = fread(fileId, width * height, 'uchar');
    imgYuv(:, :, 1, f) = reshape(buf, width, height).'; 

    %# read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 2, f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    imgYuv(:, :, 3, f) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

end
fclose(fileId);
星軌x 2024-10-03 18:29:15

我只需更改函数定义:

function [mov,imgYuv] = loadFileYuv(fileName, width, height, idxFrame)

无需返回 imgRgb,因为它与 mov(numel(idxFrame)).cdata 相同。

指定两个输出变量,您将在第二个变量中获得最后一帧的 YUV 分量。


我会更改该函数来检查缓冲区是否为空,以避免在您要求的帧数多于文件包含的帧数时出现错误。

    ...
    status = fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');
    if status == -1
        error('Cannot read frame %d',idxFrame(f));
    end

    % read Y component
    buf = fread(fileId, width * height, 'uchar');
    if isempty(buf)
        error('Cannot read frame %d',idxFrame(f));
    end
    imgYuv(:, :, 1) = reshape(buf, width, height).'; % reshape

    % read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    if isempty(buf)
        error('Cannot read frame %d',idxFrame(f));
    end
    imgYuv(:, :, 2) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    if isempty(buf)
        error('Cannot read frame %d',idxFrame(f));
    end
    ...

I would just change the function definition:

function [mov,imgYuv] = loadFileYuv(fileName, width, height, idxFrame)

There is no need to return imgRgb, since it's the same as mov(numel(idxFrame)).cdata.

Specify two output variables and you will get YUV components of the last frame in the second variable.


I would change the function to check if buffer is empty to avoid error if you ask for more frames than the file contains.

    ...
    status = fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof');
    if status == -1
        error('Cannot read frame %d',idxFrame(f));
    end

    % read Y component
    buf = fread(fileId, width * height, 'uchar');
    if isempty(buf)
        error('Cannot read frame %d',idxFrame(f));
    end
    imgYuv(:, :, 1) = reshape(buf, width, height).'; % reshape

    % read U component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    if isempty(buf)
        error('Cannot read frame %d',idxFrame(f));
    end
    imgYuv(:, :, 2) = kron(reshape(buf, width / 2, height / 2).', subSampleMat); % reshape and upsample

    % read V component
    buf = fread(fileId, width / 2 * height / 2, 'uchar');
    if isempty(buf)
        error('Cannot read frame %d',idxFrame(f));
    end
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文