分享一段图片合成视频的matlab代码
由两个文件组成:(1)pic2video.m(2)loadImgSequenceInfo.m
内容分别如下:
(1)pic2video.m
video_path = uigetdir('J:/','请选择图片所在文件夹');
video_path=strrep(video_path, '\', '/');
folderName=regexp(video_path, '/', 'split');
folderName=folderName{end};
video_path=[video_path '/'];
[img_files] =loadImgSequenceInfo(video_path);
dname=uigetdir('J:/','保存到');
savefile=[dname '\' folderName '.avi'];
aviobj = VideoWriter(savefile);
%aviobj.Quality = 100;
aviobj.FrameRate = 25;
open(aviobj);
%aviobj.compression='None';
upbound=numel(img_files) ;
for i=1:upbound ;%此处修改成自己的范围,起始位置
disp(['正在处理第' num2str(i) '帧,共' num2str(upbound) '帧']);
adata=imread([video_path img_files{i}]);
frame=im2frame(adata);
writeVideo(aviobj,frame);
end
close(aviobj);
disp([folderName '.avi' 'done sucessfully']);
(2)loadImgSequenceInfo.m
function [img_files] = load_video_info(video_path)
text_files = dir([video_path '*_frames.txt']);
if ~isempty(text_files),
f = fopen([video_path text_files(1).name]);
frames = textscan(f, '%f,%f');
fclose(f);
%see if they are in the 'imgs' subfolder or not
if exist([video_path num2str(frames{1}, 'imgs/img%05i.png')], 'file'),
video_path = [video_path 'imgs/'];
elseif ~exist([video_path num2str(frames{1}, 'img%05i.png')], 'file'),
error('No image files to load.')
end
%list the files
img_files = num2str((frames{1} : frames{2})', 'img%05i.png');
img_files = cellstr(img_files);
else
%no text file, just list all images
img_files = dir([video_path '*.png']);
if isempty(img_files),
img_files = dir([video_path '*.jpg']);
assert(~isempty(img_files), 'No image files to load.')
end
img_files = sort({img_files.name});
end
end
点我去下载文件