Image Processing Toolbox | ![]() ![]() |
Read image from graphics files
Syntax
A = imread(filename,fmt
) [X,map] = imread(filename,fmt
) [...] = imread(filename) [...] = imread(...,idx) (CUR, ICO, and TIFF only) [...] = imread(...,ref) (HDF only) [...] = imread(...,'BackgroundColor',BG) (PNG only) [A,map,alpha] = imread(...) (PNG only)
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
.
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,
[A,map] = imread(filename); A = imread(filename);
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,
[...] = imread(...,'BackgroundColor',bg);
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,
[...] = imread(...,'Back','none');
Case 3. You ask to get the alpha channel as an output variable. For example,
[A,map,alpha] = imread(filename); [A,map,alpha] = imread(filename,fmt);
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.
Note
By default, Microsoft Windows cursors are 32-by-32 pixels. MATLAB
pointers must be 16-by-16. You will probably need to scale your image. If you
have the Image Processing Toolbox, you can use the imresize function.
|
Format Support
This table summarizes the types of images that imread
can read.
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.
[X,map] = imread('flowers.tif',6);
This example reads the fourth image in an HDF file.
info = imfinfo('skull.hdf'); [X,map] = imread('skull.hdf',info(4).Reference);
This example reads a 24-bit PNG image and sets any of its fully transparent (alpha channel) pixels to red.
bg = [255 0 0]; A = imread('image.png','BackgroundColor',bg);
This example returns the alpha channel (if any) of a PNG image.
[A,map,alpha] = imread('image.png');
This example reads an ICO image, applies a transparency mask, and then displays the image.
[a,b,c] = imread('myicon.ico'); % Augment colormap for background color (white). b2 = [b; 1 1 1]; % Create new image for display. d = ones(size(a)) * (length(b2) - 1); % Use the AND mask to mix the background and % foreground data on the new image d(c == 0) = a(c == 0); % Display new image imshow(uint8(d), b2)
See Also
double
, fread
, imfinfo
, imwrite
, uint8
, uint16
![]() | improfile | imresize | ![]() |