Next: , Up: Image Processing   [Contents][Index]

32.1 Loading and Saving Images

The first step in most image processing tasks is to load an image into Octave which is done with the imread function. The imwrite function is the corresponding function for writing images to the disk.

In summary, most image processing code will follow the structure of this code

I = imread ("my_input_image.img");
J = process_my_image (I);
imwrite (J, "my_output_image.img");
: [img, map, alpha] = imread (filename)
: […] = imread (url)
: […] = imread (…, ext)
: […] = imread (…, idx)
: […] = imread (…, param1, value1, …)

Read images from various file formats.

Read an image as a matrix from the file filename or from the online resource url. If neither is given, but ext was specified, look for a file with the extension ext.

The size and class of the output depends on the format of the image. A color image is returned as an MxNx3 matrix. Grayscale and black-and-white images are of size MxN. Multipage images will have an additional 4th dimension.

The bit depth of the image determines the class of the output: "uint8", "uint16", or "single" for grayscale and color, and "logical" for black-and-white. Note that indexed images always return the indexes for a colormap, independent of whether map is a requested output. To obtain the actual RGB image, use ind2rgb. When more than one indexed image is being read, map is obtained from the first. In some rare cases this may be incorrect and imfinfo can be used to obtain the colormap of each image.

See the Octave manual for more information in representing images.

Some file formats, such as TIFF and GIF, are able to store multiple images in a single file. idx can be a scalar or vector specifying the index of the images to read. By default, Octave will read only the first page.

Depending on the file format, it is possible to configure the reading of images with parameter, value pairs. The following options are supported:

"Frames" or "Index"

This is an alternative method to specify idx. When specifying it in this way, its value can also be the string "all".


This option exists for MATLAB compatibility, but has no effect. For maximum performance when reading multiple images from a single file, use the "Index" option.


Controls the image region that is read. The value must be a cell array with two arrays of 3 elements {[rows], [cols]}. The elements in the array are the start, increment, and end pixel to be read. If the increment value is omitted it defaults to 1. For example, the following are all equivalent:

imread (filename, "PixelRegion", {[200 600], [300 700]});
imread (filename, "PixelRegion", {[200 1 600], [300 1 700]});
imread (filename)(200:600, 300:700);

See also: imwrite, imfinfo, imformats.

: imwrite (img, filename)
: imwrite (img, filename, ext)
: imwrite (img, map, filename)
: imwrite (…, param1, val1, …)

Write images in various file formats.

The image img can be a binary, grayscale, RGB, or multi-dimensional image. The size and class of img should be the same as what should be expected when reading it with imread: the 3rd and 4th dimensions reserved for color space, and multiple pages respectively. If it’s an indexed image, the colormap map must also be specified.

If ext is not supplied, the file extension of filename is used to determine the format. The actual supported formats are dependent on options made during the build of Octave. Use imformats to check the support of the different image formats.

Depending on the file format, it is possible to configure the writing of images with param, val pairs. The following options are supported:


Alpha (transparency) channel for the image. This must be a matrix with same class, and number of rows and columns of img. In case of a multipage image, the size of the 4th dimension must also match and the third dimension must be a singleton. By default, image will be completely opaque.


Compression to use one the image. Can be one of the following: "none" (default), "bzip", "fax3", "fax4", "jpeg", "lzw", "rle", or "deflate". Note that not all compression types are available for all image formats in which it defaults to your Magick library.


For formats that accept animations (such as GIF), controls for how long a frame is displayed until it moves to the next one. The value must be scalar (which will applied to all frames in img), or a vector of length equal to the number of frames in im. The value is in seconds, must be between 0 and 655.35, and defaults to 0.5.


For formats that accept animations (such as GIF), controls what happens to a frame before drawing the next one. Its value can be one of the following strings: "doNotSpecify" (default); "leaveInPlace"; "restoreBG"; and "restorePrevious", or a cell array of those string with length equal to the number of frames in img.


For formats that accept animations (such as GIF), controls how many times the sequence is repeated. A value of Inf means an infinite loop (default), a value of 0 or 1 that the sequence is played only once (loops zero times), while a value of 2 or above loops that number of times (looping twice means it plays the complete sequence 3 times). This option is ignored when there is only a single image at the end of writing the file.


Set the quality of the compression. The value should be an integer between 0 and 100, with larger values indicating higher visual quality and lower compression. Defaults to 75.


Some file formats, such as TIFF and GIF, are able to store multiple images in a single file. This option specifies if img should be appended to the file (if it exists) or if a new file should be created for it (possibly overwriting an existing file). The value should be the string "Overwrite" (default), or "Append".

Despite this option, the most efficient method of writing a multipage image is to pass a 4 dimensional img to imwrite, the same matrix that could be expected when using imread with the option "Index" set to "all".

See also: imread, imfinfo, imformats.

