video - How to extract Y,U, and V components from a given yuv file using Matlab? Each component is used for further pixels level manipulation -
hey guys. i'm playing yuv file. have suggestion on how extract y,u,v components yuv video? found piece of program shown below. don't know part valid components want. thanks.
% function mov = loadfileyuv(filename, width, height, idxframe) function [mov,imgrgb] = loadfileyuv(filename, width, height, idxframe) % load rgb movie [0, 255] yuv 4:2:0 file fileid = fopen(filename, 'r'); subsamplemat = [1, 1; 1, 1]; nrframe = length(idxframe); 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 m-by-n matrix %whose elements taken columnwise x. %an error results if x 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 , upsample % read v component buf = fread(fileid, width / 2 * height / 2, 'uchar'); imgyuv(:, :, 3) = kron(reshape(buf, width / 2, height / 2).', subsamplemat); % reshape , upsample % normalize yuv values % imgyuv = imgyuv / 255; % convert yuv 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);
not sure whether have fundamental misunderstanding yuv files, if edit function did below, have yuv components every frame in single variable called imgyuv
. note may run out of memory in process, maybe don't want load frames of movie in 1 go.
function imgyuv = loadfileyuv(filename, width, height, idxframe) % load yuv data yuv 4:2:0 file fileid = fopen(filename, 'r'); subsamplemat = [1, 1; 1, 1]; nrframe = length(idxframe); %# preassign imgyuv. in case can't keep in ram, %# better function crashes here, rather after %# having wasted time filling memory. %# since images of class 'uint8', %# can save on lot of memory initializing %# imgyuv zeros(width/2,height/2,3,nrframe,'uint8'); imgyuv = zeros(width/2 height/2, 3, nrframe); 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 , upsample % read v component buf = fread(fileid, width / 2 * height / 2, 'uchar'); imgyuv(:, :, 3, f) = kron(reshape(buf, width / 2, height / 2).', subsamplemat); % reshape , upsample end fclose(fileid);
Comments
Post a Comment