Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:48   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 如何使用Matlab从给定的yuv文件中提取Y,U和V分量?每个组件用于进一步的像素级操纵

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

更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 19:41


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.