Image Processing Toolbox User's Guide Previous page   Next Page

Writing a Graphics Image

The function imwrite writes an image to a graphics file in one of the supported formats. The most basic syntax for imwrite takes the image variable name and a filename. If you include an extension in the filename, MATLAB infers the desired file format from it. (For more information, see the reference entry for the imwrite function.)

This example loads the indexed image X from a MAT-file, clown.mat, that contains the data matrix and the associated colormap and then writes the image to a BMP file.

Specifying Additional Format-Specific Parameters

When using imwrite with some graphics formats, you can specify additional parameters. For example, with PNG files, you can specify the bit depth as an additional parameter. This example writes an grayscale image I to a 4-bit PNG file.

This example writes an image A to a JPEG file, using an additional parameter to specify the compression quality parameter.

For more information about the additional parameters associated with certain graphics formats, see the reference pages for imwrite.

Reading and Writing Binary Images in 1-Bit Format

In certain file formats, a binary image can be stored in a 1-bit format. If the file format supports it, MATLAB writes binary images as 1-bit images by default. When you read in a binary image in 1-bit format, MATLAB represents it in the workspace as a logical array.

This example reads in a binary image and writes it as a TIFF file. Because the TIFF format supports 1-bit images, the file is written to disk in 1-bit format.

To verify the bit depth of test.tif, call imfinfo and check the BitDepth field.

Determining the Storage Class of the Output File

imwrite uses the following rules to determine the storage class used in the output image.

Storage Class of Image
Storage Class of Output Image File
logical
If the output image file format specified supports 1-bit images, imwrite creates a 1-bit image file.
If the output image file format specified does not support 1-bit images, such as JPEG, imwrite converts the image to a class uint8 grayscale image.
uint8
If the output image file format specified supports unsigned 8-bit images, imwrite creates an unsigned 8-bit image file.
uint16
If the output image file format specified support unsigned 16-bit images (PNG or TIFF), imwrite creates an unsigned 16-bit image file.
If the output image file format specified does not support 16-bit images, imwrite scales the image data to class uint8 and creates an 8-bit image file.
int16
Partially supported; depends on file format.
single
Partially supported; depends on file format.
double
MATLAB scales the image data to uint8 and creates an 8-bit image file, because most image file formats use 8 bits.


Previous page  Reading a Graphics Image Querying a Graphics File Next page

© 1994-2005 The MathWorks, Inc.