cmunique

Removes duplicate colors from a colormap and reindexes the image.

Category: Graphics > Images

Syntax

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

Reduces an indexed image and its colormap to unique colors.

[Y, newmap] = cmunique(I)

Does the same for a grayscale or truecolor image, producing an indexed image.

Description

A colormap can contain the same color several times. cmunique merges the repeats into one entry and rewrites the indices in the image so the picture does not change. The result is smaller and easier to process.

Indices in the output start at 0 for uint8 images and at 1 for double ones, as in the input convention.

Input arguments

X, map
Indexed image and its colormap.
I
Grayscale or RGB image.

Output arguments

Y
Indexed image, of class uint8 when the colormap has 256 or fewer colors and the input was an integer or grayscale image.
newmap
Colormap without repeated rows.

Example

Merge duplicate colors

X = uint8([0 1; 2 0]);
map = [1 0 0; 0 0 1; 1 0 0];
[Y, newmap] = cmunique(X, map)
Output
Y = 2×2 uint8 matrix

   0   1
   0   0
newmap = 2×3

     1     0     0
     0     0     1

See also

cmpermute rgb2ind ind2rgb unique