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.
Read an image file.
A = imageread(fd)
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).
fd = fopen('image.png', 'r');
im = imageread(fd);
fclose(fd);
Options for image output.
options = imageset options = imageset(name1, value1, ...) options = imageset(options0, name1, value1, ...)
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:
| Name | Default | Meaning |
|---|---|---|
| Type | 'PNG' | 'PNG' or 'JPG'/'JPEG' |
| Quality | 80 | JPEG quality (0=worst,100=best) |
| Progressive | false | true to permit progressive decoding |
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);
Write an image file.
imagewrite(fd, A) imagewrite(fd, A, options)
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).
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);