Image I/O (Image Graphics) (MATLAB)

If you are generating an image file for use by others, you will likely choose a format that is commonly viewable with most image editing software. If your image contains only a few colors or is made up of mostly constant color areas, then the color-mapped formats, e.g., PNG and GIF, will do well; however, if you are sharing your images on the Internet, you will probably choose a format that will compress your data and reduce file size. If your images

Extension

Type

Read

Write

Use

bmp

bitmap or device independent bitmap

R/W

Native format for Microsoft Windows. Can support up to 24-bit color. Originally uncompressed, run-length encoding (lossless) compression is now supported.

jpg

jpeg – joint photographic experts group

R/W

24-bit (true color) support. Created to support the photographic industry. Compression can result in noticeable loss of image quality in some images or annoying "artifacts." Compression ratios of 25 or 30 to 1 with photographic images producing good results are not uncommon, but the more you compress, the poorer the picture quality. No transparency support.

tiff


tagged image file format

R/W

Originally created in the 1980s to support data output from scanners (raster scan). Limited to 4GB of data. Can contain information about colorimetry calibration, gamut tables, etc., such as occurs with remote sensing and multi-spectral applications. Can support various compression algorithms in compressed modes.

gif

graphics

interchange

format

R

Very common and used extensively on the Internet. Works well for illustrations or clip-arts that have large areas of flat colors. Does not work so well with photographic images or images with continuous tones. Limited to 256 colors that are "dithered" to look like more colors. Supports animation (GIF89a standard). Use for logos, bullets, or cliparts where few colors are used. Typically 5 to 1 compression ratio.

Extension

Type

Read

Write

Use

png

portable

network

graphics

R/W

Similar to GIF, very efficient lossless compression, supporting variable transparencies (alpha channels), and gamma correction, but not animations.

pcx

R/W

Similar to bmp, up to 24-bit color and lossless compression.

hdf

hierarchical data format

R/W

A data interchange format championed by the National Center for Supercomputing Applications.

xwd

X-Windows

Dump

R/W

Used on Unix workstations.

ico

Windows icon format

R

Used by Windows for icon graphics. 32 x 32 bits by default. Can have multiple images in one file (animations). No compression.

cur

Windows cursor format

R

Used by Windows for cursor graphics. Can contain animations. No compression.

The three principal image I/O functions in MATLAB are imread (for reading graphics files), imwrite(for writing data to a graphics file format), and imfinfo (for retrieving information about a specific graphics file).

Reading a Graphics Image

MATLAB includes a JPEG image of the complex planetary nebula NGC6543A, a.k.a. the "Cat’s Eye Nebula," in the file ngc6543a.jpg. This should have been automatically placed in a folder on your MATLAB path when you installed MATLAB. You can use imfinfo to retrieve information about the image file. Its general form is

tmp8414433_thumb

where both input variables are strings, the first being the name of the file and the second being the image file format. To retrieve the file data about the Cat’s Eye Nebula, you could type,

tmp8414434_thumb

which will return

tmp8414435_thumb

 

 

 

tmp8414436_thumb

From this we can see that ngc6543a.jpg is a 600×650 truecolor image. Note that you don’t have to specify a file extension in filename provided the extension is the same as fmt. The function imfinfo returns a structure, so we could have used,

tmp8414437_thumb

and then accessed specific data from the fields, such as

tmp8414438_thumb

which would return the string ‘jpg’.

The imread function has the general form

tmp8414439_thumb

where filename and fmt are strings specifying the name of the file and its format, just as in imfinfo. X is the returned image data, which can be MxN for indexed images, or MxNx3 for true color images, and C is the colormap if the image is indexed. For example, you can read the Cat’s Eye Nebula image with the following code.

tmp8414440_thumb

Since this is a truecolor image, this will return a 650x600x3 uint8 array in and an empty array for c since there is no colormap with JPEG image Knowing this is a JPEG format, we know there would not be a colormap so v could have used the form,

tmp8414441_thumb

As with imfinfo, we don’t have to specify an extension with the file name if the extension correctly corresponds to the specified file format fmt.

Displaying a Graphics Image

Displaying the image you have just read is achieved by simply typing

tmp8414442_thumb

This will open a Figure Window and plot the image on the axis. If you have read the JPEG image above, image will readily accept the 3-D array and display the RGB images. Notice that the same plotting considerations of relative axis scale arise here as well; you might wish to use axis equal to correct the perspective. If the image you read is an indexed image, i.e., a 2-D index array with a corresponding Nx3 colormap, you will need to use the colormap function to get the correct coloring of the plot. At the website you can download the indexed image usflag.dib (Windows device-independent-bitmap). Once you have that file you can read it, then view it with,

tmp8414443_thumb

You should see the image shown in Figure 5.4.

An indexed image without its colormap.

Figure 5.4 An indexed image without its colormap.

Notice that the colors are not as you would expect. You might have something very strange indeed if you have been using other colormaps since MATLAB will apply the last colormap used in the current Figure Window. To get the expected patriotic colors, you will next need to load the appropriate colormap, in this case cm.

tmp8414445_thumb

 

An indexed image with the appropriate colormap.

Figure 5.5 An indexed image with the appropriate colormap.

In Section 5.3 we examine indexed images in more detail.

Writing a Graphics Image

Writing the contents of a Figure Window to an image file is just as simple as reading one. The imwrite function provides a means to create image files of the formats indicated in Table 5.1.2. The general form of imwrite is,

tmp8414447_thumb

where Λ is the image, either grayscale if NxM, or truecolor if NxMx3, filename is a string containing the name of the file to be created, and fmt is a string indicating one of the write formats indicated in Table 5.1.2. For the case of an indexed image, i.e., one containing an image and colormap, imwrite takes the following form.

tmp8414448_thumb

In this case, X is an NxM array of indexes into colormap C. Using imwrite can be easily demonstrated by loading one of the color image data files that is distributed with MATLAB and writing to one of the image file formats. In the following example, we will use the load function to load an image of a clown. Typing

tmp8414449_thumb

at the command prompt will load X, a 200×300 double array, and map, an 81×3 double array, into the workspace. Notice that this data is an index array and colormap. We can then create, for instance, a PNG format image by typing,

tmp8414450_thumb

which will create the file clown.png in the default working folder.

Next post:

Previous post: