【问题标题】:frame to video conversion matlab帧到视频的转换matlab
【发布时间】:2014-06-19 18:34:57
【问题描述】:

所以我刚开始使用 MATLAB 进行图像处理/计算机视觉。

所以我的第一个任务是将一系列图像(帧)转换为视频。所以我通过在线资源(更具体地说是 MATLAB 网站)找到了一种方法。

所以我实现的是http://www.mathworks.com/help/matlab/examples/convert-between-image-sequences-and-video.html,它为我解决了这个问题。

但是,当我播放它时,视频在某些地方似乎有些跳动。就像它会在中间带来一个不同的帧,让整个视频在那一瞬间变得跳跃。它发生在视频中的几个地方。

有人知道为什么会这样吗?

谢谢

PS 下面是我使用的代码:

myFolder = 'C:\Users\owner\Desktop\MATLAB GUI\Color\Color'; %Specify Directory
filePattern = fullfile(myFolder, '*.jpg') %identify jpg files
jpegFiles = dir(filePattern) %use dir to list jpg files
size = length(jpegFiles); % length of the size of the file

outputVideo = VideoWriter(fullfile(myFolder,'video1.avi'));
outputVideo.FrameRate = 30;
open(outputVideo);


for i = 1:length(jpegFiles) %load all the files in the directory
  j = i; %%accumulating the number of jpegfiles into handles.j
  baseFileName = jpegFiles(i).name;
  fullFileName = fullfile(myFolder, baseFileName);
  %fprintf(1, 'Now reading %s\n', fullFileName); %filename of image
  imageArray = imread(fullFileName); %image being read
  %imageArray = rgb2gray(imageArray);
  imagecell{i} = imageArray; %storing the images in imagecells
  writeVideo(outputVideo,imagecell{i});
end

close(outputVideo);
video1 = VideoReader(fullfile(myFolder,'video1.avi'));

mov(video1.NumberOfFrames) = struct('cdata',[],'colormap',[]);

for ii = 1:video1.NumberOfFrames
    mov(ii) = im2frame(read(video1,ii));
end

set(gcf,'position', [150 150 video1.Width video1.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 video1.Width video1.Height])

image(mov(1).cdata,'Parent',gca);
axis off;

movie(mov,1,video1.FrameRate);

【问题讨论】:

  • jpg 文件的名称是什么?我问是因为你怎么知道你读取文件的顺序是正确的帧顺序?例如,如果您的文件被命名为 frame1.jpg、frame2.jpg、...、frame10.jpg、frame11.jpg 等,当您读取文件列表并写入视频对象时,图像可能会被排序如 frame1.jpg、frame10.jpg、frame11.jpg、...、frame19.jpg、frame2.jpg 等。所以这可能就是为什么每十张图片都不合适的原因。从fullFileName = fullfile(myFolder, baseFileName) 中删除分号并重新运行脚本并查看文件顺序。
  • 另外 - 您已经命名并指定了文件数量的长度,如下size = length(jpegFiles);size 是一个内置的 MATLAB 函数,因此如果您(在后面的代码行中)尝试使用 size 函数,给一个变量赋予相同的名称将是一个问题。请将此重命名为numOfFiles
  • dir 也有问题,它按照操作系统给出的顺序输入,可能没有按名称排序。我建议您检查您的jpegfiles 单元格以查看文件是否以正确的顺序读取。
  • @GeoffHayes 你是对的。文件在每 10 帧出现一次故障。那么关于如何加载文件有什么建议吗?非常感谢你。
  • @user2441667 - 你有多少个文件/帧?如果它们的名称类似于image1.jpgimage99.jpg,您可以手动使用0 填充单个数字图像:image01.jpg,image02.jpg, 等。然后当您阅读列表时,它们将按正确顺序排序文件(如果没有,您可以使用 sort 功能为您完成)。

标签: matlab image-processing video-streaming video-processing matlab-cvst


【解决方案1】:

鉴于可能要重命名的文件太多(用零填充),这里有一个快速功能可以为您完成:您只需要提供存储图像的目录/文件夹,填充(如果少于 100 个文件,则 padding 可以为 2;如果少于 1000 个文件,则 padding 可以为 3;等等),以及常见的模式。代码假定每个文件中都有一个共同的模式(如“框架”或“图像”),当删除时,只留下数字:

 renameFiles(directory,padSize,fileNamePattern)

    filePattern = fullfile(directory, '*.jpg') %identify jpg files
    jpegFiles   = dir(filePattern) %use dir to list jpg files

    for k=1:size(jpegFiles)

        % get the source file that will be moved/renamed
        fileSrc = jpegFiles(k).name;

        % get the parts of the file
        [path,name,ext] = fileparts(fileSrc);

        % where does the pattern fit in the name?
        idx = strfind(name,fileNamePattern);

        % remove the pattern from the name
        if idx==0
            % pattern does not exist in file so skip it
            continue;
        elseif idx==1
            % pattern is at the beginning of name so remove it
            frameNumberStr = name(length(fileNamePattern)+1:end);
        else
            % pattern is at the end of name so remove it
            frameNumberStr = name(1:idx-1);
        end

        % get the number of digits
        numDigits = length(frameNumberStr);

        % create the new file name
        paddedName = [fileNamePattern repmat('0',1,padSize-numDigits) frameNumberStr];
        fprintf('%s\n',paddedName);

        % only move if the destination is different
        if strcmp(paddedName,name) ~= 1

            % set the destination file
            fileDest = fullfile(directory,[paddedName ext]);

            % move the file
            movefile(fileSrc, fileDest,'f');
        end
    end
end

一个例子 - 如果所有文件都有共同的'frame'模式并且文件少于1000个,那么运行这个函数

rename(pwd,3,'frame')

所有被命名为frame1.jpgframe99.jpg 的文件现在被命名为frame001.jpgframe099.jpg

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    如果您有计算机视觉系统工具箱,您可以使用vision.VideoFileWriter 对象。您可以一次简单地将图像输入其step() 方法,它们将作为视频帧写入视频文件。请注意,图像必须全部具有相同的大小并具有相同的数据类型。

    【讨论】:

    • 嗨; vision.VideoFileWriter 是否允许我使用图像制作视频?似乎它使用现有的视频。对不起,我对此完全陌生,真的很困惑。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多