: val = IMAGE_PATH ()
: old_val = IMAGE_PATH (new_val)
: IMAGE_PATH (new_val, "local")

Query or set the internal variable that specifies a colon separated list of directories in which to search for image files.

When called from inside a function with the "local" option, the variable is changed locally for the function and any subroutines it calls. The original variable value is restored when exiting the function.


It is possible to get information about an image file on disk, without actually reading it into Octave. This is done using the imfinfo function which provides read access to many of the parameters stored in the header of the image file.

: info = imfinfo (filename)
: info = imfinfo (url)
: info = imfinfo (…, ext)

Read image information from a file.

imfinfo returns a structure containing information about the image stored in the file filename. If there is no file filename, and ext was specified, it will look for a file named filename and extension ext, i.e., a file named filename.ext.

The output structure info contains the following fields:


The full name of the image file.


Date of last modification to the file.


Number of bytes of the image on disk


Image format (e.g., "jpeg").


Image height in pixels.


Image Width in pixels.


Number of bits per channel per pixel.


Image type. Value is "grayscale", "indexed", "truecolor", "CMYK", or "undefined".


X resolution of the image.


Y resolution of the image.


Units of image resolution. Value is "Inch", "Centimeter", or "undefined".


Time in 1/100ths of a second (0 to 65535) which must expire before displaying the next image in an animated sequence.


Number of iterations to loop an animation.


Endian option for formats that support it. Value is "little-endian", "big-endian", or "undefined".


Gamma level of the image. The same color image displayed on two different workstations may look different due to differences in the display monitor.


JPEG/MIFF/PNG compression level. Value is an integer in the range [0 100].


Only valid for GIF images, control how successive frames are rendered (how the preceding frame is disposed of) when creating a GIF animation. Values can be "doNotSpecify", "leaveInPlace", "restoreBG", or "restorePrevious". For non-GIF files, value is an empty string.


Value is a 1x8 Matrix with the x,y chromaticity values for white, red, green, and blue points, in that order.


Image comment.


Compression type. Value can be "none", "bzip", "fax3", "fax4", "jpeg", "lzw", "rle", "deflate", "lzma", "jpeg2000", "jbig2", "jbig2", or "undefined".


Colormap for each image.


The orientation of the image with respect to the rows and columns. Value is an integer between 1 and 8 as defined in the TIFF 6 specifications, and for MATLAB compatibility.


Name and version of the software or firmware of the camera or image input device used to generate the image.


The manufacturer of the recording equipment. This is the manufacture of the DSC, scanner, video digitizer or other equipment that generated the image.


The model name or model number of the recording equipment as mentioned on the field "Make".


The date and time of image creation as defined by the Exif standard, i.e., it is the date and time the file was changed.


The title of the image as defined by the Exif standard.


Name of the camera owner, photographer or image creator.


Copyright notice of the person or organization claiming rights to the image.


A struct with information retrieved from the Exif tag.


A struct with geotagging information retrieved from the Exif tag.

See also: imread, imwrite, imshow, imformats.

By default, Octave’s image IO functions (imread, imwrite, and imfinfo) use the GraphicsMagick library for their operations. This means a vast number of image formats is supported but considering the large amount of image formats in science and its commonly closed nature, it is impossible to have a library capable of reading them all. Because of this, the function imformats keeps a configurable list of available formats, their extensions, and what functions should the image IO functions use. This allows one to expand Octave’s image IO capabilities by creating functions aimed at acting on specific file formats.

While it would be possible to call the extra functions directly, properly configuring Octave with imformats allows one to keep a consistent code that is abstracted from file formats.

It is important to note that a file format is not actually defined by its file extension and that GraphicsMagick is capable to read and write more file formats than the ones listed by imformats. What this means is that even with an incorrect or missing extension the image may still be read correctly, and that even unlisted formats are not necessarily unsupported.

: imformats ()
: formats = imformats (ext)
: formats = imformats (format)
: formats = imformats ("add", format)
: formats = imformats ("remove", ext)
: formats = imformats ("update", ext, format)
: formats = imformats ("factory")

Manage supported image formats.

formats is a structure with information about each supported file format, or from a specific format ext, the value displayed on the field ext. It contains the following fields:


The name of the file format. This may match the file extension but Octave will automatically detect the file format.


A long description of the file format.


A function handle to confirm if a file is of the specified format.


A function handle to write if a file is of the specified format.


A function handle to open files the specified format.


A function handle to obtain image information of the specified format.


Logical value if format supports alpha channel (transparency or matte).


Logical value if format supports multipage (multiple images per file).

It is possible to change the way Octave manages file formats with the options "add", "remove", and "update", and supplying a structure format with the required fields. The option "factory" resets the configuration to the default.

This can be used by Octave packages to extend the image reading capabilities Octave, through use of the PKG_ADD and PKG_DEL commands.

See also: imfinfo, imread, imwrite.

Next: , Up: Image Processing   [Contents][Index]