Importing Images
To import data into the MATLAB® workspace from a graphics file, use the imread
function.
Using this function, you can import data from files in many standard file formats, including
the Tagged Image File Format (TIFF), Graphics Interchange Format (GIF), Joint Photographic
Experts Group (JPEG), and Portable Network Graphics (PNG) formats. For a complete list of
supported formats, see the imread
reference page.
For example, read the image data stored in a file in JPEG format into the MATLAB workspace as the array I
.
I = imread("ngc6543a.jpg");
imread
represents the image in the workspace as a multidimensional array
of class uint8
. The dimensions of the array depend on the format of the
data. For example, imread
uses three dimensions to represent RGB color
images.
whos I
Name Size Bytes Class Attributes I 650x600x3 1170000 uint8
For more control over reading TIFF files, use the Tiff
object. For more
information, see Read Image Data and Metadata from TIFF Files.
Get Information About Image Files
If you have a file in a standard graphics format, use the imfinfo
function to get information about its contents. The
imfinfo
function returns a structure containing information about the
file. The fields in the structure vary with the file format, but
imfinfo
always returns some basic information including the filename,
last modification date, file size, and format.
For example, get information about a file in Joint Photographic Experts Group (JPEG) format.
info = imfinfo("ngc6543a.jpg")
info = Filename: 'current_directory\ngc6543a.jpg' FileModDate: '01-Oct-1996 16:19:44' FileSize: 27387 Format: 'jpg' FormatVersion: '' Width: 600 Height: 650 BitDepth: 24 ColorType: 'truecolor' FormatSignature: '' NumberOfSamples: 3 CodingMethod: 'Huffman' CodingProcess: 'Sequential' Comment: {'CREATOR: XV Version 3.00b Rev: 6/15/94 Quality = 75, Smoothing = 0↵'}
Read Image Data and Metadata from TIFF Files
While you can use imread
to import image data and metadata from TIFF
files, the function does have some limitations. For example, a TIFF file can contain
multiple images, and each image can have multiple subimages. While you can read all the
images from a multi-image TIFF file with imread
, you cannot access the
subimages. Using the Tiff
object, you can read image data, metadata, and
subimages from a TIFF file. When you construct a Tiff
object, it represents
your connection with a TIFF file and provides access to many of the routines in the LibTIFF
library.
This example provides a step-by-step demonstration of how to use Tiff
object
methods and properties to read subimages from a TIFF file. To get the most out of the
Tiff
object, you must be familiar with the TIFF specification and
technical notes. View this documentation at LibTIFF - TIFF Library and Utilities.
Read Subimages from a TIFF File
A TIFF file can contain one or more image file directories (IFD). Each IFD contains image data and the metadata (tags) associated with the image. Each IFD can contain one or more subIFDs, which also can contain image data and metadata. These subimages are typically reduced-resolution (thumbnail) versions of the image data in the IFD containing the subIFDs.
To read the subimages in an IFD, you must get the location of the subimage from the SubIFD
tag. The SubIFD
tag contains an array of byte offsets that point to the subimages. You then can pass the address of the subIFD to the setSubDirectory
method to make the subIFD the current IFD. Most Tiff
object methods operate on the current IFD.
Open a TIFF file that contains images and subimages using the Tiff
object constructor. This example uses the TIFF file created in Create TIFF Subdirectories, which contains one IFD with two subIFDs. The Tiff
constructor opens the TIFF file, and makes the first subIFD in the file the current IFD.
t = Tiff("my_subimage_file.tif","r");
Retrieve the locations of subIFDs associated with the current IFD. Use the getTag
method to get the value of the SubIFD
tag. This method returns an array of byte offsets that specify the location of subIFDs.
offsets = getTag(t,"SubIFD");
Navigate to the first subimage.
First, set the current IFD to the directory containing the first subimage.
dirNum = 1; setDirectory(t,dirNum)
Then, navigate to the first subIFD using the setSubDirectory
method. Specify the byte offset of the subIFD as an argument. This call makes the subIFD the current IFD.
setSubDirectory(t,offsets(1))
Read the image data from the current IFD (the first subIFD) the same way you read any other IFD in the file.
subimage_one = read(t);
View the first subimage.
imagesc(subimage_one)
Navigate to the second subimage.
First, reset the current IFD to the directory containing the second subimage.
setDirectory(t,dirNum)
Then, navigate to the second subIFD using the setSubDirectory
method. Specify the byte offset of the second subIFD.
setSubDirectory(t,offsets(2))
Read the image data from the current IFD (the second subIFD) the same way you would read any other IFD in the file.
subimage_two = read(t);
View the second subimage.
imagesc(subimage_two)
Close the Tiff object.
close(t)