### 32.3 Representing Images

In general Octave supports four different kinds of images, grayscale images, RGB images, binary images, and indexed images. A grayscale image is represented with an M-by-N matrix in which each element corresponds to the intensity of a pixel. An RGB image is represented with an M-by-N-by-3 array where each 3-vector corresponds to the red, green, and blue intensities of each pixel.

The actual meaning of the value of a pixel in a grayscale or RGB image depends on the class of the matrix. If the matrix is of class `double` pixel intensities are between 0 and 1, if it is of class `uint8` intensities are between 0 and 255, and if it is of class `uint16` intensities are between 0 and 65535.

A binary image is an M-by-N matrix of class `logical`. A pixel in a binary image is black if it is `false` and white if it is `true`.

An indexed image consists of an M-by-N matrix of integers and a C-by-3 color map. Each integer corresponds to an index in the color map, and each row in the color map corresponds to an RGB color. The color map must be of class `double` with values between 0 and 1.

The following convenience functions are available for conversion between image formats.

im2double (img)
im2double (img, "indexed")

  Convert image to double precision.

  The conversion of img to double precision, is dependent on the type of input image. The following input classes are supported:

  ‘`uint8, uint16, and int16`’

    The range of values from the class is scaled to the interval [0 1].

  ‘`logical`’

    True and false values are assigned a value of 0 and 1 respectively.

  ‘`single`’

    Values are cast to double.

  ‘`double`’

    Returns the same image.

  If img is an indexed image, then the second argument should be the string `"indexed"`. If so, then img must either be of floating point class, or unsigned integer class and it will simply be cast to double. If it is an integer class, a +1 offset is applied.

  See also: double.

img = gray2ind (I)
img = gray2ind (I, n)
img = gray2ind (BW)
img = gray2ind (BW, n)
[img, map] = gray2ind (…)

  Convert a grayscale or binary intensity image to an indexed image.

  The indexed image will consist of n different intensity values. If not given n defaults to 64 for grayscale images or 2 for binary black and white images.

  The output img is of class uint8 if n is less than or equal to 256; Otherwise the return class is uint16.

  See also: ind2gray, rgb2ind.

I = ind2gray (x, map)

  Convert a color indexed image to a grayscale intensity image.

  The image x must be an indexed image which will be converted using the colormap map. If map does not contain enough colors for the image, pixels in x outside the range are mapped to the last color in the map before conversion to grayscale.

  The output I is of the same class as the input x and may be one of `uint8`, `uint16`, `single`, or `double`.

  Implementation Note: There are several ways of converting colors to grayscale intensities. This functions uses the luminance value obtained from `rgb2gray` which is `I = 0.299*R + 0.587*G + 0.114*B`. Other possibilities include the value component from `rgb2hsv` or using a single color channel from `ind2rgb`.

  See also: gray2ind, ind2rgb.

[x, map] = rgb2ind (rgb)
[x, map] = rgb2ind (R, G, B)

  Convert an image in red-green-blue (RGB) color space to an indexed image.

  The input image rgb can be specified as a single matrix of size MxNx3, or as three separate variables, R, G, and B, its three color channels, red, green, and blue.

  It outputs an indexed image x and a colormap map to interpret an image exactly the same as the input. No dithering or other form of color quantization is performed. The output class of the indexed image x can be uint8, uint16 or double, whichever is required to specify the number of unique colors in the image (which will be equal to the number of rows in map) in order.

  Multi-dimensional indexed images (of size MxNx3xK) are also supported, both via a single input (rgb) or its three color channels as separate variables.

  See also: ind2rgb, rgb2hsv, rgb2gray.

rgb = ind2rgb (x, map)
[R, G, B] = ind2rgb (x, map)

  Convert an indexed image to red, green, and blue color components.

  The image x must be an indexed image which will be converted using the colormap map. If map does not contain enough colors for the image, pixels in x outside the range are mapped to the last color in the map.

  The output may be a single RGB image (MxNx3 matrix where M and N are the original image x dimensions, one for each of the red, green and blue channels). Alternatively, the individual red, green, and blue color matrices of size MxN may be returned.

  Multi-dimensional indexed images (of size MxNx1xK) are also supported.

  See also: rgb2ind, ind2gray, hsv2rgb.

Octave also provides tools to produce and work with movie frame structures. Those structures encapsulate the image data (`"cdata"` field) together with the corresponding colormap (`"colormap"` field).

