Main Content

montage

Display multiple images as rectangular montage

Description

montage(imagelist) displays a montage of images specified by imagelist. The images can be of different types and sizes. By default, the montage function arranges the images so that they roughly form a square.

example

montage(imds) displays a montage of the images in the image datastore imds.

example

montage(I) displays all frames of a multiframe image I.

example

montage(___,map) treats all grayscale and binary images (specified using any of the preceding syntaxes) as indexed images and displays them with the specified colormap map. If you specify images using filenames or an image datastore, then map overrides any internal colormap present in the image files. montage does not modify the colormap of RGB images.

montage(___,Name=Value) uses name-value arguments to customize the display of the image montage.

example

img = montage(___) returns a handle to the single image object that contains all the frames displayed.

Note

Medical Imaging Toolbox™ extends the functionality of the montage (Image Processing Toolbox™) function to display data from a medicalImage (Medical Imaging Toolbox) or medicalVolume (Medical Imaging Toolbox) object. For more information, see montage (Medical Imaging Toolbox).

Examples

collapse all

Read several images of different types and sizes into the workspace.

imRGB = imread("peppers.png");
imGray = imread("coins.png");

Display a montage containing all of the images.

figure
montage({imRGB,imGray,"cameraman.tif"})

Figure contains an axes object. The hidden axes object contains an object of type image.

Create an ImageDatastore object containing a series of ten images from the Image Processing Toolbox™ sample image folder.

fileFolder = fullfile(matlabroot,"toolbox","images","imdata");
imds = imageDatastore(fullfile(fileFolder,"AT3*"));

Display the contents of the datastore as a montage.

montage(imds)

Figure contains an axes object. The hidden axes object contains an object of type image.

Load an MRI dataset.

load mristack

Display the dataset. montage treats the data as a multiframe image and displays each slice.

montage(mristack)

Figure contains an axes object. The hidden axes object contains an object of type image.

Create a montage from a series of images in files. Make the montage a 2-by-5 rectangle. Then, create a second montage, this time using the "DisplayRange" name-value argument to highlight structures in the image.

Display the Images as a Rectangular Montage

Create a string array containing a series of file names.

fileFolder = fullfile(matlabroot,"toolbox","images","imdata");
dirOutput = dir(fullfile(fileFolder,"AT3_1m4_*.tif"));
fileNames = string({dirOutput.name});

Display the images as a montage. Specify the shape of the montage as a 2-by-5 rectangle.

montage(fileNames,"Size",[2 5]);

Figure contains an axes object. The hidden axes object contains an object of type image.

Adjust the Contrast of the Images in the Montage

In another figure, create the same 2-by-5 montage. In addition, specify the display range to adjust the contrast of the images in the montage.

figure 
montage(fileNames,"Size",[2 5],"DisplayRange",[75 200]);

Figure contains an axes object. The hidden axes object contains an object of type image.

View all the images in a MRI data set using montage with default settings. There are 27 images in the set.

load mri
montage(D,map)

Figure contains an axes object. The hidden axes object contains an object of type image.

Create a new montage containing only the first 9 images.

figure
montage(D,map,Indices=1:9);

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

Set of images, specified as a cell array or a vector of strings. Use a cell array to display any combination of these elements:

  • Numeric matrix of size m-by-n representing a grayscale image

  • Numeric array of size m-by-n-by-3 representing a color image

  • Character vector or string specifying the name of an image file.

If you want to specify all images by filename, then you can use a vector of strings. If the files are not in the current folder or in a folder on the MATLAB® path, then specify the full path name. For more information, see imread.

Data Types: single | double | int16 | uint8 | uint16 | logical | char | string | cell

Image datastore, specified as an ImageDatastore object.

Multiframe image, specified as a numeric array of one of these sizes:

  • m-by-n-by-k, representing a series of k binary or grayscale images.

  • m-by-n-by-1-by-k, representing a series of k binary or grayscale images.

  • m-by-n-by-3-by-k, representing a series of k truecolor images.

You can also specify the multiframe image as a dlarray (Deep Learning Toolbox) object:

  • An unformatted dlarray object containing a numeric array of one of the above sizes.

  • A formatted dlarray object with two spatial dimensions (labeled "S"), and at most one dimension labeled "T", "B", or "U". If the object has a channel dimension (labeled "C"), then the length of that dimension must be 1 or 3.

Data Types: single | double | int16 | uint8 | uint16 | logical

Colormap, specified as a c-by-3 numeric matrix with values in the range [0, 1]. Each row is a three-element RGB triplet that specifies the red, green, and blue components of a single color of the colormap.

Data Types: double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: montage(I,Indices=1:9) creates a montage of the first nine frames of multiframe image I.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: montage(I,"Indices",1:9)

Background color, specified as specified as an RGB triplet, a color name, or a short color name. The montage function fills all blank spaces with the background color, including the space specified by BorderSize. If you specify a background color, then the montage function renders the output as an RGB image.

You can specify any color using an RGB triplet. An RGB triplet is a 3-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0, 1].

You can specify some common colors by name as a string scalar or character vector. This table lists the named color options and the equivalent RGB triplets.

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

