How are the indices computed in gray2ind(...)?
1 view (last 30 days)
In the below code, I would like to know the mathematics that was involved behind generating an index for a pixel. Does it consider the distribution of intensity values of all the pixels in the image? Or, does it just consider pixel by pixel independently, and generate an index?
camera = imread("cameraman.tif");
[x, map] = gray2ind(camera, 32);
DGM on 16 Oct 2021
Here's an example for a uint8 image:
A = imread('cameraman.tif');
maplength = 32;
[B bmap] = gray2ind(A,maplength);
% assuming uint8 input
C = uint8((maplength-1)/255 * double(A));
cmap = gray(maplength);
immse(B,C) % images are the same
immse(bmap,cmap) % maps are the same
You can also just look at how it's done by opening gray2ind.m.
So it appears that it pays no attention to the actual image content either locally or globally. It merely does a crude uniform quantization based on the standard data range defined by the numeric class.
This is in contrast to rgb2ind() which is both more complicated in its available palette reduction methods, but also wrapped up in mex, so figuring out how it works isn't as easy.