Saturday, 12 July 2014

making a video out of images in matlab

=====================In older versions of MATLAB========================
samples_dir='C:/data'; % data is folder name that has images
files = dir([samples_dir , '\*.jpg']);
aviObj = avifile('C:\test.avi', 'Compression', 'MSVC'); %test.avi is the name of the video and gets stored
%addframe cannot be used for true color images, so convert to gray scale and then add 
%video quality is not good
for i=1:length(files)
f_bmp=[samples_dir '\' files(i).name];
img=imread(f_bmp);
gr=rgb2gray(img);
aviObj = addframe(aviObj, gr);
end
aviObj=close(aviObj);

===================In newer versions of MATLAB===========================
 clear all;
close all;
samples_dir='C:/data'; % data is folder name that has images

Files = dir([samples_dir , '\*.jpg']);

VideoFile='C:\data\MyVideo'; %path of the video file to be saved
writerObj = VideoWriter(VideoFile);
writerObj.FrameRate =1; %increase the frame rate, here i have used 1 frame per second
open(writerObj);
for t= 1:length(Files)
Frame=imread(strcat('C:\data','\',Files(t).name));
writeVideo(writerObj,im2frame(Frame));
end
close(writerObj);

%To play the video

videoFReader = vision.VideoFileReader('C:\data\MyVideo.avi');
videoPlayer = vision.VideoPlayer;
while ~isDone(videoFReader)
   frame = step(videoFReader);
   step(videoPlayer,frame);
end
release(videoFReader);
release(videoPlayer);
                                                  (or)
implay('C:/data/MyVideo.avi');
=============================================================================
Program to read images in a directory as a video
===========================================================================
samples_dir='C:/data'; % data is folder name that has images
files = dir([samples_dir , '\*.jpg']);
hVPlayer=vision.VideoPlayer;
for i=1:length(files)
f_bmp=[samples_dir '\' files(i).name];
img=imread(f_bmp);
step(hVPlayer,img);
end
release(hVPlayer);

====Reading a 'mpg' video inbuilt in MATLAB and displaying the 36th,72nd,108th and 141th frame.======
clear all;
close all;
videobj=mmreader('xylophone.mpg');
numframes=get(videobj,'numberofframes');

x=read(videobj,36);
y=read(videobj,72);
z=read(videobj,108);
k=read(videobj,141);
subplot(221);imshow(x);title('36th frame');
subplot(222);imshow(y);title('72th frame');
subplot(223);imshow(z);title('108th frame');
subplot(224);imshow(k);title('141th frame');
=====================================================================

No comments:

Post a Comment