Documentation Center

  • Trial Software
  • Product Updates

Convert Between Image Sequences and Video

This example shows how to convert between video files and sequences of image files using VideoReader and VideoWriter.

The sample file named shuttle.avi contains 121 frames. Convert the frames to image files using VideoReader and the imwrite function. Then, convert the image files to an AVI file using VideoWriter.


Create a temporary working folder to store the image sequence.

workingDir = tempname;

Construct a VideoReader Object

Create a VideoReader object to use for reading frames from the file.

shuttleVideo = VideoReader('shuttle.avi');

Create the Image Sequence

Loop through the video, reading each frame into a width-by-height-by-3 array named img. Write out each image to a JPEG file with a name in the form imgN.jpg, where N is the frame number:

for ii = 1:shuttleVideo.NumberOfFrames
    img = read(shuttleVideo,ii);

    % Write out to a JPEG file (img1.jpg, img2.jpg, etc.)

Read and Sort the Image Sequence into MATLAB®

Find all the JPEG file names in the images folder. Convert the set of image names to a cell array.

imageNames = dir(fullfile(workingDir,'images','*.jpg'));
imageNames = {}';

Notice that the image file names are not in numeric order.


To sort the file names, extract the frame numbers from the file names and use them to sort the array.

First, match any file names that contain a sequence of numeric digits. Convert the strings to doubles.

imageStrings = regexp([imageNames{:}],'(\d*)','match');
imageNumbers = str2double(imageStrings);

Sort the frame numbers from lowest to highest. The sort function returns an index matrix that indicates how to order the associated files.

[~,sortedIndices] = sort(imageNumbers);
sortedImageNames = imageNames(sortedIndices);

The sequence file names are now sorted.


Create a New Video with the Image Sequence

Construct a VideoWriter object, which creates a Motion-JPEG AVI file by default.

outputVideo = VideoWriter(fullfile(workingDir,'shuttle_out.avi'));
outputVideo.FrameRate = shuttleVideo.FrameRate;

Loop through the image sequence, load each image, and then write it to the video.

for ii = 1:length(sortedImageNames)
    img = imread(fullfile(workingDir,'images',sortedImageNames{ii}));


Finalize the video file.


View the Final Video

Construct a reader object.

shuttleAvi = VideoReader(fullfile(workingDir,'shuttle_out.avi'));

Create a MATLAB movie struct from the video frames.

mov(shuttleAvi.NumberOfFrames) = struct('cdata',[],'colormap',[]);
for ii = 1:shuttleAvi.NumberOfFrames
    mov(ii) = im2frame(read(shuttleAvi,ii));

Resize the current figure and axes based on the video's width and height, and view the first frame of the movie.

set(gcf,'position', [150 150 shuttleAvi.Width shuttleAvi.Height])
set(gca,'position',[0 0 shuttleAvi.Width shuttleAvi.Height])

axis off;

Play back the movie once at the video's frame rate.



Video of the Space Shuttle courtesy of NASA.

Was this topic helpful?