A rectangle colored pure red

"green""g"[0 1 0]

A rectangle colored pure green

"blue""b"[0 0 1]

A rectangle colored pure blue

"cyan" "c"[0 1 1]

A rectangle colored pure cyan

"magenta""m"[1 0 1]

A rectangle colored pure magenta

"yellow""y"[1 1 0]

A rectangle colored pure yellow

"black""k"[0 0 0]

A rectangle colored black

"white""w"[1 1 1]

A rectangle colored white

Here are the RGB triplets for the default colors that MATLAB uses in many types of plots.

RGB TripletAppearance
[0 0.4470 0.7410]

A rectangle colored medium blue

[0.8500 0.3250 0.0980]

A rectangle colored reddish-orange

[0.9290 0.6940 0.1250]

A rectangle colored dark yellow

[0.4940 0.1840 0.5560]

A rectangle colored dark purple

[0.4660 0.6740 0.1880]

A rectangle colored light green

[0.3010 0.7450 0.9330]

A rectangle colored light blue

[0.6350 0.0780 0.1840]

A rectangle colored dark red

Example: "BackgroundColor","r"

Example: "BackgroundColor","green"

Example: "BackgroundColor",[0 0.4470 0.7410]

Padding around each thumbnail image, in pixels, specified as a nonnegative integer or a 1-by-2 vector of nonnegative integers. The montage function pads the image borders with the background color, BackgroundColor.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Display range of grayscale images, specified as 1-by-2 vector of the form [low high]. All pixel values less than or equal to low display as black. All pixel values greater than or equal to high display as white. If you specify an empty matrix ([]), then montage uses the minimum and maximum pixel values of the images. If any of the images are color images, then the montage function ignores this argument for all images.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Frames to display in the montage, specified as an array of positive integers. The montage function interprets the values as indices into the set of images specified by imagelist, imds, or I.

By default, the montage function displays all image files or frames.

Example: "Indices",1:4 create a montage of the first four frames in I

Example: "Indices",1:2:20 displays every other frame.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Interpolation technique used to resize the montage for display, specified as one of these values.

ValueDescription
"nearest"Nearest neighbor interpolation (default)
"bilinear"Bilinear interpolation

Parent of the image object created by montage, specified as an axes object. The montage function resizes the image to fit the extents available in the parent axes.

Number of rows and columns of images, specified as a 2-element vector of the form [nrows ncols].

If you specify NaN or Inf for a particular dimension, then the montage function calculates the value of the dimension to display all images in the montage. For example, if Size is [2 NaN], then the montage will have two rows and the minimum number of columns to display all images. When there is a mismatch between Size and number of images (frames) specified, then the montage function creates the tiled image based on Size.

Data Types: single | double

Interpolation method to resize images for thumbnails, specified as one of these values.

MethodDescription

"bicubic"

Bicubic interpolation; the output pixel value is a weighted average of pixels in the nearest 4-by-4 neighborhood.

Note

Bicubic interpolation can produce pixel values outside the original range.

"bilinear"

Bilinear interpolation; the output pixel value is a weighted average of pixels in the nearest 2-by-2 neighborhood.

"nearest"

Nearest-neighbor interpolation; the output pixel is assigned the value of the pixel that the point falls within. No other pixels are considered.

"box"

Box-shaped interpolation kernel

"triangle"Triangular interpolation kernel (equivalent to "bilinear")
"cubic"Cubic interpolation kernel (equivalent to "bicubic")
"lanczos2"Lanczos-2 interpolation kernel
"lanczos3"Lanczos-3 interpolation kernel

The default resizing method, "bicubic", typically works well when the images are larger than the thumbnails. If the size of your images is smaller than the thumbnails and you want to avoid smoothing the pixel values, then consider setting ThumbnailInterpolation as "nearest".

Data Types: char | string

Size of each thumbnail, in pixels, specified as a 2-element vector of positive integers. The aspect ratio of each image is preserved, and any blank space is filled with the background color, BackgroundColor.

If you specify an empty array ([]), then the thumbnail size is the full size of the first image. If you specify either element as NaN or Inf, then the montage function calculates the corresponding value automatically to preserve the aspect ratio of the first image.

Data Types: single | double

Output Arguments

collapse all

Montage image, returned as an Image object.

Tips

  • If you specify an indexed image, then montage converts it to RGB using the colormap present in the file.

  • If there is a data type mismatch between images, then the montage function converts all images to data type double using the im2double function.

  • When calculating the number of images to display horizontally and vertically, montage considers the aspect ratio of the images, so that the displayed montage is nearly square.

  • Figure titles can appear cut off in the Live Editor. To ensure the whole title is visible, set the PositionContraint property of the parent axes object to "outerposition". Update the property value after the montage function and before the title function.

    I = imread("peppers.png");
    montage({I,I})
    ax = gca;
    ax.PositionConstraint = "outerposition";
    title("Peppers");
    If you specify the parent axes using the Parent name-value argument, set the PositionConstraint property of the specified parent axes object. For more details about axes position properties, see Control Axes Layout.

Extended Capabilities

Version History

Introduced before R2006a

expand all