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");
Read images from various file formats.
Reads an image as a matrix from 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 size and class of the output depends on the format of the image. A color image is returned as an MxNx3 matrix. Gray-level 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"
or"uint16"
for gray and color, and"logical"
for black and white. Note that indexed images always return the indexes for a colormap, independent if map is a requested output. To obtain the actual RGB image, useind2rgb
. 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 only read the first page.
Depending on the file format, it is possible to configure the reading of images with param, val 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"
.- ‘
"Info"
’- This option exists for matlab compatibility and has no effect. For maximum performance while reading multiple images from a single file, use the Index option.
- ‘
"PixelRegion"
’- Controls the image region that is read. Takes as value 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, defaults to 1.
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:
- ‘Quality’
- 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.
- ‘WriteMode’
- 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 usingimread
with the option"Index"
set to"all"
.
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.See also: EXEC_PATH, OCTAVE_HOME.
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.
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:
- ‘Filename’
- The full name of the image file.
- ‘FileSize’
- Number of bytes of the image on disk
- ‘FileModDate’
- Date of last modification to the file.
- ‘Height’
- Image height in pixels.
- ‘Width’
- Image Width in pixels.
- ‘BitDepth’
- Number of bits per channel per pixel.
- ‘Format’
- Image format (e.g.,
"jpeg"
).- ‘LongFormat’
- Long form image format description.
- ‘XResolution’
- X resolution of the image.
- ‘YResolution’
- Y resolution of the image.
- ‘TotalColors’
- Number of unique colors in the image.
- ‘TileName’
- Tile name.
- ‘AnimationDelay’
- Time in 1/100ths of a second (0 to 65535) which must expire before displaying the next image in an animated sequence.
- ‘AnimationIterations’
- Number of iterations to loop an animation (e.g., Netscape loop extension) for.
- ‘ByteOrder’
- Endian option for formats that support it. Value is
"little-endian"
,"big-endian"
, or"undefined"
.- ‘Gamma’
- Gamma level of the image. The same color image displayed on two different workstations may look different due to differences in the display monitor.
- ‘Matte’
true
if the image has transparency.- ‘ModulusDepth’
- Image modulus depth (minimum number of bits required to support red/green/blue components without loss of accuracy).
- ‘Quality’
- JPEG/MIFF/PNG compression level.
- ‘QuantizeColors’
- Preferred number of colors in the image.
- ‘ResolutionUnits’
- Units of image resolution. Value is
"pixels per inch"
,"pixels per centimeter"
, or"undefined"
.- ‘ColorType’
- Image type. Value is
"grayscale"
,"indexed"
,"truecolor"
, or"undefined"
.- ‘View’
- FlashPix viewing parameters.
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 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 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.
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:
- ext
- The name of the file format. This may match the file extension but Octave will automatically detect the file format.
- description
- A long description of the file format.
- isa
- A function handle to confirm if a file is of the specified format.
- write
- A function handle to write if a file is of the specified format.
- read
- A function handle to open files the specified format.
- info
- A function handle to obtain image information of the specified format.
- alpha
- Logical value if format supports alpha channel (transparency or matte).
- multipage
- 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.