簡體   English   中英

如何使用Matlab從給定的yuv文件中提取Y,U和V分量? 每個組件用於進一步的像素級操縱

[英]How to extract Y,U, and V components from a given yuv file using Matlab? Each component is used for further pixels level manipulation

大家好。 我目前正在使用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);

不知道我是否對YUV文件有一些基本的誤解,但是如果您像下面那樣編輯函數,則每個幀中的YUV組件都在一個名為imgYUV變量中。 請注意,您可能會在此過程中耗盡內存,或者不想一次性加載電影的所有幀。

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);

我只是更改函數定義:

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
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM