imagematlabimage-processingcomputer-vision

How to match the resulting "map" with the given "color_map"?


I have a RGB image called imrgb, the size of which is 320*512*3 double.And I also have the color_map = 64*3 double. I use the following code:

[X, map] = rgb2ind(imrgb, 256)

the resulting X= 320*512 uint8, and the resulting map = 65*3 double. The resulting "map" is totally different from the given "color_map". How to fix this problem?

The first few rows of "map" looks like this:

    0      0        0
    0      0.125    1
    0.56   1        0.439
    1      0.125    0
    0.188   1       0.812
    1       0.749   0
    0       0.7490  1
    0.5019  0       0
    0.7490  1       0.25098

The first few rows of given "color_map" looks like this:

0   0   0.5625
0   0   0.6250
0   0   0.6875
0   0   0.7500
0   0   0.8125
0   0   0.8750
0   0   0.9375
0   0       1
0   0.0625  1

Solution

  • Using rgb2ind performs a colour quantization (the default is uniform quantization directly in RGB space and we also apply dithering to the image) of your image to a certain number of colours (in your case, 256) if you don't specify an input colour map. The map output provides a colour map that best segments the colours using that quantization for your image imrgb. The variable X gives you indices of where each pixel maps to in the colour map from the map variable. This is actually a lookup table of colours.

    This map variable tells us which colour from the lookup table that the pixel should be visualized as. For example, the above call in your post will produce an indexed image X that has indices from 1 to 256 and map will contain 256 rows where each column is a proportion of red (first column), green (second column) and blue (third column). The proportions are between [0-1]. Also, each row is a unique colour. Therefore, if an index found in the image was 5, we'd look up the fifth row of this colour map and that is the colour that would be representative of that pixel.

    Therefore, calling rgb2ind in your current way does not correspond to a custom colour map that was provided by you unless you provide this colour map as input into rgb2ind. As such, if you want to obtain an indexed image using a custom colour map provided by you, use this colour map as input into rgb2ind so that the indexed image is with respect to this new colour map.

    In that case, you simply need to do:

    [X, map] = rgb2ind(imrgb, colour_map);