frame = getframe ()
frame = getframe (hax)
frame = getframe (hfig)
frame = getframe (…, rect)

  Capture a figure or axes as a movie frame structure.

  Without an argument, capture the current axes excluding ticklabels, title, and x/y/zlabels. The returned structure frame has a field `cdata`, which contains the actual image data in the form of an NxMx3 (RGB) uint8 matrix, and a field `colormap` which is provided for MATLAB compatibility but is always empty.

  If the first argument hax is an axes handle, then capture this axes, rather than the current axes returned by `gca`.

  If the first argument hfig is a figure handle then the entire corresponding figure canvas is captured.

  Finally, if a second argument rect is provided it must be a four-element vector ([left bottom width height]) defining the region inside the figure to be captured. Regardless of the figure `"units"` property, rect must be defined in pixels.

  See also: im2frame, frame2im, movie.

movie (mov)
movie (mov, n)
movie (mov, n, fps)
movie (h, …)

  Play a movie defined by an array of frame structures.

  The movie mov must be a struct array of frames with fields `"cdata"` and `"colormap"`, as returned by the `getframe` function. By default all images are displayed once, at 12 fps, in the current axes.

  The optional argument n is a scalar or vector of integers that controls the number of times the movie is displayed and which particular frames are shown:

  First element:
    n(1) > 0

      Play the movie n(1) times.

    n(1) < 0

      Play the movie `abs (n(1)` times alternatively in forward and backward order.

  Other elements (if any):

    Indices of the frames in mov that will be displayed.

  If the first argument is a handle to a figure or axes h, the movie is played in that figure or axes instead of the current axes.

  See also: getframe, im2frame, frame2im.

[x, map] = frame2im (frame)

  Convert movie frame to indexed image.

  A movie frame is simply a struct with the fields `"cdata"` and `"colormap"`.

  Support for N-dimensional images or movies is given when frame is a struct array. In such cases, x will be a MxNx1xK or MxNx3xK for indexed and RGB movies respectively, with each frame concatenated along the 4th dimension.

  See also: im2frame, getframe.

im2frame (rgb)
im2frame (x, map)

  Convert image to movie frame.

  A movie frame is simply a struct with the fields `"cdata"` and `"colormap"`.

  Support for N-dimensional images is given when each image projection, matrix sizes of MxN and MxNx3 for RGB images, is concatenated along the fourth dimension. In such cases, the returned value is a struct array.

  See also: frame2im.

The `colormap` function is used to change the colormap of the current axes or figure.

cmap = colormap ()
cmap = colormap (map)
cmap = colormap (`"default"`)
cmap = colormap (map_name)
cmap = colormap (hax, …)
colormap map_name

  Query or set the current colormap.

  With no input arguments, `colormap` returns the current color map.

  `colormap (map)` sets the current colormap to map. The colormap should be an n row by 3 column matrix. The columns contain red, green, and blue intensities respectively. All entries must be between 0 and 1 inclusive. The new colormap is returned.

  `colormap (`"default"`)` restores the default colormap (the `viridis` map with 64 entries). The default colormap is returned.

  The map may also be specified by a string, map_name, which is the name of a function that returns a colormap.

  If the first argument hax is an axes handle, then the colormap for those axes is queried or set.

  For convenience, it is also possible to use this function with the command form, `colormap map_name`.

  The list of built-in colormaps is:

  Map | Description
  --- | ---
  viridis | default
  jet | colormap traversing blue, cyan, green, yellow, red.
  cubehelix | colormap traversing black, blue, green, red, white with increasing intensity.
  hsv | cyclic colormap traversing Hue, Saturation, Value space.
  rainbow | colormap traversing red, yellow, blue, green, violet.
  ————- | ———————————————————————————————
  hot | colormap traversing black, red, orange, yellow, white.
  cool | colormap traversing cyan, purple, magenta.
  spring | colormap traversing magenta to yellow.
  summer | colormap traversing green to yellow.
  autumn | colormap traversing red, orange, yellow.
  winter | colormap traversing blue to green.
  ————- | ———————————————————————————————
  gray | colormap traversing black to white in shades of gray.
  bone | colormap traversing black, gray-blue, white.
  copper | colormap traversing black to light copper.
  pink | colormap traversing black, gray-pink, white.
  ocean | colormap traversing black, dark-blue, white.
  ————- | ———————————————————————————————
  colorcube | equally spaced colors in RGB color space.
  flag | cyclic 4-color map of red, white, blue, black.
  lines | cyclic colormap with colors from axes `"ColorOrder"` property.
  prism | cyclic 6-color map of red, orange, yellow, green, blue, violet.
  ————- | ———————————————————————————————
  white | all white colormap (no colors).

  See also: viridis, jet, cubehelix, hsv, rainbow, hot, cool, spring, summer, autumn, winter, gray, bone, copper, pink, ocean, colorcube, flag, lines, prism, white.

