Image I/O

This section describes functions which offer support for reading and writing image files. Formats supported include PNG and JPEG.

Calerga gratefully acknowledges the following contributions: PNG encoding and decoding are based on libpng; and JPEG encoding and decoding are based on the work of the Independent JPEG Group.

imageread

Read an image file.

Syntax

A = imageread(fd)

Description

imageread(fd) reads a PNG or JPEG file from file descriptor fd and returns it as an array whose first dimension is the image height and second dimension the image width. Grayscale images give a third dimension equal to 1 (i.e. plain matrices). Color images give a third dimension equal to 3; fist plane is the red component, second plane the green component, and third plane the blue component. In both cases, value range is 0 for black to 1 for maximum intensity.

The file descriptor is usually obtained by opening a file with fopen in binary mode (text mode, with end-of-line translation, would produce garbage or cause a decoding error).

Example

fd = fopen('image.png', 'r');
im = imageread(fd);
fclose(fd);

See also

imagewrite

imageset

Options for image output.

Syntax

options = imageset
options = imageset(name1, value1, ...)
options = imageset(options0, name1, value1, ...)

Description

imageset(name1,value1,...) creates the option argument used by imagewrite. Options are specified with name/value pairs, where the name is a string which must match exactly the names in the table below. Case is significant. Options which are not specified have a default value. The result is a structure whose fields correspond to each option. Without any input argument, imageset creates a structure with all the default options. Note that imagewrite also interpret the lack of an option argument, or the empty array [], as a request to use the default values.

When its first input argument is a structure, imageset adds or changes fields which correspond to the name/value pairs which follow.

Here is the list of permissible options:

NameDefaultMeaning
Type'PNG''PNG' or 'JPG'/'JPEG'
Quality80JPEG quality (0=worst,100=best)
Progressivefalsetrue to permit progressive decoding

Examples

Default options:

imageset
  Type: 'png'
  Quality: 80
  Progressive: false

Writing the contents of array A into a small, low-quality JPEG file:

fd = fopen('A.jpg', 'w');
imagewrite(fd, A, imageset('Type','JPG','Quality',20));
fclose(fd);

See also

imagewrite

imagewrite

Write an image file.

Syntax

imagewrite(fd, A)
imagewrite(fd, A, options)

Description

imagewrite(fd,A) writes array A to a PNG file specified by file descriptor fd. Image A is an array whose first dimension is the image height and second dimension the image width. Grayscale images have their third dimension equal to 1 (i.e. they are plain matrices). Color images have a third dimension equal to 3; fist plane is the red component, second plane the green component, and third plane the blue component. In both cases, value range is 0 for black to 1 for maximum intensity. Values outside this range are clipped.

imagewrite(fd,A,options) uses structure options to specify image file options. Options are usually created with function imageset; they include the file type.

The file descriptor is usually obtained by opening a file with fopen in binary mode (text mode, with end-of-line translation, would produce a corrupted image file).

Example

Write the image contained in the matrix im to a file "image.png", using the default options.

fd = fopen('image.png', 'w');
imagewrite(fd, im);
fclose(fd);

Write the same image as a JPEG file.

fd = fopen('image.jpg', 'w');
imagewrite(fd, im, imageset('Type','JPEG'));
fclose(fd);

See also

imageset, imageread


Copyright 2003-2008, Calerga.
All rights reserved.