如何从 Matlab 中将文件保存到 y4m?

发布于 2024-10-12 17:22:17 字数 1035 浏览 1 评论 0原文

所以,我有一个 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 技术交流群。

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

发布评论

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

评论(1

等数载,海棠开 2024-10-19 17:22:17

对于“侧面”(我假设你的意思是旋转),你只需要旋转你的数组(交换列与行)。对于跟踪,如果它偏离固定量,则似乎与您的重塑有关(尝试(宽度 - 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文