displays the grayscale image
I in a figure.
imshow uses the default display range for the image
data type and optimizes figure, axes, and image object properties for image
displays the binary image
BW in a figure. For binary
imshow displays pixels with the value
0 (zero) as black and
imshow(___, displays an
image, using name-value arguments to control aspects of the operation.
Display RGB, Grayscale, Binary, or Indexed Image
Display an RGB (truecolor), grayscale, binary, or indexed image using the
Display an RGB Image
Read a sample RGB image,
peppers.png, into the MATLAB® workspace.
rgbImage = imread("peppers.png");
Display the RGB image using
Display a Grayscale Image
Convert the RGB image to a grayscale image by using the
grayImage = im2gray(rgbImage);
Display the grayscale image using
Display a Binary Image
Convert the grayscale image to a binary image by using thresholding.
meanVal = mean(grayImage,"all"); binaryImage = grayImage >= meanVal;
Display the binary image using
Display an Indexed Image
Read a sample indexed image,
corn.tif, into the MATLAB workspace.
[corn_indexed,map] = imread("corn.tif");
Display the indexed image using
Display Image from File
Display an image stored in a file.
filename = "peppers.png"; imshow(filename)
Change Colormap of Displayed Image
Load a sample grayscale volumetric image,
mri.mat, into the variable
D in the workspace. Remove the singleton dimension of the volume using the
load("mri.mat"); vol = squeeze(D);
Select a slice from the middle of the volume. Display the slice using the
copper colormap and scaling the display range to the range of pixel values.
sliceZ = vol(:,:,13); imshow(sliceZ,,Colormap=copper)
Change the colormap for the image using the
Scale Display Range of Image
Read a truecolor (RGB) image into the workspace. The data type of the image is
RGB = imread("peppers.png");
Extract the green channel of the image. The green channel is the second color plane.
G = RGB(:,:,2); imshow(G)
Create a filter that detects horizontal edges in the image.
filt = [-1 -1 -1; 0 0 0; 1 1 1];
Filter the green channel of the image using the
filter2 function. The result is an image of data type
double, with a minimum value of -422 and a maximum value of 656. Pixels with a large magnitude in the filtered image indicate strong edges.
edgeG = filter2(filt,G);
Display the filtered image using
imshow with the default display range. For images of data type
double, the default display range is [0, 1]. The image appears black and white because the filtered pixel values exceed the range [0, 1].
Display the filtered image and scale the display range to the pixel values in the image. The image displays with the full range of grayscale values.
Magnify Image Using Nearest Neighbor and Bilinear Interpolation
Read the grayscale image from the
corn.tif file into the workspace. The grayscale version of the image is the second image in the file.
corn_gray = imread("corn.tif",2);
Select a small portion of the image. Display the detail image at 100% magnification using
corn_detail = corn_gray(1:100,1:100); imshow(corn_detail)
Display the image at 1000% magnification by using the "
InitialMagnification" name-value argument. By default,
inshow performs nearest neighbor interpolation of pixel values. The image has blocking artifacts.
Display the image at 1000% magnification, specifying the bilinear interpolation technique. The image appears smoother.
Display Image Using Associated Spatial Referencing Object
Read a grayscale image into the workspace. Display the image and get the
Image object created by the
I = imread("pout.tif"); hIm = imshow(I)
hIm = Image with properties: CData: [291x240 uint8] CDataMapping: 'scaled' Use GET to show all properties
YData properties of the
Image object. The axes limits show the spatial extent of the image. By default the spatial extent is equal to the size of the image.
spatialExtentIm = [hIm.XData; hIm.YData]
spatialExtentIm = 2×2 1 240 1 291
Create a spatial referencing object associated with the image. Use the referencing object to set the x- and y-axes limits in the world coordinate system.
RI = imref2d(size(I)); RI.XWorldLimits = [0 3]; RI.YWorldLimits = [2 5];
Display the image, specifying the spatial referencing object. Get the spatially-referenced
Image object created by the
hRef = imshow(I,RI);
YData properties of the spatially-referenced
Image object. The axes limits are now within the world limits specified by the spatial referencing object. The difference between
RI.XWorldLimits (and similarly
RI.YWorldLimits) arises because the former measures the distance between the centers of edge pixels whereas the latter measures the distance between the outer boundaries of edge pixels.
spatialExtentRef = [hRef.XData; hRef.YData]
spatialExtentRef = 2×2 0.0063 2.9937 2.0052 4.9948
I — Grayscale image
Grayscale image, specified as a matrix. A grayscale image can be of any numeric data type.
RGB — Truecolor image
Truecolor image, specified as an m-by-n-by-3 array.
If you specify a truecolor image of data type
double, then values should be in the range [0, 1]. If
pixel values are outside this range, then you can use the
rescale function to scale pixel values to the range [0, 1].
DisplayRange argument has no effect when the input
image is truecolor.
BW — Binary image
Binary image, specified as a matrix.
X — Indexed image
2-D matrix of positive integers
Indexed image, specified as a 2-D matrix of positive integers. The values in
X are indices into the colormap specified by
map — Colormap
Colormap associated with indexed image
as a c-by-3 matrix. Each row of
is a three-element RGB triplet that specifies the red, green, and blue
components of a single color of the colormap. When
is of data type
values of the matrix are in the range [0, 1].
filename — Filename
string scalar | character vector
Filename, specified as a string scalar or character vector. The image must
be readable by the
imread function. The
imshow function displays the image, but does not
store the image data in the MATLAB® workspace. If the file contains multiple images, then
imshow displays the first image in the file.
[low high] — Grayscale image display range
Grayscale image display range, specified as a two-element vector. For more information, see
DisplayRange name-value argument.
R — 2-D spatial referencing object
2-D spatial referencing object associated with the input image, specified
Specify optional pairs of arguments as
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.
imshow("board.tif",Border="tight") displays the image
board.tif without space around the image in the
Before R2021a, use commas to separate each name and value, and enclose
Name in quotes.
imshow("board.tif","Border","tight") displays the image with
board.tif without space around the image in the
Border — Figure window border space
"loose" (default) |
Figure window border space, specified as
"loose". When set to
the figure window includes space around the image in the figure. When
"tight", the figure window does not include
any space around the image in the figure.
If the image is very small or if the figure contains other objects besides an image and its
imshow might use a border regardless of how
this argument is set.
Colormap — Colormap
Colormap of the axes, specified as a c-by-3 matrix with values in the range
[0, 1]. Each row of the matrix is a three-element RGB triplet that
specifies the red, green, and blue components of a single color of the
colormap. Use this argument to view grayscale images in false color. If
you specify an empty colormap (
), then the
imshow function ignores this argument.
cmap = copper;
DisplayRange — Grayscale image display range
two-element vector |
Display range of a grayscale image, specified as a two-element vector of the form
[low high]. The
function displays the value
low (and any value less
low) as black, and it displays the value
high (and any value greater than
high) as white. Values between
high are displayed as
intermediate shades of gray, using the default number of gray
If you specify an empty matrix (
imshow uses a display range of
[min(I(:)) max(I(:))]. In other words, the
minimum value in
I is black, and the maximum value is
If you do not specify a display range, then
imshow selects a default display range based on
the image data type.
Iis an integer type, then
defaults to the minimum and maximum representable values for that integer class. For example, the default display range for
uint16arrays is [0, 65535].
Iis of data type
double, then the default display range is [0, 1].
Including the argument name is optional, except when the image is
specified by a filename. The syntax
high]) is equivalent to
imshow(I,DisplayRange=[low high]). If you
imshow with a filename, then you must
InitialMagnification — Initial magnification of image display
100 (default) | numeric scalar |
Initial magnification of the image display, specified as a numeric scalar or
"fit". If set to
imshow displays the image at 100% magnification
(one screen pixel for each image pixel). If set to
the entire image to fit in the window.
imshow attempts to display the entire image at the specified
magnification. If the magnification value is so large that the image is
too big to display on the screen,
the image at the largest magnification that fits on the screen.
If the image is displayed in a figure with its
WindowStyle property set to
displays the image at the largest magnification that fits in the
Note: If you specify the axes position, then
imshow ignores any initial
magnification you might have specified and defaults to the
When you use
imshow with the
argument, the initial magnification must be
InitialMagnification is set to
"fit" and cannot be changed.
Interpolation — Interpolation technique
"nearest" (default) |
Interpolation method, specified as
"bilinear". MATLAB uses interpolation to display a scaled version of the
image on your screen. The value you choose does not affect the image
data. Choose an interpolation method based on your image content and the
effect you want to achieve:
"nearest"— Nearest neighbor interpolation. The value of a pixel located at (x, y) is the value of the pixel that is closest to (x, y) in the original image. This method is best when there are a small number of pixel values that represent distinct categories, or when you want to see individual pixels in a highly zoomed-in view.
"bilinear"— Bilinear interpolation. The value of a pixel located at (x, y) is a weighted average of the surrounding pixels in the original image. To minimize display artifacts, the
imshowfunction performs antialiasing when you shrink the image. This method is best in almost all other situations.
Parent — Parent axes of image object
Axes object |
Parent axes of image object, specified as an
Axes object or a
UIAxes object. Use the
name-value argument to build a UI that gives you control of the
Reduce — Indicator for subsampling
Indicator for subsampling image, specified as a numeric or logical
0). This argument is valid only when you use it
with the name of a TIFF file. Use the
to display overviews of very large images.
XData — X-axis limits of nondefault coordinate system
X-axis limits of nondefault coordinate system, specified as a two-element vector. This
argument establishes a nondefault spatial coordinate system by
specifying the image
XData. The value can have more
than two elements, but
imshow uses only the first
and last elements.
YData — Y-axis limits of nondefault coordinate system
Y-axis limits of nondefault coordinate system, specified as a two-element vector. The value
can have more than two elements, but
only the first and last elements.
himage — Image created by
Image created by the
imshow function, specified as an image
To change the colormap after you create the image, use the
You can create an axes on top of the axes created by
imshowby using the
hold oncommand after calling
You can use the Image Viewer app as an integrated environment for displaying images and performing common image processing tasks.
You can set Image Processing Toolbox™ preferences that modify the behavior of
imshowby using the
imshowfunction is not supported when you start MATLAB with the
Figure titles can appear cut off in the Live Editor. To ensure the whole title is visible, set the
PositionContraintproperty of the parent axes object to
"outerposition". Update the property value after the
imshowfunction and before the
titlefunction.If you specify the parent axes using the
I = imread("peppers.png"); imshow(I) ax = gca; ax.PositionConstraint = "outerposition"; title("Peppers");
Parentname-value argument, set the
PositionConstraintproperty of the specified parent axes object. For more details about axes position properties, see Control Axes Layout.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
This function operates on distributed arrays, but executes in the client MATLAB.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version HistoryIntroduced before R2006a
R2023a: MATLAB Online limits image display resolution
Online limits the maximum
imshow display resolution to
improve rendering speeds for large images. This behavior affects the on-screen
display, but it does not affect the image data. Before displaying an image,
imshow resizes the largest dimension to a maximum size of
512 pixels. The smaller dimension adjusts to preserve the aspect ratio of the image.
To view images at their full resolution, use MATLAB desktop or set the
of the output
Image object to
details about the
MaxRenderedResolution property, see Image Properties.