dither

Converts an image to fewer colors using error-diffusion dithering.

Category: Graphics > Images

Syntax

X = dither(RGB, map)

Approximates the truecolor image RGB with the colors of map, returning an indexed image.

BW = dither(I)

Converts a grayscale image to a binary one.

Description

A pixel is rounded to the nearest available color, and the rounding error is spread to neighboring pixels that are processed later. Regions of intermediate tone then become patterns of the available colors, which the eye averages back.

The result looks smoother than plain quantization but uses only the palette entries.

Input arguments

RGB
m-by-n-by-3 truecolor image.
map
Colormap of the palette to use.
I
Grayscale image.

Output arguments

X
Indexed image, or logical image for the grayscale form.

Example

Two-color palette

g = [1 0.2; 0.5 0];
RGB = cat(3, g, g, g);
X = dither(RGB, [0 0 0; 1 1 1])
Output
X = 2×2 uint8 matrix

   1   0
   1   0

See also

rgb2ind ind2rgb rgb2gray