convert images to video

hello there,
I need to make movie/video to show how the graph changes over time.
i have images named "hgate00.png" to "hgate79.png", each images have plot that keep changing slightly with each images.
i need to show how that changes through video of that 80 images.
also where should I kept that images and MATLAB file in folder so that they can work?
Thank you in advance.

Answers (3)

KSSV
KSSV on 27 Apr 2020
imgFile = dir('*.png') ;
N = length(imgFiles) ;
% create the video writer with 1 fps
writerObj = VideoWriter('myVideo.avi');
writerObj.FrameRate = 10;
% open the video writer
open(writerObj);
% write the frames to the video
for i=1:N
img = imgFiles(i).name ;
I = imread(img) ;
imshow(I) ;
F = getframe(gcf) ;
writeVideo(writerObj, F);
end
% close the writer object
close(writerObj);

8 Comments

>> trial2
Error using VideoWriter/writeVideo (line 368)
Frame must be 748 by 536
Error in trial2 (line 14)
writeVideo(writerObj, F);
This is the error I got. could you please help me out?? Thank you
KSSV
KSSV on 29 Apr 2020
It looks like the sizes of images are different......you need to resize the images to same size before you write them. Read aboit imresize.
This is the changes I have made into scripts,
v = VideoWriter('newfile.avi','Uncompressed AVI');
open(v)
for k = 01:79
A = imread(['hgate', num2str(k,'%02d'), '.png']);
A2 = imresize(A, [431 NaN]);
writeVideo(v,A2)
end
close(v)
but still the same error is coming with different number. Any help please??
>> trial1
Error using VideoWriter/writeVideo (line 368)
Frame must be 431 by 332
Error in trial1 (line 7)
writeVideo(v,A2)
>> trial1
Error using VideoWriter/writeVideo (line 368)
Frame must be 559 by 431
Error in trial1 (line 7)
writeVideo(v,A2)
KSSV
KSSV on 30 Apr 2020
Why NaN? You need to fix a size first and resize every image into this size.
Angu
Angu on 30 Apr 2020
how??
KSSV
KSSV on 30 Apr 2020
Edited: KSSV on 30 Apr 2020
Fix your desired size say to 512*512.....and you resize every image before you write into video using imresize.
Thanks @KSSV. There is a typo in the first line of your code. It should be: imgFiles = dir('*.png')
I want to add something that might save some time for someone:
If you use imshow and the images are larger than your screen size, then imshow will downscale the image to fit in your screen. Therefore all the parameters of writevideo will result in (in some cases) very low video quality.
A workaround to this is to use:
F=im2frame(I);
Instead of imshow and getframe, just like @yanqi liu used in their solution.

Sign in to comment.

clc; clear all; close all;
selpath = uigetdir(fullfile(pwd),'Choose image folder');
if isequal(selpath, 0)
return;
end
imgFilePath = selpath;
filename_out = fullfile(pwd, 'out.avi');
% "hgate00.png" to "hgate79.png"
startnum = 0;
endnum = 79;
% create video file
writerObj = VideoWriter(filename_out);
writerObj.FrameRate = 24;
open(writerObj);
h = waitbar(0, '', 'Name', 'Write Video File...');
steps = endnum - startnum;
for num = startnum : endnum
file = sprintf('hgate%02d.png', num);
file = fullfile(imgFilePath, file);
frame = imread(file);
frame = im2frame(frame);
writeVideo(writerObj,frame);
pause(0.01);
step = num - startnum;
waitbar(step/steps, h, sprintf('Process:%d%%', round(step/steps*100)));
end
close(writerObj);
close(h);

1 Comment

thanks a lot! no other video editing programs do this so easy as this code.

Sign in to comment.

Christophe
Christophe on 27 Apr 2020
Edited: Christophe on 27 Apr 2020
First you need to read your image. Use the imread function to do so.
Then, you can use the VideoWriter object to write videofiles.
Here is an untested program. Your png files must be in the current work directory. It will output the video in the same directory.
v = VideoWriter('newfile.avi','Uncompressed AVI');
open(v)
for k = 0:79
A = imread(['hgate', num2str(k,'%02d'), '.png');
writeVideo(v,A)
end
close(v)
Please check the documentation for further explanation.

5 Comments

>> vidtry
Error using imread>get_full_filename (line 566)
File "hgate" does not exist.
Error in imread (line 375)
fullname = get_full_filename(filename);
Error in vidtry (line 5)
A = imread(['hgate'], num2str(k,'%02d'), '.png');
This is the error I got!! Could you please have a look at this and help????? Thank you
KSSV
KSSV on 30 Apr 2020
May be you fix the size of image to 512*512. Resize every image image to this using imresize.
Angu
Angu on 30 Apr 2020
one more problem...
few images are not there,
such as hgate71.png,hgate75.png.
how shoud I ignore the missing images?
Angu
Angu on 30 Apr 2020
the script is working for 01 to 70 images, but the video has just 01 to 29 images in a second and then just blank... how to solve that?? also the images are moving way to fast...
Angu
Angu on 30 Apr 2020
Its is not making video after hgate29.png files at all.
I tried creating another video for images 29 and onwards but no luck....

Sign in to comment.

Products

Release

R2019b

Asked:

on 27 Apr 2020

Commented:

on 18 Jul 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!