1

所以我刚开始用MATLAB中的图像处理/计算机视觉。帧到视频转换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); 
+0

jpg文件的名称是什么?我在问,因为你怎么知道你读取文件的顺序是正确的帧顺序?例如,如果在读取文件列表并写入视频对象时,您的文件被命名为frame1.jpg,frame2.jpg,...,frame10.jpg,frame11.jpg等,则图像可能会被排序作为frame1.jpg,frame10.jpg,frame11.jpg,...,frame19.jpg,frame2.jpg等等。所以这可能是为什么每第十个图像不合适的原因。从'fullFileName = fullfile(myFolder,baseFileName)'中移除分号,然后重新运行脚本并查看文件顺序。 – Geoff

+0

另外 - 您已经命名并指定了文件数的长度,如下所示:size = length(jpegFiles);'。 'size'是一个内置的MATLAB函数,所以如果你曾经(在代码的后面的行中)尝试使用'size'函数,给一个变量赋一个相同的名字将会成为一个问题。请将其重命名为'numOfFiles'。 – Geoff

+0

'dir'也存在问题,它按照操作系统给出的顺序输入,可能不会按名称排序。我建议你检查你的'jpegfiles'单元格,看看这些文件是否按正确的顺序读取。 – Nishant

回答

0

考虑到可能有太多的文件进行重命名(用零填充),这里是一个快速的函数,将做它你:你只需要提供存储图像的目录/文件夹,填充(如果少于100个文件,则填充可以是2;如果少于1000个文件,则填充可以是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 

一个例子 - 如果所有文件都'的公共模式框架”,并有不到1000个文件,然后运行该功能

rename(pwd,3,'frame') 

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

希望这会有所帮助!

0

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

+0

嗨; vision.VideoFileWriter能让我使用图像制作视频吗?似乎它使用现有的视频。我很抱歉,我对这个完全陌生,很困惑。谢谢 –

+0

是的。我编辑了这个问题。 – Dima