Read, Write, and Query Image Files
Working with Image Formats
In its native form, a graphics file format image is not stored
as a MATLAB® matrix, or even necessarily as a matrix. Most graphics
files begin with a header containing format-specific information tags,
and continue with bitmap data that can be read as a continuous stream.
For this reason, you cannot use the standard MATLAB I/O commands load
and save
to
read and write a graphics file format image.
Call special MATLAB functions to read and write image data from graphics file formats:
To read a graphics file format image use
imread
.To write a graphics file format image, use
imwrite
.To obtain information about the nature of a graphics file format image, use
imfinfo
.
This table gives a clearer picture of which MATLAB commands should be used with which image types.
Reading a Graphics Image
The imread
function reads
an image from any supported graphics image file in any of the supported
bit depths. Most of the images that you read are 8-bit. When these
are read into memory, they are stored as class uint8
.
The main exception to this rule is MATLAB support for 16-bit
data for PNG and TIFF images; if you read a 16-bit PNG or TIFF image,
it is stored as class uint16
.
Note
For indexed images, imread
always reads the
colormap into an array of class double
, even though
the image array itself can be of class uint8
or uint16
.
The following commands read the image ngc6543a.jpg
into
the workspace variable RGB
and then displays the
image using the image
function:
RGB = imread('ngc6543a.jpg'); image(RGB)
You can write (save) image data using the imwrite
function.
The statements
load clown % An image that is included with MATLAB
imwrite(X,map,'clown.bmp')
create a BMP file containing the clown image.
Writing a Graphics Image
When you save an image using imwrite
,
the default behavior is to automatically reduce the bit depth to uint8
.
Many of the images used in MATLAB are 8-bit, and most graphics
file format images do not require double-precision data. One exception
to the rule for saving the image data as uint8
is
that PNG and TIFF images can be saved as uint16
.
Because these two formats support 16-bit data, you can override the MATLAB default
behavior by specifying uint16
as the data type
for imwrite
. The following example shows writing
a 16-bit PNG file using imwrite
.
imwrite(I,'clown.png','BitDepth',16);
Subsetting a Graphics Image (Cropping)
Sometimes you want to work with only a portion of an image file or you want to break it up into subsections. Specify the intrinsic coordinates of the rectangular subsection you want to work with and save it to a file from the command line. If you do not know the coordinates of the corner points of the subsection, choose them interactively, as the following example shows:
% Read RGB image from graphics file. im = imread('street2.jpg'); % Display image with true aspect ratio image(im); axis image % Use ginput to select corner points of a rectangular % region by pointing and clicking the mouse twice p = ginput(2); % Get the x and y corner coordinates as integers sp(1) = min(floor(p(1)), floor(p(2))); %xmin sp(2) = min(floor(p(3)), floor(p(4))); %ymin sp(3) = max(ceil(p(1)), ceil(p(2))); %xmax sp(4) = max(ceil(p(3)), ceil(p(4))); %ymax % Index into the original image to create the new image MM = im(sp(2):sp(4), sp(1): sp(3),:); % Display the subsetted image with appropriate axis ratio figure; image(MM); axis image % Write image to graphics file. imwrite(MM,'street2_cropped.tif')
If you know what the image corner coordinates should be, you can manually define
sp
in the preceding example rather than using
ginput
.
You can also display a “rubber band box” as you interact with the image to
subset it. See the code example for rbbox
for details. For further
information, see the documentation for the ginput
and image
functions.
Obtaining Information About Graphics Files
The imfinfo
function
enables you to obtain information about graphics files in any of the
standard formats listed earlier. The information you obtain depends
on the type of file, but it always includes at least the following: