Products & Services Solutions Academia Support User Community Company

Learn more about Image Processing Toolbox   

Writing Image Data to a File

Overview

To export image data from the MATLAB workspace to a graphics file in one of the supported graphics file formats, use the imwrite function. When using imwrite, you specify the MATLAB variable name and the name of the file. If you include an extension in the filename, imwrite attempts to infer the desired file format from it. For example, the file extension .jpg infers the Joint Photographic Experts Group (JPEG) format. You can also specify the format explicitly as an argument to imwrite.

This example loads the indexed image X from a MAT-file, clown.mat, along with the associated colormap map, and then exports the image as a bitmap (BMP) file.

load clown
whos
  Name          Size         Bytes  Class

  X           200x320       512000  double array
  caption       2x1              4  char array
  map          81x3           1944  double array

Grand total is 64245 elements using 513948 bytes

imwrite(X,map,'clown.bmp')

Specifying Format-Specific Parameters

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

imwrite(I,'clown.png','BitDepth',4);

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

imwrite(A, 'myfile.jpg', 'Quality', 100);

For more information about these additional format-specific syntaxes, see the imwrite reference page.

Reading and Writing Binary Images in 1-Bit Format

In certain file formats, such as TIFF, a binary image can be stored in a 1-bit format. When you read in a binary image in 1-bit format, imread stores the data in the workspace as a logical array. If the file format supports it, imwrite writes binary images as 1-bit images by default. This example reads in a binary image and writes it as a TIFF file.

BW = imread('text.png');
imwrite(BW,'test.tif'); 

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

info = imfinfo('test.tif');

info.BitDepth
ans =

     1

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 supports 1-bit images, imwrite creates a 1-bit image file.

If the output image file format specified does not support 1-bit images, imwrite exports the image data as a uint8 grayscale image.

uint8

If the output image file format supports unsigned 8-bit images, imwrite creates an unsigned 8-bit image file.

uint16

If the output image file format supports unsigned 16-bit images (PNG or TIFF), imwrite creates an unsigned 16-bit image file.

If the output image file format 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.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS