FUNCTION REFERENCE

rgb2ind

Convert an RGB image to an indexed image.

Convert an RGB image to an indexed image.

Syntax

[X, map] = rgb2ind(RGB, n)

Syntax

X = rgb2ind(RGB, map)

Syntax

[X, map] = rgb2ind(RGB, tol)

Syntax

[X, map] = rgb2ind(RGB, n, 'nodither')

RGB is an M-by-N-by-3 double, single, uint8 or uint16 image. With n (1 to 65536 colours) an image with at most n distinct colours is reproduced exactly: map holds its distinct colours in ascending (R, G, B) order. Otherwise map comes from deterministic median cut (Heckbert 1982: the colour box with the longest side is split at its pixel-weighted median until there are n boxes; each entry is the weighted mean of its box) and every pixel takes the nearest entry. With a c-by-3 colormap each pixel takes the nearest colour (Euclidean distance in RGB, the first one on ties). With 0 < tol < 1 the image is quantized uniformly to floor(1/tol)+1 levels per channel and map holds the colours used (at most 65536). X is zero-based: uint8 when map has at most 256 colours, otherwise uint16; map is double in [0, 1].

Example

[X, map] = rgb2ind(uint8(cat(3, [255 0; 0 255], [0 255; 0 255], [0 0; 255 255])), 4); X   % [2 1; 0 3] uint8

Example

X = rgb2ind(cat(3, [0.1 0.9], [0.1 0.8], [0.2 0.9]), [0 0 0; 1 1 1])   % [0 1] uint8

Example

[X, map] = rgb2ind(cat(3, [0 0.3 1], [0 0.3 1], [0 0.3 1]), 0.5); map   % [0 0 0; 0.5 0.5 0.5; 1 1 1]

Differences from MATLAB: 'dither' is accepted but maps without dithering, like 'nodither'. MATLAB's quantizer for n colours is minimum variance quantization, so for images with more than n colours the map (and its order) differs; the rgb2ind(R, G, B, ...) form is not supported.

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.