Jet colormap to grayscale

PerroNoob picture PerroNoob · Sep 16, 2011 · Viewed 29.4k times · Source

I have a jet colormap:

jet colormap

and I would like to know if there's some way to convert to a grayscale. I can't use average because maximum and minimum value goes to the same gray color. Or if there's some way to convert to another color palette.

I can't find on Google a function to convert it. MATLAB uses some thing called rgb2ind but I would like to know the formula.

Answer

Amro picture Amro · Sep 21, 2011

First let me create an indexed image using the Jet colormap:

img = repmat(uint8(0:255), 100, 1);
cmap = jet(256);

imshow(img, 'Colormap',cmap)

jet

The straightforward conversion using IND2GRAY produces the following:

J = ind2gray(img,cmap);
imshow(J)

jet_to_gray

As you expressed, the min/max converge to the same value. From what I understood, you are looking to map the jet colormap to linearly go from dark to light shades of gray. For this, we can reorder using the hue value which we get with the RGB2HSV function. Compare the following against the original colormap:

[~,idx] = sortrows(rgb2hsv(cmap), -1);  %# sort by Hue
C = gray(256);
C = C(idx,:);

imshow(img, 'Colormap',C)

hue_sorted