montage
Display multiple images as rectangular montage
Syntax
Description
montage(
displays a montage of
images specified by imagelist
)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.
montage(___,
treats all
grayscale and binary images (specified using any of the preceding syntaxes) as
indexed images and displays them with the specified colormap
map
)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(___,
uses
name-value arguments to customize the display of the image montage.Name=Value
)
returns a
handle to the single image object that contains all the frames displayed.img
= montage(___)
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
Create Montage Containing Images of Different Types and Sizes
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"})
Create Montage from Image Datastore
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)
Create Montage from Multiframe Image
Load an MRI dataset.
load mristack
Display the dataset. montage
treats the data as a multiframe image and displays each slice.
montage(mristack)
Create Montage from Images in Files
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]);
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]);
Customize Number of Images in Montage
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);
Input Arguments
imagelist
— Set of images
cell array | vector of strings
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
imds
— Image datastore
ImageDatastore
object
Image datastore, specified as an ImageDatastore
object.
I
— Multiframe image
numeric array | dlarray
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
map
— Colormap
c-by-3 numeric matrix
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)
BackgroundColor
— Background color
"black"
(default) | RGB triplet | color name | short color name
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 Name | Short Name | RGB Triplet | Appearance |
---|---|---|---|
"red" | "r" | [1 0 0] |
|
"green" | "g" | [0 1 0] |
|
"blue" | "b" | [0 0 1] |
|
"cyan"
| "c" | [0 1 1] |
|
"magenta" | "m" | [1 0 1] |
|
"yellow" | "y" | [1 1 0] |
|
"black" | "k" | [0 0 0] |
|
"white" | "w" | [1 1 1] |
|
Here are the RGB triplets for the default colors that MATLAB uses in many types of plots.
RGB Triplet | Appearance |
---|---|
[0 0.4470 0.7410] |
|
[0.8500 0.3250 0.0980] |
|
[0.9290 0.6940 0.1250] |
|
[0.4940 0.1840 0.5560] |
|
[0.4660 0.6740 0.1880] |
|
[0.3010 0.7450 0.9330] |
|
[0.6350 0.0780 0.1840] |
|
Example: "BackgroundColor","r"
Example: "BackgroundColor","green"
Example: "BackgroundColor",[0 0.4470
0.7410]
BorderSize
— Padding around each thumbnail image
[0 0]
(default) | nonnegative integer | 1-by-2 vector of nonnegative integers
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
DisplayRange
— Display range
1-by-2 vector
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
Indices
— Frames to display
array of positive integers
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
— Interpolation technique
"nearest"
(default) | "bilinear"
Interpolation technique used to resize the montage for display, specified as one of these values.
Value | Description |
---|---|
"nearest" | Nearest neighbor interpolation (default) |
"bilinear" | Bilinear interpolation |
Parent
— Parent of image object
axes object
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.
Size
— Number of rows and columns of images
2-element vector
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
ThumbnailInterpolation
— Interpolation method to resize images for thumbnails
"bicubic"
(default) | "bilinear"
| "nearest"
| ...
Interpolation method to resize images for thumbnails, specified as one of these values.
Method | Description |
---|---|
| 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 interpolation; the output pixel value is a weighted average of pixels in the nearest 2-by-2 neighborhood. |
| 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
ThumbnailSize
— Size of each thumbnail
2-element vector of positive integers | []
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
img
— Montage image
Image
object
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 typedouble
using theim2double
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 themontage
function and before thetitle
function.If you specify the parent axes using theI = imread("peppers.png"); montage({I,I}) ax = gca; ax.PositionConstraint = "outerposition"; title("Peppers");
Parent
name-value argument, set thePositionConstraint
property of the specified parent axes object. For more details about axes position properties, see Control Axes Layout.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.
Version History
Introduced before R2006aR2024b: Control method to resize images for thumbnails
The new ThumbnailInterpolation
name-value argument enables
you to specify the interpolation method used to resize images for thumbnails.
There is no change to how the function resizes the montage for display.
R2024b: Support for deep learning arrays
The montage
function now accepts dlarray
input for deep learning applications.
R2024b: Support for GPU acceleration
montage
now supports GPU acceleration
(requires Parallel Computing Toolbox™).
R2022b: Resizes images for thumbnails using bicubic interpolation
The montage
function now uses bicubic interpolation to resize
images to form thumbnails. There is no change to how the function resizes the
montage for display.
R2020b: Resizes images for thumbnails using nearest-neighbor interpolation
The montage
function now uses nearest-neighbor interpolation
to resize images to form thumbnails. Before, the function used bicubic
interpolation.
There is no change to how the function resizes the montage for display.
R2018a: Display images in an image datastore, and more formats of 3-D volumetric images
The montage
function now can display images in an imageDatastore
.
Additionally, montage
no longer requires grayscale, binary,
and indexed volumetric images to be 4-D arrays of size
m-by-n-by-1-by-p.
Instead, you can specify a 3-D array of size
m-by-n-by-p.
R2018a: Changes to display of single images, indexed images, and thumbnails
You can now specify the size of the image thumbnails displayed in the montage by
using the new ThumbnailSize
name-value argument.
Starting in R2018a, there are several changes to the behavior of
montage
.
Previous Behavior | New Behavior | Compatibility Mode |
---|---|---|
If you pass a single truecolor (RGB) image to
montage , then the function displays a single
truecolor image. | montage displays each color channel of an RGB
image as three separate grayscale images. | Use the imshow function to display a truecolor
image imshow(RGB) . |
If one or more of the image files contained an indexed image,
then montage used the colormap from the first
indexed image file. | montage converts any indexed image into its
corresponding RGB version using the internal colormap present in the
file. | Explicitly read the colormap of the first indexed image and then
use the syntax montage(filenames,cmap) . |
Each constituent thumbnail preserved the full image size. | Each thumbnail is resized to the specified (or default)
ThumbnailSize with appropriate
zero-padding. | Use the syntax
montage(...,"Thumbnail",[]) . |
If you specify a set of indexed images, then
montage used the colormap of the first image
to set the colormap of the axes. This enabled you to change the
colormap of all the images displayed after creating the montage by
changing the colormap of the axes. | If you specify a set of indexed images, then
montage converts each one to an RGB image
using its internal colormap or the colormap specified in the command
line. Changing the colormap of the axes has no effect. | Explicitly specify the colormap at time of creation, using the
syntax montage(filenames,cmap) . |
See Also
Apps
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)