function varargout=im2avi(ext, imdir, scale, framerate, filename, playflag)
% im2avi converts image sequenses to avi video
%
% SYNTAX
%
% Inputs: imdir: image sequense directory
% ext: the extension name of the image such as 'jpg', 'tif',
% scale: image resize, like [320 400] or 0.9
% framerate: avi video frame rate
% filename: save avi as
% playflag: play the avi video; if it is 0, no play, if >0,
% play the avi 'playflag' times.
%
% EXAMPLE: im2avi(ext, imdir, scale, framerate, filename, playflag)
%
% NOTES: based on im2avi (author: Zhe Wu @ Univ of Rochester)
% Wenbin, 09-May-2007
warning('im2avi:warning','Do NOT close or open any image window during the process!\n')
filearray=dir([imdir filesep '*.' ext]);
s=size(filearray,1);
frameind=0;
mv =struct('cdata',{}, 'colormap', {});
figure, h =gcf;
for i=1:s
frameind=frameind+1;
imgname=[imdir filesep filearray(i).name];
im=imread(imgname) ;
im=imresize(im, scale);
imshow(im);
mv(frameind)=getframe(h);
end
close(h)
movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate);
% movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, 'compression', 'None');
if nargout >0
varargout{1} =mv;
end
if playflag>0
movie(mv, playflag);
end
Chinese version:
hi,
ReplyDeletei copy ur code to embed in my current code. TQ.
how can you set the sequence of image in the directoy?? have 57 images, and it taking it randomly. I want to give the images in sequence .
ReplyDeleteLet me know if you can give images i specified sequence
Thanks
Priyanka
Hey
ReplyDeleteI had image name sequence as 1.jpg to 57.jpg ,, just giving, 1.jpg as 01.jpg solves the problem .. It takes the images in sequence.
Thanks
Priyanka
hi..
ReplyDeletei used ur code and it worked...
thanks dear
hey i am getting error in that for loop
ReplyDeleteimresize(im, scale);
plz help me
ya i got the video but got error in the last movie cmd
ReplyDelete% if the img name 1.gif,2.gif ....
ReplyDeleteaviobj = avifile('a.avi');
aviobj.Quality = 80;
aviobj.COMPRESSION ='None';%%color image
for i =1:10
image =imread(strcat(int2str(i),'.gif'));
aviobj = addframe(aviobj,image);
end
aviobj = close(aviobj);
thanks a lot man indeed thankful to you please continue ur helping everyone
ReplyDeletecan we get video to images in a pariculat specified directory...............
ReplyDeletefollowing is the code for video(avi) to image conversion but in the output i'm getting only one frame ....please help me in finding corrections so that i can proceed further.............
ReplyDeleteclc;
obj = aviread('C:\Users\hp\Desktop\videos\river.avi');
i=0;
for i=1:length(obj)
v=obj(1,i);
frame=frame2im(v);
imwrite(frame,'C:\Users\hp\Desktop\videos\images\num2str(0i).jpg');
end
See http://nw360.blogspot.com/2010/11/convert-video-sequence-to-images.html for example.
ReplyDeleteHi, your code worked pretty well, it showed the video during the execution, but when I try to open the avi file later, it doesn't open. What could it be?
ReplyDeleteReplace the following
ReplyDeletemovie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate);
to
movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, 'compression', 'None');
Thank you so much!! :)
ReplyDeletehey does this code works in matlab 2011a...kindly help me :)
ReplyDeleteWhy not run it in MATLAB 2011a and find out?
ReplyDeletesumone wrote d code on date of "11/29/2010 6:39 PM" thr is an error.
ReplyDeleteimwrite(frame,'C:\Users\hp\Desktop\videos\images\num2str(0i).jpg')
it must be
path=['C:Users\hp\Desktop\videos\images\' '0' num2str(i)];
imwrite(frame,[path,'.jpg']);
hi
ReplyDeletefor me showing error in imdir command ... kindly help me pls
Yeah.. it's not accepting imdir command.
ReplyDeleteimdir is an input you send to the function.
ReplyDeleteHi, thanks for the code! It worked great (in MATLAB 2011A, BTW) for up to about 550images. But for more than that- the avi file could not be read. I would appreciate any help... Thanks!
ReplyDeletecan any1 help?
ReplyDeletei m geting following error
??? Error using ==> movie2avi at 58
Inputs must be a MATLAB movie, a filename, and param/value pairs.
Error in ==> im2avi at 38
movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, '');
i hv entered following parameters
ReplyDeleteext='jpg'
imdir='c:\C:\Users\Usman Khan\Documents\MATLAB\done'
scale=0
framerate=5
filename='out'
playflag=8
it is playing the video along with above mentioned error.
ReplyDelete??? Error using ==> movie2avi at 58
Inputs must be a MATLAB movie, a filename, and param/value pairs.
Error in ==> im2avi at 38
movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, '');
also how to save this video,as this code is just playing it,not saving it.
Try:
Deletescale = 0.75
The function saves the movie in "imdir" under the name "filename".
hii i have used the code for images to avi movie
ReplyDeletebut it converts the coloured images to black and wight in movie
also changes the axis to 45 degree
i am providing you the code can you check please
function screen_capture(movie_name,recording_time)
movie_name1=strcat(movie_name,'.avi')
mov = avifile(movie_name1);
mov.Quality=100;
mov.COMPRESSION='NONE'
count=0;
robo = java.awt.Robot;
t = java.awt.Toolkit.getDefaultToolkit();
rectangle = java.awt.Rectangle(t.getScreenSize());
number_of_frames=round(recording_time/0.65);
display_time_of_frame=10;
for i=1:number_of_frames
name1=strcat('scrcapture',num2str(i),'.jpg');
image = robo.createScreenCapture(rectangle);
filehandle = java.io.File(name1);
javax.imageio.ImageIO.write(image,'jpg',filehandle);
end
for i=1:number_of_frames
name1=strcat('scrcapture',num2str(i),'.jpg');
a=imread(name1);
while count<display_time_of_frame
count=count+1;
mov=addframe(mov,a);
end
count=0;
end
close all
mov=close(mov);
load gong;
wavplay(y,Fs)
im getting error as below
ReplyDeleteError in ==> im2av at 33
im=imread(imgname) ;
please help me
Error using movie2avi (line 58)
ReplyDeleteInputs must be a MATLAB movie, a filename, and param/value pairs.
Error in im2avi (line 43)
movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, '');
These r the things i given...pls help me...i am getting above error...
ext='jpg';
imdir='D:\EQ';
scale=0.75;
framerate=5;
filename='out';
playflag=8;
After running the function It plays the video, but it couldn't create .avi file
ReplyDeleteI am getting the following error:
Warning: Do NOT close or open any image window during the process!
> In im2avi at 19
??? Error using ==> movie2avi at 58
Inputs must be a MATLAB movie, a filename, and param/value pairs.
Error in ==> im2avi at 38
movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, '');
Please reply
Try
Deletemovie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate); instead.
filearray=dir([imdir filesep '*.' ext]);
ReplyDeleteNot enough input arguments.how to debug?
thanks your code is good
ReplyDeletehave 128 images and it takes them randomly, 1,10 100,102103 and so on why is this
ReplyDelete