Image Processing Toolbox    
imread

Read image from graphics files

Syntax

Description

A = imread(filename,fmt) reads a grayscale or truecolor image named filename into A. If the file contains a grayscale intensity image, A is a two-dimensional array. If the file contains a truecolor (RGB) image, A is a three-dimensional (m-by-n-by-3) array.

[X,map] = imread(filename,fmt) reads the indexed image in filename into X and its associated colormap into map. The colormap values are rescaled to the range [0,1]. A and map are two-dimensional arrays.

[...] = imread(filename) attempts to infer the format of the file from its content.

filename is a string that specifies the name of the graphics file, and fmt is a string that specifies the format of the file. If the file is not in the current directory or in a directory in the MATLAB path, specify the full pathname for a location on your system. If imread cannot find a file named filename, it looks for a file named filename.fmt. If you do not specify a string for fmt, the toolbox will try to discern the format of the file by checking the file header.

This table lists the possible values for fmt.

Format
File Type
'bmp'
Windows Bitmap (BMP)
'cur'
Windows Cursor resources (CUR)
'hdf'
Hierarchical Data Format (HDF)
'ico'
Windows Icon resources (ICO)
'jpg' or 'jpeg'
Joint Photographic Experts Group (JPEG)
'pcx'
Windows Paintbrush (PCX)
`png'
Portable Network Graphics (PNG)
'tif' or 'tiff'
Tagged Image File Format (TIFF)
'xwd'
X Windows Dump (XWD)

Special Case Syntax:

TIFF-Specific Syntax

[...] = imread(...,idx) reads in one image from a multi-image TIFF file. idx is an integer value that specifies the order in which the image appears in the file. For example, if idx is 3, imread reads the third image in the file. If you omit this argument, imread reads the first image in the file.

PNG-Specific Syntax

The discussion in this section is only relevant to PNG files that contain transparent pixels. A PNG file does not necessarily contain transparency data. Transparent pixels, when they exist, will be identified by one of two components: a transparency chunk or an alpha channel. (A PNG file can only have one of these components, not both.)

The transparency chunk identifies which pixel values will be treated as transparent, e.g., if the value in the transparency chunk of an 8-bit image is 0.5020, all pixels in the image with the color 0.5020 can be displayed as transparent. An alpha channel is an array with the same number of pixels as are in the image, which indicates the transparency status of each corresponding pixel in the image (transparent or nontransparent).

Another potential PNG component related to transparency is the background color chunk, which (if present) defines a color value that can be used behind all transparent pixels. This section identifies the default behavior of the toolbox for reading PNG images that contain either a transparency chunk or an alpha channel, and describes how you can override it.

Case 1. You do not ask to output the alpha channel and do not specify a background color to use. For example,

If the PNG file contains a background color chunk, the transparent pixels will be composited against the specified background color.

If the PNG file does not contain a background color chunk, the transparent pixels will be composited against 0 for grayscale (black), 1 for indexed (first color in map), or [0 0 0] for RGB (black).

Case 2. You do not ask to output the alpha channel but you specify the background color parameter in your call. For example,

The transparent pixels will be composited against the specified color. The form of bg depends on whether the file contains an indexed, intensity (grayscale), or RGB image. If the input image is indexed, bg should be an integer in the range [1,P] where P is the colormap length. If the input image is intensity, bg should be an integer in the range [0,1]. If the input image is RGB, bg should be a three-element vector whose values are in the range [0,1].

There is one exception to the toolbox's behavior of using your background color. If you set background to 'none' no compositing will be performed. For example,

Case 3. You ask to get the alpha channel as an output variable. For example,

No compositing is performed; the alpha channel will be stored separately from the image (not merged into the image as in cases 1 and 2). This form of imread returns the alpha channel if one is present, and also returns the image and any associated colormap. If there is no alpha channel, alpha returns []. If there is no colormap, or the image is grayscale or truecolor, map may be empty.

HDF-Specific Syntax

[...] = imread(...,ref) reads in one image from a multi-image HDF file. ref is an integer value that specifies the reference number used to identify the image. For example, if ref is 12, imread reads the image whose reference number is 12. (Note that in an HDF file the reference numbers do not necessarily correspond to the order of the images in the file. You can use imfinfo to match up image order with reference number.) If you omit this argument, imread reads the first image in the file.

CUR- and ICO-Specific Syntax

[...] = imread(...,idx) reads in one image from a multi-image icon or cursor file. idx is an integer value that specifies the order that the image appears in the file. For example, if idx is 3, imread reads the third image in the file. If you omit this argument, imread reads the first image in the file.

[A,map,alpha] = imread(...) returns the AND mask for the resource, which can be used to determine the transparency information. For cursor files, this mask may contain the only useful data.

Format Support

This table summarizes the types of images that imread can read.

Format
Variants
BMP
1-bit, 4-bit, 8-bit, and 24-bit uncompressed images; 4-bit and 8-bit run-length encoded (RLE) images
CUR
1-bit, 4-bit, and 8-bit uncompressed images
HDF
8-bit raster image datasets, with or without associated colormap; 24-bit raster image datasets
ICO
1-bit, 4-bit, and 8-bit uncompressed images
JPEG
Any baseline JPEG image (8 or 24-bit); JPEG images with some commonly used extensions
PCX
1-bit, 8-bit, and 24-bit images
PNG
Any PNG image, including 1-bit, 2-bit, 4-bit, 8-bit, and 16-bit grayscale images; 8-bit and 16-bit indexed images; 24-bit and 48-bit RGB images
TIFF
Any baseline TIFF image, including 1-bit, 8-bit, and 24-bit uncompressed images; 1-bit, 8-bit, 16-bit, and 24-bit images with packbits compression; 1-bit images with CCITT compression; also 16-bit grayscale, 16-bit indexed, and 48-bit RGB images.
XWD
1-bit and 8-bit ZPixmaps; XYBitmaps; 1-bit XYPixmaps

Class Support

In most of the image file formats supported by imread, pixels are stored using eight or fewer bits per color plane. When reading such a file, the class of the output (A or X) is uint8. imread also supports reading 16-bit-per-pixel data from TIFF and PNG files; for such image files, the class of the output (A or X) is uint16. Note that for indexed images, imread always reads the colormap into an array of class double, even though the image array itself may be of class uint8 or uint16.

Remarks

imread is a function in MATLAB.

Examples

This example reads the sixth image in a TIFF file.

This example reads the fourth image in an HDF file.

This example reads a 24-bit PNG image and sets any of its fully transparent (alpha channel) pixels to red.

This example returns the alpha channel (if any) of a PNG image.

This example reads an ICO image, applies a transparency mask, and then displays the image.

See Also

double, fread, imfinfo, imwrite, uint8, uint16


 improfile imresize