This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

montage

Display multiple image frames as rectangular montage

Syntax

montage(filenames)
montage(I)
montage(imagelist)
montage(imds)
montage(X,map)
montage(___,Name,Value)
img = montage(___)

Description

example

montage(filenames) displays a montage of the images with file names specified in filenames. The images can be of different types and sizes. By default, montage arranges the images so that they roughly form a square.

example

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

montage(imagelist) displays a montage of the images specified in the cell array imagelist.

example

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

montage(X,map) treats all grayscale images in X as indexed images and applies the specified color map map for all frames. If X is a cell array of file names, then map overrides any internal color map present in the image files.

example

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

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

Examples

collapse all

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 parameter 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]);

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]);

Create an m-by-n-by-4 multiframe image from a series of grayscale images. The images must all be the same size.

img1 = imread('AT3_1m4_01.tif');
img2 = imread('AT3_1m4_02.tif');
img3 = imread('AT3_1m4_03.tif');
img4 = imread('AT3_1m4_04.tif');
multi = cat(3,img1,img2,img3,img4);

Display a montage of the images in the multiframe image.

montage(multi);

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'})

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)

Create a new montage containing only the first 9 images.

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

Create an ImageDatastore 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 imageDatastore as a montage.

img = montage(imds)

img = 
  Image with properties:

           CData: [1024x1026 uint8]
    CDataMapping: 'scaled'

  Show all properties

Input Arguments

collapse all

Image data store, specified as an ImageDatastore.

Names of files containing image, specified as an array of strings or an n-by-1 or 1-by-n cell array of string scalars or character vectors. If the files are not in the current folder or in a folder on the MATLAB® path, then specify the full path name. See the imread command for more information.

Data Types: char | string | cell

Set of images, specified as an n-by-1 or 1-by-n cell array of numeric matrices of size m-by-n or m-by-n-by-3.

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

Multiframe image array, specified as one of the following:

  • m-by-n-by-k numeric array representing a sequence of k binary or grayscale images

  • m-by-n-by-1-by-k numeric array representing a sequence of k binary or grayscale images

  • m-by-n-by-3-by-k numeric array representing a sequence of k truecolor images

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

Indexed image, specified as a 2-D numeric matrix.

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

Color map, specified as an c-by-3 numeric matrix, where c is the number of colors in the color map.

Data Types: double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: montage(D,map,'Indices',1:9)

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

Padding around each thumbnail image, specified as a nonnegative integer or a 1-by-2 vector of nonnegative integers. montage 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 in array I, 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.

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 array I or into cell array filenames or imagelist.

By default, montage displays all frames or image files.

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

Example: montage(I,'Indices',1:2:20) displays every other frame.

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

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 montage 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, imtile creates the tiled image based on 'Size'.

Data Types: single | double

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 ([]), montage uses the full size of the first image as the thumbnail size. If you specify either element as NaN or Inf, then montage 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 a handle to a single matlab.graphics.primitive.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 all images are rescaled to be 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.

Introduced before R2006a