Thursday, May 10, 2007

Convert images to avi video in MATLAB

Yesterday I wrote a function that converts images in a directory to avi video based on Wu's code. Here is the simplified version.

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:

用MATLAB转换图片到avi视频

http://time360.blogspot.com/2007/05/matlabavi.html

33 comments:

  1. hi,
    i copy ur code to embed in my current code. TQ.

    ReplyDelete
  2. 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 .
    Let me know if you can give images i specified sequence
    Thanks
    Priyanka

    ReplyDelete
  3. Hey
    I 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

    ReplyDelete
  4. hi..
    i used ur code and it worked...
    thanks dear

    ReplyDelete
  5. hey i am getting error in that for loop

    imresize(im, scale);
    plz help me

    ReplyDelete
  6. ya i got the video but got error in the last movie cmd

    ReplyDelete
  7. % if the img name 1.gif,2.gif ....

    aviobj = 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);

    ReplyDelete
  8. thanks a lot man indeed thankful to you please continue ur helping everyone

    ReplyDelete
  9. can we get video to images in a pariculat specified directory...............

    ReplyDelete
  10. following 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.............
    clc;
    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

    ReplyDelete
  11. See http://nw360.blogspot.com/2010/11/convert-video-sequence-to-images.html for example.

    ReplyDelete
  12. Hi, 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?

    ReplyDelete
  13. Replace the following
    movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate);
    to
    movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate, 'compression', 'None');

    ReplyDelete
  14. Thank you so much!! :)

    ReplyDelete
  15. hey does this code works in matlab 2011a...kindly help me :)

    ReplyDelete
  16. Why not run it in MATLAB 2011a and find out?

    ReplyDelete
  17. sumone wrote d code on date of "11/29/2010 6:39 PM" thr is an error.

    imwrite(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']);

    ReplyDelete
  18. hi
    for me showing error in imdir command ... kindly help me pls

    ReplyDelete
  19. Yeah.. it's not accepting imdir command.

    ReplyDelete
  20. imdir is an input you send to the function.

    ReplyDelete
  21. Hi, 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!

    ReplyDelete
  22. can any1 help?
    i 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, '');

    ReplyDelete
  23. i hv entered following parameters
    ext='jpg'
    imdir='c:\C:\Users\Usman Khan\Documents\MATLAB\done'
    scale=0
    framerate=5
    filename='out'
    playflag=8

    ReplyDelete
  24. it is playing the video along with above mentioned 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, '');

    also how to save this video,as this code is just playing it,not saving it.

    ReplyDelete
    Replies
    1. Try:
      scale = 0.75

      The function saves the movie in "imdir" under the name "filename".

      Delete
  25. hii i have used the code for images to avi movie
    but 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)

    ReplyDelete
  26. im getting error as below
    Error in ==> im2av at 33
    im=imread(imgname) ;
    please help me

    ReplyDelete
  27. Error using movie2avi (line 58)
    Inputs 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;

    ReplyDelete
  28. After running the function It plays the video, but it couldn't create .avi file
    I 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

    ReplyDelete
    Replies
    1. Try
      movie2avi(mv, [imdir filesep filename '.avi'], 'fps', framerate); instead.

      Delete
  29. filearray=dir([imdir filesep '*.' ext]);
    Not enough input arguments.how to debug?

    ReplyDelete
  30. thanks your code is good

    ReplyDelete
  31. have 128 images and it takes them randomly, 1,10 100,102103 and so on why is this

    ReplyDelete