Convert grayscale or binary image to indexed image
[X, map] = gray2ind(I,n)
[X, map] = gray2ind(BW,n)
[X, map] = gray2ind(I,n) converts the grayscale image I to an indexed image X. n specifies the size of the colormap, gray(n). n must be an integer between 1 and 65536. If n is omitted, it defaults to 64.
gray2ind scales and then rounds the intensity image to produce an equivalent indexed image.
The input image I can be logical, uint8, uint16, int16, single, or double and must be a real and nonsparse. The image I can have any dimension. The class of the output image X is uint8 if the colormap length is less than or equal to 256; otherwise it is uint16.
Convert a grayscale image into an indexed image and then view the result.
I = imread('cameraman.tif'); [X, map] = gray2ind(I, 16); imshow(X, map);