imread - Read image from graphics file - MATLAB (original) (raw)

Read image from graphics file

Syntax

Description

[A](#btnczv9-1-A) = imread([filename](#btnczv9-1%5Fsep%5Fmw%5F3c509488-6925-4904-a5a7-4b420a010c3e)) reads the image from the file specified byfilename, inferring the format of the file from its contents. If filename is a multi-image file, then imread reads the first image in the file.

example

[A](#btnczv9-1-A) = imread([filename](#btnczv9-1%5Fsep%5Fmw%5F3c509488-6925-4904-a5a7-4b420a010c3e),[fmt](#btnczv9-1%5Fsep%5Fmw%5Ffadddc0e-e6d8-4592-b3d2-2de376dd7bcd)) additionally specifies the format of the file with the standard file extension indicated by fmt. Ifimread cannot find a file with the name specified by filename, it looks for a file named_filename.fmt_.

[A](#btnczv9-1-A) = imread(___,[idx](#btnczv9-1-idx)) reads the specified image or images from a multi-image file. This syntax applies only to CUR, GIF, HDF4, ICO, PBM, PGM, PPM, SVS, and TIFF files. You must specify a filename input, and you can optionally specify fmt.

[A](#btnczv9-1-A) = imread(___,[Name=Value](#namevaluepairarguments)) specifies format-specific options using one or more name-value arguments in addition to any of the input argument combinations in the previous syntaxes. For example, to orient a JPEG image automatically, set AutoOrient to true (since R2024b).

example

[[A](#btnczv9-1-A),[map](#btnczv9-1-map)] = imread(___) reads the indexed image infilename into A and reads the associated colormap of the image intomap. Colormap values in the image file are automatically rescaled into the range [0, 1]. This syntax does not apply to HEIF and HEIC image files as colormap is not supported by these file formats.

example

[[A](#btnczv9-1-A),[map](#btnczv9-1-map),[transparency](#btnczv9-1-transparency)] = imread(___) additionally returns the image transparency. This syntax applies only to PNG, CUR, and ICO files. For PNG files, transparency is the alpha channel, if one is present. For CUR and ICO files,transparency is the AND (opacity) mask.

example

Examples

collapse all

Read a sample image.

A = imread("ngc6543a.jpg");

The imread function returns a 650-by-600-by-3 array of type uint8.

Display the image.

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

Read the first image in a sample indexed image file.

[A,map] = imread("corn.tif"); whos A map

Name Size Bytes Class Attributes

A 415x312 129480 uint8
map 256x3 6144 double

The indexed image A is a 415-by-312 matrix of type uint8, and the colormap map is a 256-by-3 matrix of type double. The dimensions of map indicate that the indexed image contains up to 256 colors.

Display the image.

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

Convert the indexed image to an RGB image. The result is a 415-by-312-by-3 array of type double.

Check that the values of the RGB image are in the range [0, 1].

[minVal,maxVal] = bounds(RGB(:))

Read and display the third image in a sample file.

[A,map] = imread("corn.tif",3); imshow(A,map)

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

Return the alpha channel of a sample image.

[A,map,alpha] = imread("peppers.png"); whos alpha

Name Size Bytes Class Attributes

alpha 0x0 0 double

No alpha channel is present, so alpha is empty.

Read a specific region of pixels of a sample image.

Specify the PixelRegion name-value argument with a cell array of vectors indicating the boundaries of the region to read. The first vector describes the range of rows to read, and the second vector describes the range of columns to read.

[A,map] = imread("corn.tif",PixelRegion={[201 400] [151 250]});

The imread function reads the image data in rows 201–400 and columns 151–250 from corn.tif and returns the 200-by-100 array A.

Display the image.

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

Since R2024b

Some image files contain orientation metadata in an exchangeable image file format (Exif) Orientation tag. When reading the image file with imread, you can orient the image data automatically according to this orientation tag by specifying the AutoOrient name-value argument as true.

Make a tiling of eight versions of the same image with different values in their Exif Orientation tags. The file clock_ n.jpg has a value of n in its Exif Orientation tag. If you do not specify the AutoOrient name-value argument, then the images are read without regard to their respective Exif Orientation tag values.

filenames = "clock_" + string(1:8) + ".jpg";

for i = 1:8 rawImages{i} = imread(filenames(i)); end

imshow(imtile(rawImages,BorderSize=[25 25],GridSize=[2 4]))

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

Use the AutoOrient name-value argument to transform each image according to its respective Exif Orientation tag value before reading the image data into the workspace. View the transformed images.

for i = 1:8 orientedImages{i} = imread(filenames(i),AutoOrient=true); end imshow(imtile(orientedImages,BorderSize=[25 25],GridSize=[2 4]))

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

Input Arguments

collapse all

Name of the graphics file, specified as a string scalar or character vector.

Depending on the location of your file, filename can take one of these forms.

Location Form
Current folder or folder on the MATLAB® path Specify the name of the file infilename.Example: "myImage.jpg"
File in a folder If the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name.Example: "C:\myFolder\myImage.png"Example: "\imgDir\myImage.bmp"
Uniform resource locator (URL) If the file is located by an internet URL, thenfilename must contain the protocol type, such as http://.Example: "http://my\_hostname/my\_path/my\_image.jpg"
Remote location If the file is stored at a remote location, thenfilename must contain the full path of the file specified as a URL of the form:scheme_name://path_to_file/_my_file.ext_Based on the remote location,scheme_name can be one of the values in this table. Remote Location_scheme_name_Amazon S3™s3Windows Azure® Blob Storagewasb, wasbsHDFS™hdfsFor more information, see Work with Remote Data.Example: "s3://my_bucket/my_path/my_image.heif"

Image format, specified as a string scalar or character vector indicating the standard file extension. Call imformats to see a list of supported formats and their file extensions.

Example: "png"

Image to read, specified as an integer scalar or, for GIF files, a vector of integers. For example, if idx is 3, then the imread function reads the third image in the file. For a GIF file, ifidx is 1:5, then the imread function reads only the first five frames. The idx argument is supported only for multi-image GIF, CUR, ICO, and HDF4 files.

When reading multiple frames from the same GIF file, specifyidx as a vector of frames or specify the Frames name-value argument as"all". Because of the way that GIF files are structured, these syntaxes provide faster performance compared to calling imread in a loop.

Note

For HDF4 files, idx corresponds to the reference number of the image to read. Reference numbers do not necessarily correspond to the order of the images in the file. You can use imfinfo to match image order with reference number.

Example: 2

Example: 6:10

Data Types: double

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=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: imread("myImage.tif",Index=5) reads the fifth image of a TIFF file.

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

Example: imread("myImage.tif","Index",5) reads the fifth image of a TIFF file.

GIF Files

expand all

Frames to read, specified as a positive integer, a vector of integers, or "all". For example, if you specify the value 3, thenimread reads the third frame in the file. If you specify "all", thenimread reads all frames and returns them in the order in which they appear in the file.

Example: 5

Example: 1:10

Example: "all"

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

JPEG Files

expand all

Since R2024b

Automatically orient the image, specified asfalse or true. Specify AutoOrient astrue to transform the data infilename according to the ExifOrientation tag in the image file. If you specify AutoOrient asfalse, theimread function ignores the Exif Orientation tag.

Data Types: logical

JPEG 2000 Files

expand all

Boundary of region to read, specified as a cell array of the form{_`rows`_,_`cols`_}. The _`rows`_ value describes the range of rows to read, and the_`cols`_ value describes the range of columns to read. Both_`rows`_ and _`cols`_ must be two-element vectors containing 1-based indices. For example, PixelRegion={[1 2] [3 4]} reads the region bounded by rows 1 and 2 and by columns 3 and 4 of the image data.

Note

If you specify ReductionLevel as a positive value, then specifyPixelRegion in reference to the image after the reduction of resolution.

Example: {[1 100] [4 500]}

Reduction of the image resolution, specified as a nonnegative integer. If you specifyReductionLevel as value_`L`_, then the image resolution is reduced by a factor of2_`L`_. The reduction level is limited by the total number of decomposition levels, as specified by theWaveletDecompositionLevels field in the output of the imfinfo function.

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

Compatibility with MATLAB 7.9 (R2009b) and earlier, specified asfalse or true. If you specify V79Compatible astrue, then the returned grayscale or RGB image is consistent with previous versions ofimread (MATLAB 7.9 (R2009b) and earlier).

Data Types: logical

PNG Files

expand all

Background color, specified as "none", a positive integer, a number in the range [0, 1], or a three-element vector of numbers in the range [0, 1]. If you specify BackgroundColor as"none", then theimread function does not perform any compositing. Otherwise, theimread function blends transparent pixels with the background color.

The default value for BackgroundColor depends on the presence of thetransparency output argument and the image type:

Example: 2

Example: 0.5

Example: [0.2 0.8 0.5]

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

TIFF Files

expand all

Since R2024b

Automatically orient the image, specified asfalse or true. Specify AutoOrient astrue to transform the data infilename according to the ExifOrientation tag in the image file. If you specify AutoOrient asfalse, theimread function ignores the Exif Orientation tag.

Note

If you specify [PixelRegion](imread.html#mw%5F7af663ea-4021-498f-8764-38005b96e4c1) in addition to specifyingAutoOrient astrue, then theimread function first reads the specified region and then transforms the region according to the Exif Orientation tag in the file.

Data Types: logical

Image to read, specified as a positive integer. For example, if the value of Index is3, then theimread function reads the third image in the file.

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

Information about the image, specified as a structure array returned by the imfinfo function. Use the Info name-value argument to help imread locate the images in a multi-image TIFF file more quickly.

Data Types: struct

Boundary of region to read, specified as a cell array of the form{_`rows`_,_`cols`_}. The _`rows`_ value describes the range of rows to read, and the_`cols`_ value describes the range of columns to read. Both_`rows`_ and _`cols`_ must be two- or three-element vectors containing 1-based indices.

Example: {[1 100] [4 500]}

Example: {[100 5 200] [250 2 500]}

HIEF and HEIC Files

Since R2025a

expand all

Option to automatically orient the image, specified as a numeric or logical 1 (true) or0 (false). SpecifyAutoOrient astrue to transform the data infilename according to the ExifOrientation tag in the image file. If you specify AutoOrient asfalse, theimread function ignores the Exif Orientation tag.

Data Types: logical

Output Arguments

collapse all

Image data, returned as an array. If the image data has_`m`_ rows and _`n`_ columns, then:

The class of A depends on the image format and the bit depth of the image data. For more information, seeAlgorithms.

Colormap associated with the indexed image data inA, returned as a three-column matrix of class double.

Transparency information, returned as a matrix.

More About

collapse all

Bit depth is the number of bits used to represent each image pixel.

Bit depth is calculated by multiplying the bits-per-sample with the samples-per-pixel. Thus, a format that uses 8 bits for each color component (or sample) and three samples per pixel has a bit depth of 24. Sometimes the sample size associated with a bit depth can be ambiguous. For example, a 48-bit bit depth can represent six 8-bit samples, four 12-bit samples, or three 16-bit samples. See Algorithms for sample size information to avoid this ambiguity.

Tips

Algorithms

collapse all

For most image file formats, imread uses 8 or fewer bits per color plane to store image pixels. This table lists the data type of the returned image array, A, for the bit depths used by the file formats.

Bit Depth in File Class of Array Returned byimread
1 bit per pixel logical
2 to 8 bits per color plane uint8
9 to 16 bits per pixel uint16 (BMP, JPEG, PNG, and TIFF)For the 16-bit BMP packed format (5-6-5), MATLAB returnsuint8.

These sections provide information about the support for specific formats, listed in alphabetical order by format name.

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths No Compression RLE Compression Output Class Notes
1 bit logical
4 bit or 8 bit uint8
16 bit uint8 1 sample/pixel
24 bit uint8 3 samples/pixel
32 bit uint8 3 samples/pixel (1 byte padding)

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths No Compression Compression Output Class
1 bit logical
4 bit uint8
8 bit uint8

Note

By default, Microsoft® Windows® cursors are 32-by-32 pixels. Because MATLAB pointers must be 16-by-16, you might need to scale your image. You can use the imresize function for this operation.

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths No Compression Compression Output Class
1 bit logical
2 bit to 8 bit uint8

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raster Image with Colormap Raster Image Without Colormap Output Class Notes
8 bit uint8
24 bit uint8 3 samples/pixel

This table lists the bit depths, compression, and data types supported by the function for HEIF and HEIC files. (since R2025a)

Supported Bit Depths No Compression Compression Output Class Notes
8 bit uint8 RGB
10 bit uint8 RGB
12 bit uint8 RGB

The imread function reads baseline JPEG images, as well as JPEG images with some commonly used extensions. For information on JPEG 2000 file support, see JPEG 2000 — Joint Photographic Experts Group 2000. This table lists supported bit depths and the data type of the output image data array.

Supported Bits per Sample Lossy Compression Lossless Compression Output Class Notes
8 bit uint8 Grayscale or RGB
12 bit uint16 Grayscale or RGB
16 bit uint16 Grayscale

For information about JPEG files, see JPEG — Joint Photographic Experts Group. This table lists supported bit depths and the data type of the output image data array.

Supported Bits per Sample Lossy Compression Lossless Compression Output Class Notes
1 bit logical Grayscale only
2 bit to 8 bit uint8 orint8 Grayscale or RGB
9 bit to 16 bit uint16 orint16 Grayscale or RGB

Note

Indexed JPEG 2000 images are not supported. Only JP2 compatible color spaces are supported for JP2/JPX files. By default, theimread function returns all image channels in the order they are stored in the file.

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raw Binary ASCII (Plain) Encoded Output Class
1 bit logical

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Output Class Notes
1 bit logical Grayscale
8 bit uint8 Grayscale or indexed
24 bit uint8 RGB, three 8-bit samples/pixel

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raw Binary ASCII (Plain) Encoded Output Class Notes
8 bit uint8
16 bit uint16
Arbitrary 1- to 8-bit:uint89- to 16-bit:uint16 Values are scaled.

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Output Class Notes
1 bit logical Grayscale
2 bit or 4 bit uint8 Grayscale
8 bit uint8 Grayscale or indexed
16 bit uint16 Grayscale or indexed
24 bit uint8 RGB, three 8-bit samples/pixel
48 bit uint16 RGB, three 16-bit samples/pixel

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raw Binary ASCII (Plain) Encoded Output Class
Up to 16 bit uint8
Arbitrary uint8,uint16, ordouble

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Output Class Notes
1 bit logical Bitmap
8 bit uint8 Indexed
24 bit uint8 RGB, three 8-bit samples/pixel
32 bit uint8 RGB with Alpha, four 8-bit samples/pixel

TIFF-based image file format. The imread function supports reading uncompressed and compressed images, including images with JPEG 2000 compression. For more information, see TIFF — Tagged Image File Format.

The imread function reads most images supported by the TIFF specification or LibTIFF, and supports these TIFF capabilities:

The imread function reads and converts TIFF images as follows:

This table lists the supported bit depths, compression, and the data type of the output image data array.

Supported Bit Depths ZPixmaps XYBitmaps XYPixmaps Output Class
1 bit logical
8 bit uint8

Extended Capabilities

expand all

Usage notes and limitations:

Usage notes and limitations:

Version History

Introduced before R2006a

expand all

You can now use imread to get information about HEIF and HEIC image files.

This functionality requires MATLAB Support for HEIF/HEIC Image Format, which is available only on MATLAB desktop environment. However, you cannot use HEIF and HEIC image files in a standalone application workflow.

To install the support package:

Apply Exif Orientation tag values to JPEG and TIFF images by using the AutoOrient name-value argument.

Using MATLAB Coder, you can generate code that uses theimread function with 8-bit JPEG images and then use the generated code on embedded targets.

Pixel value differences might exist between JPEG 2000 images in R2021b and previous releases of MATLAB.

When you read a GIF file without specifying additional arguments, theimread function reads only the first frame by default. Previously, imread read all the frames in the file by default.

You can read Aperio SVS microscopy image files and TIFF image files with JPEG 2000 compression.

You can read specified images from multi-image PGM, PBM, or PPM files.