Next: Plotting on top of Images, Previous: Displaying Images, Up: Image Processing [Contents][Index]

### 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.

Function File: iscolormap (cmap)

  Return true if cmap is a colormap.

  A colormap is a real matrix with n rows and 3 columns. Each row represents a single color. The columns contain red, green, and blue intensities respectively. All entries must be between 0 and 1 inclusive.

  See also: colormap, rgbplot.

Function File: img = gray2ind (I)
Function File: img = gray2ind (I, n)
Function File: img = gray2ind (BW)
Function File: img = gray2ind (BW, n)
Function File: [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.

Function File: 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 cmap. If cmap 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: gray2ind, ind2rgb.

Function File: [x, map] = rgb2ind (rgb)
Function File: [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 colour 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 colour channels as separate variables.

  See also: ind2rgb, rgb2hsv, rgb2ntsc.

Function File: rgb = ind2rgb (x, map)
Function File: [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, ntsc2rgb.

Function File: cmap = colormap ()
Function File: cmap = colormap (map)
Function File: cmap = colormap ("default")
Function File: cmap = colormap ("map_name")
Function File: cmap = colormap (hax, …)
Command: colormap map_name
Function File: cmaps = colormap ("list")
Function File: colormap ("register", "name")
Function File: colormap ("unregister", "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 `jet` map with 64 entries). The default colormap is returned.

  The map may also be specified by a string, `"map_name"`, where map_name 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`.

  `colormap ("list")` returns a cell array with all of the available colormaps. The options `"register"` and `"unregister"` add or remove the colormap name from this list.

  See also: jet.

Function File: rgbplot (cmap)
Function File: rgbplot (cmap, style)
Function File: 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 each RGB components 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.

Function File: map = autumn ()
Function File: 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.

Function File: map = bone ()
Function File: 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.

Function File: map = colorcube ()
Function File: 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.

Function File: map = cool ()
Function File: 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.

Function File: map = copper ()
Function File: 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.

Function File: map = flag ()
Function File: 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.

Function File: map = gray ()
Function File: 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.

Function File: map = hot ()
Function File: 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.

Function File: 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.

Function File: map = jet ()
Function File: 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.

Function File: map = lines ()
Function File: 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, green, red, cyan, pink, yellow, and gray. The argument n must be a scalar. If unspecified, the length of the current colormap, or 64, is used.

  See also: colormap.

Function File: map = ocean ()
Function File: 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.

Function File: map = pink ()
Function File: map = pink (n)

  Create color colormap. This colormap varies from black to white with shades of gray-pink. It 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.

Function File: map = prism ()
Function File: 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.

Function File: map = rainbow ()
Function File: 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.

Function File: map = spring ()
Function File: 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.

Function File: map = summer ()
Function File: 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.

Function File: map = white ()
Function File: map = white (n)

  Create color colormap. This colormap is completely white. The argument n should be a scalar. If it is omitted, the length of the current colormap or 64 is assumed.

  See also: colormap.

Function File: map = winter ()
Function File: 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.

Function File: cmap = contrast (x)
Function File: 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.

An additional colormap is `gmap40`. This code map contains only colors with integer values of the red, green and blue components. This is a workaround for a limitation of gnuplot 4.0, that does not allow the color of line or patch objects to be set. `gmap40` is chiefly useful to gnuplot 4.0 users, and particularly in conjunction with the bar, surf, and contour functions.

Function File: map = gmap40 ()
Function File: map = gmap40 (n)

  Create color colormap. The colormap consists of red, green, blue, yellow, magenta and cyan. This colormap is specifically designed for users of gnuplot 4.0 where these 6 colors are the allowable ones for patch objects. The argument n must be a scalar. If unspecified, a length of 6 is assumed. Larger values of n result in a repetition of the above colors.

  See also: colormap.

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

Function File: map_out = brighten (map, beta)
Function File: map_out = brighten (beta)
Function File: map_out = brighten (h, beta)

  Brighten or darken a 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.

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

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

  See also: colormap, contrast.

Function File: spinmap ()
Function File: spinmap (t)
Function File: spinmap (t, inc)
Function File: 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.

Function File: whitebg ()
Function File: whitebg (color)
Function File: whitebg ("none")
Function File: 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.

  See also: reset, get, set.

The following functions can be used to manipulate colormaps.

Function File: [Y, newmap] = cmunique (X, map)
Function File: [Y, newmap] = cmunique (RGB)
Function File: [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.

Function File: [Y, newmap] = cmpermute (X, map)
Function File: [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.

Next: Plotting on top of Images, Previous: Displaying Images, Up: Image Processing [Contents][Index]