Working with Images in MATLAB Graphics
What Is Image Data?
The basic MATLAB data structure is the array, an ordered set of real or complex elements. An array is naturally suited to the representation of images, real-valued, ordered sets of color or intensity data. (An array is suited for complex-valued images.)
In the MATLAB workspace, most images are represented as two-dimensional arrays (matrices), in which each element of the matrix corresponds to a single pixel in the displayed image. For example, an image composed of 200 rows and 300 columns of different colored dots stored as a 200-by-300 matrix. Some images, such as RGB, require a three-dimensional array, where the first plane in the third dimension represents the red pixel intensities, the second plane represents the green pixel intensities, and the third plane represents the blue pixel intensities.
This convention makes working with graphics file format images similar to working with any other type of matrix data. For example, you can select a single pixel from an image matrix using normal matrix subscripting:
This command returns the value of the pixel at row 2, column 15 of the image I.
The following sections describe the different data and image types, and give details about how to read, write, work with, and display graphics images; how to alter the display properties and aspect ratio of an image during display; how to print an image; and how to convert the data type or graphics format of an image.
MATLAB math supports three different numeric classes for image display:
- double-precision floating-point (double)
- 16-bit unsigned integer (uint16)
- 8-bit unsigned integer (uint8)
The image display commands interpret data values differently depending on the numeric class the data is stored in. Working with 8-Bit and 16-Bit Images includes details on the inner workings of the storage for 8- and 16-bit images.
By default, most data occupy arrays of class double. The data in these arrays is stored as double-precision (64-bit) floating-point numbers. All MATLAB functions and capabilities work with these arrays.
For images stored in one of the graphics file formats supported by MATLAB functions, however, this data representation is not always ideal. The number of pixels in such an image can be very large; for example, a 1000-by-1000 image has a million pixels. Since at least one array element represents each pixel , this image requires about 8 megabytes of memory if it is stored as class double.
To reduce memory requirements, you can store image data in arrays of class uint8 and uint16. The data in these arrays is stored as 8-bit or 16-bit unsigned integers. These arrays require one-eighth or one-fourth as much memory as data in double arrays.
MATLAB input functions read the most commonly used bit depths (bits per pixel) of any of the supported graphics file formats. When the data is in memory, it can be stored as uint8, uint16, or double. For details on which bit depths are appropriate for each supported format, see imread and imwrite.
Supported Image Formats
MATLAB commands read, write, and display several types of graphics file formats for images. As with MATLAB generated images, once a graphics file format image is displayed, it becomes a Handle Graphics image object. MATLAB supports the following graphics file formats, along with others:
- BMP (Microsoft Windows Bitmap)
- GIF (Graphics Interchange Files)
- HDF (Hierarchical Data Format)
- JPEG (Joint Photographic Experts Group)
- PCX (Paintbrush)
- PNG (Portable Network Graphics)
- TIFF (Tagged Image File Format)
- XWD (X Window Dump)
For more information about the bit depths and image types supported for these formats, see imread and imwrite.
Functions for Reading, Writing, and Displaying Images
Images are essentially two-dimensional matrices, so many MATLAB functions can operate on and display images. The following table lists the most useful ones. The sections that follow describe these functions in more detail.
|axis||Plot axis scaling and appearance.||Display|
|image||Display image (create image object).||Display|
|imagesc||Scale data and display as image.||Display|
|imread||Read image from graphics file.||File I/O|
|imwrite||Write image to graphics file.||File I/O|
|imfinfo||Get image information from graphics file.||Utility|
|ind2rgb||Convert indexed image to RGB image.||Utility|