Search code examples
matlabmatlab-figureindexed-image

why different commands result in different indexed color figure


I obtain the index map and colormap using [index_map,colormap] = imread('indexed_color_image.tif'); And then I show the result using two different commands. Their results look very different.

Case 1: figure;imshow(uint8(index_map), colormap);

case 2: figure; imshow(double(index_map), colormap);

Could you give some explanation ? Thanks ! This is the original image.(indexed image).

enter image description here

The result of case 1 is as the original image. But the result for case 2 is weired. See this

enter image description here


Solution

  • It took a couple of hours of digging through the docs, but I finally figured out what the difference is. This comes from the documentation for the image function, which is eventually called after a call to imshow:

    Double-Precision Data (double Array):

    Image is stored as a two-dimensional (m-by-n) array of integers in the range [1, length(colormap)]; colormap is an m-by-3 array of floating-point values in the range [0, 1].

    8-Bit Data (uint8 Array) 16-Bit Data (uint16 Array):

    Image is stored as a two-dimensional (m-by-n) array of integers in the range [0, 255] (uint8) or [0, 65535] (uint16); colormap is an m-by-3 array of floating-point values in the range [0, 1].

    So the answer is that the uint8 data is expected to be in the range [0, 255], while the double data is expected to be in the range [1, 256]. I was able to confirm this by trying this:

    [imdata, immap] = imread('ostrich.png');
    imhandle = imshow(1+double(imdata),immap);
    

    Matlab infers from the datatype how it should index the colormap. It happens to be different for uint8 vs. double, and that is why you saw the strange behavior.