iscolormap (cmap)

  Return true if cmap is a colormap.

  A colormap is a real matrix, of class single or double, with 3 columns. Each row represents a single color. The 3 columns contain red, green, and blue intensities respectively.

  All values in a colormap should be in the [0 1] range but this is not enforced. Each function must decide what to do for values outside this range.

  See also: colormap, rgbplot.

The following functions return predefined colormaps, the same that can be requested by name using the `colormap` function.

rgbplot (cmap)
rgbplot (cmap, style)
h = rgbplot (…)

  Plot the components of a colormap.

  Two different styles are available for displaying the cmap:

  profile (default)

    Plot the RGB line profile of the colormap for each of the channels (red, green and blue) with the plot lines colored appropriately. Each line represents the intensity of an RGB component across the colormap.

  composite

    Draw the colormap across the X-axis so that the actual index colors are visible rather than the individual color components.

  The optional return value h is a graphics handle to the created plot.

  Run `demo rgbplot` to see an example of `rgbplot` and each style option.

  See also: colormap.

map = autumn ()
map = autumn (n)

  Create color colormap. This colormap ranges from red through orange to yellow.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = bone ()
map = bone (n)

  Create color colormap. This colormap varies from black to white with gray-blue shades.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = colorcube ()
map = colorcube (n)

  Create color colormap. This colormap is composed of as many equally spaced colors (not grays) in the RGB color space as possible.

  If there are not a perfect number n of regularly spaced colors then the remaining entries in the colormap are gradients of pure red, green, blue, and gray.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = cool ()
map = cool (n)

  Create color colormap. The colormap varies from cyan to magenta.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = copper ()
map = copper (n)

  Create color colormap. This colormap varies from black to a light copper tone.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = cubehelix ()
map = cubehelix (n)

  Create cubehelix colormap.

  This colormap varies from black to white going though blue, green, and red tones while maintaining a monotonically increasing perception of intensity. This is achieved by traversing a color cube from black to white through a helix, hence the name cubehelix, while taking into account the perceived brightness of each channel according to the NTSC specifications from 1953.

      rgbplot (cubehelix (256))

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  Reference: Green, D. A., 2011, "A colour scheme for the display of astronomical intensity images", Bulletin of the Astronomical Society of India, 39, 289.

  See also: colormap.

map = flag ()
map = flag (n)

  Create color colormap. This colormap cycles through red, white, blue, and black with each index change.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = gray ()
map = gray (n)

  Create gray colormap. This colormap varies from black to white with shades of gray.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = hot ()
map = hot (n)

  Create color colormap. This colormap ranges from black through dark red, red, orange, yellow, to white.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

hsv (n)

  Create color colormap. This colormap begins with red, changes through yellow, green, cyan, blue, and magenta, before returning to red.

  It is useful for displaying periodic functions. The map is obtained by linearly varying the hue through all possible values while keeping constant maximum saturation and value. The equivalent code is `hsv2rgb ([(0:N-1)'/N, ones(N,2)])`.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = jet ()
map = jet (n)

  Create color colormap. This colormap ranges from dark blue through blue, cyan, green, yellow, red, to dark red.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = lines ()
map = lines (n)

  Create color colormap. This colormap is composed of the list of colors in the current axes `"ColorOrder"` property. The default is blue, orange, yellow, purple, green, light blue, and dark red.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = ocean ()
map = ocean (n)

  Create color colormap. This colormap varies from black to white with shades of blue.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = pink ()
map = pink (n)

  Create color colormap. This colormap varies from black to white with shades of gray-pink.

  This colormap gives a sepia tone when used on grayscale images.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = prism ()
map = prism (n)

  Create color colormap. This colormap cycles through red, orange, yellow, green, blue and violet with each index change.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = rainbow ()
map = rainbow (n)

  Create color colormap. This colormap ranges from red through orange, yellow, green, blue, to violet.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = spring ()
