In the recent article we have given the introduction of
How to Extract Frames Using MATLAB code, now its time to look at the reverse process. That is, how to convert those frames or a sequence of images back into the video file. And all that with MATLAB coding. If you intend to apply this code in your system, then you need to understand the code, so read below carefully.
Code for that:
ImFolder=uigetdir;
pngFile = dir(strcat(ImFolder,'\*.png'));
S = [pngFile(:).datenum];
[S,S] = sort(S);
pngFiles = pngFile(S);
VideoFile=strcat(ImFolder,'\Video');
writeObj = VideoWriter(VideoFile);
fps= 15;
writeObj.FrameRate = fps;
open(writeObj);
for t= 1:length(pngFiles)
Frame=imread(strcat(ImFolder,'\',pngFiles(t).name));
writeVideo(writeObj,im2frame(Frame));
end
close(writerObj);
Explanation of the above code:
# ImFolder=uigetdir; --> This line when executed, will open a browse window in which you can select your desired folder, where the frames or sequence of images is. The path will be stored in the variable ImFolder.
# pngFile = dir(strcat(ImFolder,'\*.png')); --> here the variable pngFile will be a structure that contain all the names of PNG files that are in the directory you have specified. If your image files are in jpeg format, so give the above command as dir(strcat(ImFolder,'\*.jpg')). Here '\*.png' will be acting as a wildcard for all the PNG files available in that folder.
# S = [pngFile(:).datenum];
[S,S] = sort(S);
--> This 2 lines of code will sort the files according to the datenum property of the pngFile structure.
# pngFiles = pngFile(S); --> creating another variable pngFiles same as pngFile , but the former contains the files list in sorted form.
# VideoFile=strcat(ImFolder,'\Video'); --> Creating a Video File name as Video
# writeObj = VideoWriter(VideoFile); --> This will create a Video Object named writeObj.
# fps= 15; --> specifying the frame rate as 15 (or whatever is required or whatever you want). Your video file length will be decided by the value of fps and the number of the image files.
# writeObj.FrameRate = fps; --> we have set the FrameRate property of writeObj video object as specified in the variable fps.
# open(writeObj); --> Open to start writing the frames in the writeObj video object.
# for t= 1:length(pngFiles)
Frame=imread(strcat(ImFolder,'\',pngFiles(t).name));
writeVideo(writeObj,im2frame(Frame));
end
--> These 4 lines will write the frames in the newly created video file, that is initially empty.
# close(writeObj); --> close the writeObj to complete the video write process.
After this line of code's execution a video file will be created the the root folder with the name Video.avi or what ever you have specified.
Screenshots:
|
Initial Frame Sequence, that we have, before applying this code on this folder |
|
The working directory indicating the frames along with the output video file |
|
The output video file along with the frames, |
The main idea behind the code was obtained from the blog below (We adapted it for Our need though!), it does has various topics about image processing, do refer it:
An active & prominent author at Digital iVision Labs! Specializing in MATLAB, C++, Arduino, OpenCV, NI Labview, Web Designing & other Electronics stuffs! Just started M.Tech. From IIIT Delhi, looking for excellent PhD Opportunities with prominent researchers. Drop a mail: vibhutesh[at]gmail.com or Follow him at....
0 comments: