把一列图像变成一部电影



我有一个jpg文件文件夹,我想把它们做成电影。我正在使用这个脚本:

% Create video out of list of jpgs
clear 
clc
% Folder with all the image files you want to create a movie from, choose this folder using:
ImagesFolder = uigetdir;
% Verify that all the images are in the correct time order, this could be useful if you were using any kind of time lapse photography. We can do that by using dir to map our images and create a structure with information on each file.
jpegFiles = dir(strcat(ImagesFolder,'*.jpg'));
% Sort by date from the datenum information.
S = [jpegFiles(:).datenum]; 
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% The sub-structures within jpegFilesS is now sorted in ascending time order.
% Notice that datenum is a serial date number, for example, if you would like to get the time difference in hours between two images you need to subtract their datenum values and multiply by 1440.
% Create a VideoWriter object, in order to write video data to an .avi file using a jpeg compression.
VideoFile = strcat(ImagesFolder,'MyVideo');
writerObj = VideoWriter(VideoFile);
% Define the video frames per second speed (fps)
fps = 1; 
writerObj.FrameRate = fps;
% Open file for writing video data
open(writerObj);
% Running over all the files, converting them to movie frames using im2frame and writing the video data to file using writeVideo
for t = 1:length(jpegFilesS)
     Frame = imread(strcat(ImagesFolder,'',jpegFilesS(t).name));
     writeVideo(writerObj,im2frame(Frame));
end
% Close the file after writing the video data 
close(writerObj);

(由http://imageprocessingblog.com/how-to-create-a-video-from-image-files/提供)

但是它给了我这个错误:

警告:没有视频帧被写入这个文件。文件可能无效。
比;在VideoWriter.VideoWriter> VideoWriter。收于289
在Movie_jpgCompilation at 37

我确定我的jpg文件没问题,它们在我指定的文件夹里。有什么问题吗?

(这是我的第一个帖子,所以我希望它能有所帮助)。

如果你在Linux上,反斜杠不需要是正斜杠吗?当我在Mac上运行它时,我的jpegFiles是一个空的Struct。当我改变它们时,它工作了:

% Create video out of list of jpgs
clear 
clc
% Folder with all the image files you want to create a movie from, choose this folder using:
ImagesFolder = uigetdir;
% Verify that all the images are in the correct time order, this could be useful if you were using any kind of time lapse photography. We can do that by using dir to map our images and create a structure with information on each file.
jpegFiles = dir(strcat(ImagesFolder,'/*.jpg'));
% Sort by date from the datenum information.
S = [jpegFiles(:).datenum]; 
[S,S] = sort(S);
jpegFilesS = jpegFiles(S);
% The sub-structures within jpegFilesS is now sorted in ascending time order.
% Notice that datenum is a serial date number, for example, if you would like to get the time difference in hours between two images you need to subtract their datenum values and multiply by 1440.
% Create a VideoWriter object, in order to write video data to an .avi file using a jpeg compression.
VideoFile = strcat(ImagesFolder,'/MyVideo.avi');
writerObj = VideoWriter(VideoFile);
% Define the video frames per second speed (fps)
fps = 1; 
writerObj.FrameRate = fps;
% Open file for writing video data
open(writerObj);
% Running over all the files, converting them to movie frames using im2frame and writing the video data to file using writeVideo
for t = 1:length(jpegFilesS)
     Frame = imread(strcat(ImagesFolder,'/',jpegFilesS(t).name));
     writeVideo(writerObj,im2frame(Frame));
end
% Close the file after writing the video data 
close(writerObj);

编辑:您还可以使用filesep,以便文件分隔符是特定于操作系统的。http://www.mathworks.com/help/matlab/ref/filesep.html

使用Windows Movie Maker [Windows]或iMovie [mac]会更简单。对于你的目的,你应该使用ppt

最新更新