map = spring (n)

  Create color colormap. This colormap varies from magenta to yellow.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = summer ()
map = summer (n)

  Create color colormap. This colormap varies from green to yellow.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = viridis ()
map = viridis (n)

  Create color colormap. This colormap ranges from dark purplish-blue through blue, green, to yellow.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = white ()
map = white (n)

  Create color colormap. This colormap is completely white.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

map = winter ()
map = winter (n)

  Create color colormap. This colormap varies from blue to green.

  The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

cmap = contrast (x)
cmap = contrast (x, n)

  Return a gray colormap that maximizes the contrast in an image.

  The returned colormap will have n rows. If n is not defined then the size of the current colormap is used.

  See also: colormap, brighten.

The following three functions modify the existing colormap rather than replace it.

map_out = brighten (beta)
map_out = brighten (map, beta)
map_out = brighten (h, beta)
brighten (…)

  Brighten or darken a colormap.

  The argument beta must be a scalar between -1 and 1, where a negative value darkens and a positive value brightens the colormap.

  If the map argument is omitted, the function is applied to the current colormap.

  The first argument can also be a valid graphics handle h, in which case `brighten` is applied to the colormap associated with this handle.

  If no output is specified then the result is written to the current colormap.

  See also: colormap, contrast.

spinmap ()
spinmap (t)
spinmap (t, inc)
spinmap ("inf")

  Cycle the colormap for t seconds with a color increment of inc.

  Both parameters are optional. The default cycle time is 5 seconds and the default increment is 2. If the option `"inf"` is given then cycle continuously until `Control-C` is pressed.

  When rotating, the original color 1 becomes color 2, color 2 becomes color 3, etc. A positive or negative increment is allowed and a higher value of inc will cause faster cycling through the colormap.

  See also: colormap.

whitebg ()
whitebg (color)
whitebg ("none")
whitebg (hfig)
whitebg (hfig, color)
whitebg (hfig, "none")

  Invert the colors in the current color scheme.

  The root properties are also inverted such that all subsequent plots will use the new color scheme.

  If the optional argument color is present then the background color is set to color rather than inverted. color may be a string representing one of the eight known colors or an RGB triplet. The special string argument `"none"` restores the plot to the factory default colors.

  If the first argument hfig is a figure handle or list of figure handles, then operate on these figures rather than the current figure returned by `gcf`. The root properties will not be changed unless 0 is in the list of figures.

  Programming Note: `whitebg` operates by changing the color properties of the children of the specified figures. Only objects with a single color are affected. For example, a patch with a single `"FaceColor"` will be changed, but a patch with shading (`"interp"`) will not be modified. For inversion, the new color is simply the inversion in RGB space: `cnew = [1-R 1-G 1-B]`. When a color is specified, the axes and figure are set to the new color, and the color of child objects are then adjusted to have some contrast (visibility) against the new background.

  See also: reset, get, set.

The following functions can be used to manipulate colormaps.

[Y, newmap] = cmunique (X, map)
[Y, newmap] = cmunique (RGB)
[Y, newmap] = cmunique (I)

  Convert an input image X to an ouput indexed image Y which uses the smallest colormap possible newmap.

  When the input is an indexed image (X with colormap map) the output is a colormap newmap from which any repeated rows have been eliminated. The output image, Y, is the original input image with the indices adjusted to match the new, possibly smaller, colormap.

  When the input is an RGB image (an MxNx3 array), the output colormap will contain one entry for every unique color in the original image. In the worst case the new map could have as many rows as the number of pixels in the original image.

  When the input is a grayscale image I, the output colormap will contain one entry for every unique intensity value in the original image. In the worst case the new map could have as many rows as the number of pixels in the original image.

  Implementation Details:

  newmap is always an Mx3 matrix, even if the input image is an intensity grayscale image I (all three RGB planes are assigned the same value).

  The output image is of class uint8 if the size of the new colormap is less than or equal to 256. Otherwise, the output image is of class double.

  See also: rgb2ind, gray2ind.

[Y, newmap] = cmpermute (X, map)
[Y, newmap] = cmpermute (X, map, index)

  Reorder colors in a colormap.

  When called with only two arguments, `cmpermute` randomly rearranges the colormap map and returns a new colormap newmap. It also returns the indexed image Y which is the equivalent of the original input image X when displayed using newmap.

  When called with an optional third argument the order of colors in the new colormap is defined by index.

  Caution: index should not have repeated elements or the function will fail.