Exporting to Images

To export data from the MATLAB® workspace using one of the
standard graphics file formats, use the imwrite function.
Using this function, you can export data in formats such as the Tagged
Image File Format (TIFF), Joint Photographic Experts Group (JPEG),
and Portable Network Graphics (PNG). For a complete list of supported
formats, see the imwrite reference
page.

The following example writes a multidimensional array of uint8 data I from
the MATLAB workspace into a file in TIFF format. The class of
the output image written to the file depends on the format specified.
For most formats, if the input array is of class uint8, imwrite outputs
the data as 8-bit values. See the imwrite reference
page for details.

Noteimwrite supports different syntaxes for several
of the standard formats. For example, with TIFF file format, you can
specify the type of compression MATLAB uses to store the image.
See the imwrite reference page
for details.

Exporting Image Data and Metadata to TIFF Files

While you can use imwrite to export image
data and metadata (tags) to Tagged Image File Format (TIFF) files,
the function does have some limitations. For example, when you want
to modify image data or metadata in the file, you must write the all
the data to the file. You cannot write only the updated portion. Using
the Tiff object, you can write portions of the
image data and modify or add individual tags to 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.

The following sections provide step-by-step examples of using Tiff object
methods and properties to perform some common tasks with TIFF files.
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

Creating a New TIFF File

Create some image data. This example reads image data
from a JPEG file included with MATLAB:

imgdata = imread('ngc6543a.jpg');

Create a new TIFF file by constructing a Tiff object,
specifying the name of the new file as an argument. To create a file
you must specify either write mode ('w') or append
mode ('a'):

t = Tiff('myfile.tif','w');

When you create a new TIFF file, the Tiff constructor
creates a file containing an image file directory (IFD). A TIFF file
uses this IFD to organize all the data and metadata associated with
a particular image. A TIFF file can contain multiple IFDs. The Tiff object
makes the IFD it creates the current IFD. Tiff object
methods operate on the current IFD. You can navigate among IFDs in
a TIFF file and specify which IFD is the current IFD using Tiff object
methods.

Set required TIFF tags using the setTag method
of the Tiff object. These required tags specify
information about the image, such as its length and width. To break
the image data into strips, specify a value for the RowsPerStrip tag.
To break the image data into tiles, specify values for the TileWidth and TileLength tags.
The example creates a structure that contains tag names and values
and passes that to setTag. You also can set each
tag individually.

For information about supported TIFF tags and how to set their
values, see Setting Tag Values.
For example, the Tiff object supports properties
that you can use to set the values of certain properties. This example
uses the Tiff object PlanarConfiguration property
to specify the correct value for the chunky configuration: Tiff.PlanarConfiguration.Chunky.

Write the image data and metadata to the current directory
using the write method of the Tiff object.

t.write(imgdata);

If you wanted to put multiple images into your file, call the writeDirectory method
right after performing this write operation. The writeDirectory method
sets up a new image file directory in the file and makes this new
directory the current directory.

Close your connection to the file by closing the Tiff object:

t.close();

Test that you created a valid TIFF file by using the imread function
to read the file, and then display the image:

imagesc(imread('myfile.tif'));

Writing a Strip or Tile of Image Data

Note:
You can only modify a strip or a tile of image data if the data
is not compressed.

Open an existing TIFF file for modification by creating
a Tiff object. This example uses the file created
in Creating a New TIFF File. The Tiff constructor
returns a handle to a Tiff object.

t = Tiff('myfile.tif','r+');

Generate some data to write to a strip in the image. This
example creates a three-dimensional array of zeros that is the size
of a strip. The code uses the number of rows in a strip, the width
of the image, and the number of samples per pixel as dimensions. The
array is an array of uint8 values.

If the image data had a tiled layout, you would use the TileWidth and TileLength tags
to specify the dimensions.

Write the data to a strip in the file using the writeEncodedStrip method.
Specify the index number that identifies the strip you want to modify.
The example picks strip 18 because it is easier to see the change
in the image.

t.writeEncodedStrip(18, stripData);

If the image had a tiled layout, you would use the writeEncodedTile method
to modify the tile.

Close your connection to the file by closing the Tiff object.

t.close();

Test that you modified a strip of the image in the TIFF
file by using the imread function to read the file,
and then display the image.

modified_imgdata = imread('myfile.tif');
imagesc(modified_imgdata)

Note the black strip across the middle of the image.

Modifying TIFF File Metadata (Tags)

Open an existing TIFF file for modification using the Tiff object.
This example uses the file created in Creating a New TIFF File. The Tiff constructor
returns a handle to a Tiff object.

t = Tiff('myfile.tif','r+');

Verify that the file does not contain the Artist tag,
using the getTag method. This code should issue
an error message saying that it was unable to retrieve the tag.

artist_value = t.getTag('Artist');

Add the Artist tag using the setTag method.

t.setTag('Artist','Pablo Picasso');

Write the new tag data to the TIFF file using the rewriteDirectory method.
Use the rewriteDirectory method when modifying
existing metadata in a file or adding new metadata to a file.

t.rewriteDirectory();

Close your connection to the file by closing the Tiff object.

t.close();

Test your work by reopening the TIFF file and getting
the value of the Artist tag, using the getTag method.

Create a new TIFF file by constructing a Tiff object
and specifying the name of the new file as an argument. To create
a file you must specify either write mode ('w')
or append mode ('a'). The Tiff constructor
returns a handle to a Tiff object.

t = Tiff('my_subimage_file.tif','w');

Set required TIFF tags using the setTag method
of the Tiff object. These required tags specify
information about the image, such as its length and width. To break
the image data into strips, specify a value for the RowsPerStrip tag.
To break the image data into tiles, use the TileWidth and TileLength tags.
The example creates a structure that contains tag names and values
and passes that to setTag. You can also set each
tag individually.

To create subdirectories, you must set the SubIFD tag,
specifying the number of subdirectories you want to create. Note that
the number you specify isn't the value of the SubIFD tag.
The number tells the Tiff software to create a SubIFD that
points to two subdirectories. The actual value of the SubIFD tag
will be the byte offsets of the two subdirectories.

For information about supported TIFF tags and how to set their
values, see Setting Tag Values.
For example, the Tiff object supports properties
that you can use to set the values of certain properties. This example
uses the Tiff object PlanarConfiguration property
to specify the correct value for the chunky configuration: Tiff.PlanarConfiguration.Chunky.

Write the image data and metadata to the current directory
using the write method of the Tiff object.

t.write(imgdata);

Set up the first subdirectory by calling the writeDirectory method.
The writeDirectory method sets up the subdirectory
and make the new directory the current directory. Because you specified
that you wanted to create two subdirectories, writeDirectory sets
up a subdirectory.

t.writeDirectory();

Set required tags, just as you did for the regular directory.
According to the LibTIFF API, a subdirectory cannot contain a SubIFD tag.

Write the image data and metadata to the subdirectory
using the write method of the Tiff object:

t.write(img_third);

Close your connection to the file by closing the Tiff object:

t.close();

Setting Tag Values

The following table lists all the TIFF tags that the Tiff object
supports and includes information about their MATLAB class and
size. For certain tags, the table also indicates the set of values
that the Tiff object supports, which is a subset
of all the possible values defined by the TIFF specification. You
can use Tiff object properties to specify the supported
values for these tags. For example, use Tiff.Compression.JPEG to
specify JPEG compression. See the Tiff class reference page for a
full list of properties.