octave: Representing Images

 
 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.
 
  -- : 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: Seedouble XREFdouble.
 
  -- : 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: Seecolormap XREFcolormap, Seergbplot XREFrgbplot.
 
  -- : 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: Seeind2gray XREFind2gray, Seergb2ind XREFrgb2ind.
 
  -- : 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 ‘rgb2ntsc’ 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: Seegray2ind XREFgray2ind, Seeind2rgb XREFind2rgb.
 
  -- : [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: Seeind2rgb XREFind2rgb, Seergb2hsv XREFrgb2hsv,
      Seergb2ntsc XREFrgb2ntsc.
 
  -- : 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: Seergb2ind XREFrgb2ind, Seeind2gray XREFind2gray,
      Seehsv2rgb XREFhsv2rgb, Seentsc2rgb XREFntsc2rgb.
 
  -- : [X, MAP] = frame2im (F)
      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 F 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: Seeim2frame XREFim2frame.
 
  -- : 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: Seeframe2im XREFframe2im.
 
  -- : 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
      the parent figure of HAX 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).
 
DONTPRINTYET       See also: Seeviridis XREFviridis, Seejet XREFjet, *noteDONTPRINTYET DONTPRINTYET       See also: Seeviridis XREFviridis, Seejet XREFjet, See
      cubehelix XREFcubehelix, Seehsv XREFhsv, *noterainbow:
DONTPRINTYET DONTPRINTYET DONTPRINTYET       See also: Seeviridis XREFviridis, Seejet XREFjet, See
      cubehelix XREFcubehelix, Seehsv XREFhsv, Seerainbow

      XREFrainbow, Seehot XREFhot, Seecool XREFcool, *noteDONTPRINTYET DONTPRINTYET DONTPRINTYET DONTPRINTYET       See also: Seeviridis XREFviridis, Seejet XREFjet, See
      cubehelix XREFcubehelix, Seehsv XREFhsv, Seerainbow

      XREFrainbow, Seehot XREFhot, Seecool XREFcool, See
      spring XREFspring, Seesummer XREFsummer, *noteautumn:
DONTPRINTYET DONTPRINTYET DONTPRINTYET DONTPRINTYET DONTPRINTYET       See also: Seeviridis XREFviridis, Seejet XREFjet, See
      cubehelix XREFcubehelix, Seehsv XREFhsv, Seerainbow

      XREFrainbow, Seehot XREFhot, Seecool XREFcool, See
      spring XREFspring, Seesummer XREFsummer, Seeautumn

      XREFautumn, Seewinter XREFwinter, Seegray XREFgray, *noteDONTPRINTYET DONTPRINTYET DONTPRINTYET DONTPRINTYET DONTPRINTYET       See also: Seeviridis XREFviridis, Seejet XREFjet, See
      cubehelix XREFcubehelix, Seehsv XREFhsv, Seerainbow

      XREFrainbow, Seehot XREFhot, Seecool XREFcool, See
      spring XREFspring, Seesummer XREFsummer, Seeautumn

      XREFautumn, Seewinter XREFwinter, Seegray XREFgray, See
      bone XREFbone, Seecopper XREFcopper, Seepink XREFpink,
DONTPRINTYET       Seeocean XREFocean, Seecolorcube XREFcolorcube, *noteflag:
DONTPRINTYET DONTPRINTYET       Seeocean XREFocean, Seecolorcube XREFcolorcube, Seeflag

      XREFflag, Seelines XREFlines, Seeprism XREFprism, *noteDONTPRINTYET DONTPRINTYET       Seeocean XREFocean, Seecolorcube XREFcolorcube, Seeflag

      XREFflag, Seelines XREFlines, Seeprism XREFprism, See
      white XREFwhite.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : 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.
 
DONTPRINTYET       See also: Seecolormap XREFcolormap, *notebrighten:
DONTPRINTYET       See also: Seecolormap XREFcolormap, Seebrighten

      XREFbrighten.
 
    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.
 
DONTPRINTYET       See also: Seecolormap XREFcolormap, *notecontrast:
DONTPRINTYET       See also: Seecolormap XREFcolormap, Seecontrast

      XREFcontrast.
 
  -- : 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: Seecolormap XREFcolormap.
 
  -- : whitebg ()
  -- : whitebg (COLOR)
  -- : whitebg ("none")
  -- : whitebg (HFIG, ...)
      Invert the colors in the current color scheme.
 
      The root properties are also inverted such that all subsequent plot
      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 default
      colors.
 
      If the first argument HFIG is a figure handle, then operate on this
      figure rather than the current figure returned by ‘gcf’.  The root
      properties will not be changed.
 
DONTPRINTYET       See also: Seereset XREFreset, Seeget XREFget, *noteset:
DONTPRINTYET       See also: Seereset XREFreset, Seeget XREFget, Seeset

      XREFset.
 
    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: Seergb2ind XREFrgb2ind, Seegray2ind XREFgray2ind.
 
  -- : [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.