The following code takes input as a video reader and read the number of frames and writes to a directory.
and the output is a video of the frames which are sorted in certain time intervals.
workingDir = tempname;
mkdir(workingDir);
mkdir(workingDir,'images');
shuttleVideo = VideoReader('shuttle.avi');
for ii = 1:shuttleVideo.NumberOfFrames
img = read(shuttleVideo,ii);
imwrite(img,fullfile(workingDir,'images',sprintf('img%d.jpg',ii)));
end
imageNames = dir(fullfile(workingDir,'images','*.jpg'));
imageNames = {imageNames.name}';
disp(imageNames(1:10));
imageStrings = regexp([imageNames{:}],'(\d*)','match');
imageNumbers = str2double(imageStrings);
[~,sortedIndices] = sort(imageNumbers);
sortedImageNames = imageNames(sortedIndices);
disp(sortedImageNames(1:10));
outputVideo = VideoWriter(fullfile(workingDir,'shuttle_out.avi'));
outputVideo.FrameRate = shuttleVideo.FrameRate;
open(outputVideo);
for ii = 1:length(sortedImageNames)
img = imread(fullfile(workingDir,'images',sortedImageNames{ii}));
writeVideo(outputVideo,img);
end
close(outputVideo);
shuttleAvi = VideoReader(fullfile(workingDir,'shuttle_out.avi'));
mov(shuttleAvi.NumberOfFrames) = struct('cdata',[],'colormap',[]);
for ii = 1:shuttleAvi.NumberOfFrames
mov(ii) = im2frame(read(shuttleAvi,ii));
end
set(gcf,'position', [150 150 shuttleAvi.Width shuttleAvi.Height])
set(gca,'units','pixels');
set(gca,'position',[0 0 shuttleAvi.Width shuttleAvi.Height])
image(mov(1).cdata,'Parent',gca);
axis off;
movie(mov,1,shuttleAvi.FrameRate);
displayEndOfDemoMessage(mfilename)
No comments:
Post a Comment