MATLAB: Matlab cuts colors from color map

colormapcolorsgray2hsigrayimageshsiind2rgbMATLABpseudocoloringquntizationrgbrgb2ind

Hello everyone,
I have a problem during pseudocoloring of my gray images with self defined colormaps. Here is the code:
in = imread('B0002_0003.png'); %uint8 image%
map = zeros(256,3);
x = 1:256;
map(:,1) = 1/2 - 1/2*sqrt(1 - (x/256).*(x/256));
map(:,2) = 0.8;
map(:,3) = (log(((x+1) / (256 + 1)))+5)/5;
figure;imshow(in,map);
o = ind2rgb(in,map);
figure;imshow(o);
[m, n] = rgb2ind(o,60000);
figure;plot(n);
figure;plot(map);
After execution of this code, the figure uses only 33 colors from the colormap. I conclude that from n variable. And it distorts my colormap. Why is this happening? How can I perform full coloring with definied colormap?

Best Answer

You are doing stuff that doesn't make sense, perhaps because you didn't realize it because you didn't use descriptive variable names. Look at my modified code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
% in = imread('B0002_0003.png'); %uint8 image%
inputImage = imread('cameraman.tif'); % Gray scale demo image.
map = zeros(256,3);
x = 1:256;
map(:,1) = 1/2 - 1/2*sqrt(1 - (x/256).*(x/256));
map(:,2) = 0.8;
map(:,3) = (log(((x+1) / (256 + 1)))+5)/5;
% Plot my colormap
subplot(2, 2, 1);
plot(map(:, 1), 'r', 'LineWidth', 3);
hold on;
plot(map(:, 2), 'g', 'LineWidth', 3);
plot(map(:, 3), 'b', 'LineWidth', 3);
grid on;
title('My Color Map', 'FontSize', fontSize);
subplot(2, 2, 2);
imshow(inputImage, 'ColorMap', map);
title('Input Image with My Color Map applied', 'FontSize', fontSize);
colorbar;
rgbImage = ind2rgb(inputImage, map);
subplot(2, 2, 3);
imshow(rgbImage, 'ColorMap', map); % Will throw a warning saying you can't use a colormap on a RGB image.
title('Reconstructed RGB Image', 'FontSize', fontSize);
% Create map with 60,000 colors from the indexedImage
% which does not make sense.
numberOfColors = 256;
[indexedImage, newColorMap] = rgb2ind(rgbImage, numberOfColors);
% Plot colormap created by rgb2ind
subplot(2, 2, 4);
plot(newColorMap(:, 1), 'r', 'LineWidth', 3);
hold on;
plot(newColorMap(:, 2), 'g', 'LineWidth', 3);
plot(newColorMap(:, 3), 'b', 'LineWidth', 3);
grid on;
title('Color Map from rgb2ind', 'FontSize', fontSize);
0000 Screenshot.png
Is your input image RGB or gray scale, or indexed?
You can see you're trying to get a colormap from an, essentially, indexed image in rgb2ind() instead of from an RGB image. It's RGB but it was created from an indexed process so there are just the number of colors that were in the colormap you applied. So when you use rgb2ind() to try to get back your original colormap, it will do that but it thinks it doesn't need 60 thousand colors and can use ony 36. It looks weird to you because there is no guarantee that the colors it comes up with will be in the same order. In fact, the order is all scrambled. Chances are the colors are in there just at a different index than you used. For example, you might say 100 should be red, but when it found red it might have said "Let's let red be at index 40" so it didn't pick the same indexes that you used, and that is why the colormap it came up with looks weird when plotted.
Also, most monitors can't take more than 256 colors, not 60 thousand. Also in newer versions of MATLAB you need to use the word 'Colormap' when specifying the colormap, which you didn't do.
Related Question