Reading Image Data

To import an image from any supported graphics image file format, in any of the supported bit depths, use the imread function. This example reads a truecolor image into the MATLAB® workspace as the variable RGB.

RGB = imread('football.jpg');

If the image file format uses 8-bit pixels, imread stores the data in the workspace as a uint8 array. For file formats that support 16-bit data, such as PNG and TIFF, imread creates a uint16 array.

imread uses two variables to store an indexed image in the workspace: one for the image and another for its associated colormap. imread always reads the colormap into a matrix of class double, even though the image array itself may be of class uint8 or uint16.

[X,map] = imread('trees.tif');

In these examples, imread infers the file format to use from the contents of the file. You can also specify the file format as an argument to imread. imread supports many common graphics file formats, such as Microsoft® Windows® Bitmap (BMP), Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPEG), Portable Network Graphics (PNG), and Tagged Image File Format (TIFF) formats. For the latest information concerning the bit depths and/or image formats supported, see imread and imformats.

If the graphics file contains multiple images, imread imports only the first image from the file. To import additional images, you must use imread with format-specific arguments to specify the image you want to import. In this example, imread imports a series of 27 images from a TIFF file and stores the images in a four-dimensional array. You can use imfinfo to determine how many images are stored in the file.

mri = zeros([128 128 1 27],'uint8'); % preallocate 4-D array 

for frame=1:27
	[mri(:,:,:,frame),map] = imread('mri.tif',frame);
end

When a file contains multiple images that are related in some way, you can call image processing algorithms directly. For more information, see What is an Image Sequence?.

If you are working with a large file, you may want to try block processing to reduce memory usage. For more information, see Neighborhood or Block Processing: An Overview.

Was this topic helpful?