Making a movie from images in matlab

42 views (last 30 days)
Hey, I found this code from a link given by image analyst. It is perfect for want but i don't why this error is coming up...........
here is the code:
% Make an avi movie from a collection of PNG images in a folder.
% Specify the folder.
clear all; clc;
MATLAB = '/home/user/Documents/Tunde/MATLAB';
if ~isdir(MATLAB)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a directory listing.
filePattern = fullfile(MATLAB, '*.png');
pngFiles = dir(filePattern);
% Open the video writer object.
writerObj = VideoWriter('YourAVI.avi');
% Go through image by image writing it out to the AVI file.
for frameNumber = 1 : length(pngFiles)
% Construct the full filename.
baseFileName = pngFiles(frameNumber).name;
fullFileName = fullfile(MATLAB, baseFileName);
% Display image name in the command window.
fprintf(1, 'Now reading %s\n', fullFileName);
% Display image in an axes control.
thisimage = imread(fullFileName);
imshow(thisimage); % Display image.
drawnow; % Force display to update immediately.
% Write this frame out to the AVI file.
writeVideo(writerObj, thisimage);
end
% Close down the video writer object to finish the file.
close(writerObj);
and here is the comment:
Now reading /home/user/Documents/Tunde/MATLAB/Fitting (2).png
??? Error using ==> VideoWriter.VideoWriter>VideoWriter.writeVideo at 319
OBJ must be open before writing video. Call open(obj) before calling writeVideo.
Error in ==> movieeed at 28
writeVideo(writerObj, thisimage);

Accepted Answer

Marc Jakobi
Marc Jakobi on 8 Oct 2016
Edited: Marc Jakobi on 8 Oct 2016
Please make sure your code is formatted correctly. It is barely readable.
Anyways, you didn't actually open the video writer object in your code. The line
writerObj = VideoWriter('YourAVI.avi');
Should be followed by:
open(writerObj);
  3 Comments
Walter Roberson
Walter Roberson on 8 Oct 2016
Check to see that you have permission to write YourAVI.avi in your current directory. The default current directory for MATLAB is often a directory that you are not permitted to write to.
TTA
TTA on 9 Oct 2016
please how can I set the code to take any frame size of the images

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 9 Oct 2016
In order to be able to take any frame size for the images, you have three choices:
  1. Allow the first image file to control the size of the video and change all other image files to be the size of the first, by cropping or padding images or by resizing them; Or
  2. Scan through the information about all of the images to find the largest height and the largest width. Then go back and starting at the first image, make all of the other image files be the size of the first, by cropping or padding images or by resizing them; Or
  3. If you have enough memory to hold all of the images at the same time, then start reading the images and store them all in an array as you go. If you encounter an image which is larger in one direction than any you have read so far, then pad the array that you have created so far to extend it out to that size, and then store the new image; if you encounter an image which is smaller than the current largest size, then store it in one corner of the current plane (or figure out how to center it in the current plane). When everything has been read in, the array will now be the maximum size and you can proceed to write the array as a movie.
To put it explicitly: it is not possible to tell the video writer to save a frame that is larger or smaller than the first frame that you wrote in the movie, so you need to take steps to make all of the frames the same size.

Image Analyst
Image Analyst on 9 Oct 2016
Edited: Image Analyst on 9 Oct 2016
See my attached m-file. It makes a movie from a bunch of image files stored in a folder. I'm using it to create time lapse movie of a new lab building being built outside our lab. (My photos are not included - you use your own photos of course).

Community Treasure Hunt

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

Start Hunting!