如何从 Matlab 中将文件保存到 y4m?
所以,我有一个 Matlab 电影格式的电影(实际上,只有几个 RGB 帧),我想将其保存为 y4m 文件。 (与 x.264 一起使用)。我已经获得了在开始时编写帧头所需的所有信息,并且我有将 RGB 帧转换为 YUV 的函数,但任何时候我尝试将其保存出来,它都是横向的并且跟踪关闭。我也在尝试将其转换为 C420。目前,这是我的功能:
function saveMovToY4m(mov, fileName, f, width, height, fpsNum, fpsDen, inter, ascNum, ascDen)
fileId = fopen(fileName, 'w');
fprintf(fileId, 'YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d C420jpeg\n', width, height, fpsNum, fpsDen, inter, ascNum, ascDen);
for frame = 1:f-1
frame
imgRgb = frame2im(mov(frame));
imgYuv = (convertRgbToYuv(imgRgb, width, height));
fprintf(fileId,'FRAME\n');
% Print Y component
buf = reshape(imgYuv(:,:,1),width*height,1);
buf = buf;
fwrite(fileId, buf, 'uint8');
% Print U component
Cb = reshape(imgYuv(:,:,2),width*height,1);
CBdown = downsample(Cb, 4);
fwrite(fileId, CBdown, 'uint8');
% Print V component
Cbr = reshape(imgYuv(:,:,3),width*height,1);
CBrDown = downsample(Cbr, 4);
fwrite(fileId, CBrDown, 'uint8');
end
fclose(fileId);
end
So, I've got a movie in the Matlab movie format (Really, just several RGB frames) and I'd like to save it out as a y4m file. (to use with x.264). I've got all the information I need to write the frame header thing at the beginning, and I've got functions that convert the RGB frame to YUV, but any time I try to save it out, it's sideways and the tracking is off. I'm also trying to convert it to C420. Here's my function, currently:
function saveMovToY4m(mov, fileName, f, width, height, fpsNum, fpsDen, inter, ascNum, ascDen)
fileId = fopen(fileName, 'w');
fprintf(fileId, 'YUV4MPEG2 W%d H%d F%d:%d I%c A%d:%d C420jpeg\n', width, height, fpsNum, fpsDen, inter, ascNum, ascDen);
for frame = 1:f-1
frame
imgRgb = frame2im(mov(frame));
imgYuv = (convertRgbToYuv(imgRgb, width, height));
fprintf(fileId,'FRAME\n');
% Print Y component
buf = reshape(imgYuv(:,:,1),width*height,1);
buf = buf;
fwrite(fileId, buf, 'uint8');
% Print U component
Cb = reshape(imgYuv(:,:,2),width*height,1);
CBdown = downsample(Cb, 4);
fwrite(fileId, CBdown, 'uint8');
% Print V component
Cbr = reshape(imgYuv(:,:,3),width*height,1);
CBrDown = downsample(Cbr, 4);
fwrite(fileId, CBrDown, 'uint8');
end
fclose(fileId);
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于“侧面”(我假设你的意思是旋转),你只需要旋转你的数组(交换列与行)。对于跟踪,如果它偏离固定量,则似乎与您的重塑有关(尝试(宽度 - 1)*高度,看看它会做什么,然后从那里开始)
For the "sideways"(I assume you mean rotated) you just need to rotate your array(swap the columns with the rows). For the tracking, if it is off by a fixed amount it seems it has to do with your reshaping(try (width - 1)*height and see what that does and go from there)