Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

## Working with Image Sequences

### Overview of Toolbox Functions That Work with Image Sequences

Some applications work with collections of images related by time, such as frames in a movie, or by spatial location, such as magnetic resonance imaging (MRI) slices. These collections of images are referred to by a variety of names, such as image sequences or image stacks.

The ability to create N-dimensional arrays can provide a convenient way to store image sequences. For example, an m-by-n-by-p array can store an array of p two-dimensional images, such as grayscale or binary images, as shown in the following figure. An m-by-n-by-3-by-p array can store truecolor images where each image is made up of three planes.

Multidimensional Array Containing an Image Sequence

Many toolbox functions can operate on multi-dimensional arrays and, consequently, can operate on image sequences. For example, if you pass a multi-dimensional array to the imtransform function, it applies the same 2-D transformation to all 2-D planes along the higher dimension.

Some toolbox functions that accept multi-dimensional arrays, however, do not by default interpret an m-by-n-by-p or an m-by-n-by-3-by-p array as an image sequence. To use these functions with image sequences, you must use particular syntax and be aware of other limitations. The following table lists these toolbox functions and provides guidelines about how to use them to process image sequences. For information about displaying image sequences, see Viewing Image Sequences.

Function

Image Sequence Dimensions

Guideline When Used with an Image Sequence

bwlabeln

m-by-n-by-p only

Must use the bwlabeln(BW,conn) syntax with a 2-D connectivity.

deconvblind

m-by-n-by-p or
m-by-n-by-3-by-p

PSF argument can be either 1-D or 2-D.

deconvlucy

m-by-n-by-p or
m-by-n-by-3-by-p

PSF argument can be either 1-D or 2-D.

edgetaper

m-by-n-by-p or
m-by-n-by-3-by-p

PSF argument can be either 1-D or 2-D.

entropyfilt

m-by-n-by-p only

nhood argument must be 2-D.

imabsdiff

m-by-n-by-p or
m-by-n-by-3-by-p

Image sequences must be the same size.

m-by-n-by-p or
m-by-n-by-3-by-p

Image sequences must be the same size. Cannot add scalar to image sequence.

imbothat

m-by-n-by-p only

SE argument must be 2-D.

imclose

m-by-n-by-p only

SE argument must be 2-D.

imdilate

m-by-n-by-p only

SE argument must be 2-D.

imdivide

m-by-n-by-p or
m-by-n-by-3-by-p

Image sequences must be the same size.

imerode

m-by-n-by-p only

SE argument must be 2-D.

imextendedmax

m-by-n-by-p only

Must use the imextendedmax(I,h,conn) syntax with a 2-D connectivity.

imextendedmin

m-by-n-by-p only

Must use the imextendedmin(I,h,conn) syntax with a 2-D connectivity.

imfilter

m-by-n-by-p or
m-by-n-by-3-by-p

With grayscale images, h can be 2-D. With truecolor images (RGB), h can be 2-D or 3-D.

imhmax

m-by-n-by-p only

Must use the imhmax(I,h,conn) syntax with a 2-D connectivity.

imhmin

m-by-n-by-p only

Must use the imhmin(I,h,conn) syntax with a 2-D connectivity.

imlincomb

m-by-n-by-p or
m-by-n-by-3-by-p

Image sequences must be the same size.

immultiply

m-by-n-by-p or
m-by-n-by-3-by-p

Image sequences must be the same size.

imopen

m-by-n-by-p only

SE argument must be 2-D.

imregionalmax

m-by-n-by-p only

Must use the imextendedmax(I,conn) syntax with a 2-D connectivity.

imregionalmin

m-by-n-by-p only

Must use the imextendedmin(I,conn) syntax with a 2-D connectivity.

imsubtract

m-by-n-by-p or
m-by-n-by-3-by-p

Image sequences must be the same size.

imtophat

m-by-n-by-p only

SE argument must be 2-D.

imwarp

m-by-n-by-p or
m-by-n-by-3-by-p

TFORM argument must be 2-D.

m-by-n-by-p or
m-by-n-by-3-by-p

PADSIZE argument must be a two-element vector.

rangefilt

m-by-n-by-p only

NHOOD argument must be 2-D.

stdfilt

m-by-n-by-p only

NHOOD argument must be 2-D.

tformarray

m-by-n-by-p or
m-by-n-by-3-by-p

• T must be 2-D to 2-D (compatible with imtransform).

• R must be 2-D.

• TDIMS_A and TDIMS_B must be 2-D, i.e., [2 1] or [1 2].

• TSIZE_B must be a two-element array [D1 D2], where D1 and D2 are the first and second transform dimensions of the output space.

• TMAP_B must be [TSIZE_B 2].

• F can be a scalar or a p-by-1 array, for m-by-n-by-p arrays. Or F can be a scalar, 1-by-p array; 3-by-1 array; or 3-by-p array, for m-by-n-by-3-by-p arrays.

watershed

m-by-n-by-p only

Must use watershed(I,conn) syntax with a 2-D connectivity.

### Process Image Sequences

This example starts by reading a series of images from a directory into the MATLAB® workspace, storing the images in an m-by-n-by-p array. The example then passes the entire array to the stdfilt function and performs standard deviation filtering on each image in the sequence. Note that, to use stdfilt with an image sequence, you must use the nhood argument, specifying a 2-D neighborhood.

```% Create an array of filenames that make up the image sequence
fileFolder = fullfile(matlabroot,'toolbox','images','imdemos');
dirOutput = dir(fullfile(fileFolder,'AT3_1m4_*.tif'));
fileNames = {dirOutput.name}';
numFrames = numel(fileNames);

% Preallocate the array
sequence = zeros([size(I) numFrames],class(I));
sequence(:,:,1) = I;

% Create image sequence array
for p = 2:numFrames
end

% Process sequence
sequenceNew = stdfilt(sequence,ones(3));

% View results
figure;
for k = 1:numFrames
imshow(sequence(:,:,k));
title(sprintf('Original Image # %d',k));
pause(1);
imshow(sequenceNew(:,:,k),[]);
title(sprintf('Processed Image # %d',k));
pause(1);
end```

### Process Multi-Frame Image Arrays

The toolbox includes two functions, immovie and montage, that work with a specific type of multi-dimensional array called a multi-frame array. In this array, images, called frames in this context, are concatenated along the fourth dimension. Multi-frame arrays are either m-by-n-by-1-by-p, for grayscale, binary, or indexed images, or m-by-n-by-3-by-p, for truecolor images, where p is the number of frames.

For example, a multi-frame array containing five, 480-by-640 grayscale or indexed images would be 480-by-640-by-1-by-5. An array with five 480-by-640 truecolor images would be 480-by-640-by-3-by-5.

 Note   To process a multi-frame array of grayscale images as an image sequence, as described in Working with Image Sequences, you can use the squeeze function to remove the singleton dimension.

You can use the cat command to create a multi-frame array. For example, the following stores a group of images (A1, A2, A3, A4, and A5) in a single array.

`A = cat(4,A1,A2,A3,A4,A5)`

You can also extract frames from a multiframe image. For example, if you have a multiframe image MULTI, this command extracts the third frame.

`FRM3 = MULTI(:,:,:,3)`

Note that, in a multiframe image array, each image must be the same size and have the same number of planes. In a multiframe indexed image, each image must also use the same colormap.