MATLAB Answers


matlab cuts colors from color map

Asked by iMatten
on 14 Jan 2019
Latest activity Commented on by iMatten
on 15 Jan 2019
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;
o = ind2rgb(in,map);
[m, n] = rgb2ind(o,60000);
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?


Sign in to comment.

1 Answer

Answer by Image Analyst
on 14 Jan 2019
 Accepted 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);
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.


ok, so input image is a gray scale image(uint8) which I want to colorize with color map that I definded as map.
I understand that colors wont be in the same order. But I dont understand why if I define at the beginning a 256 colormap and try to apply it to the image. It uses only 33 colors, while there are 237 diffrent gray values to colorize in this specific image. I did attach the image to this post.
It did use 256 colors to create the indexed image, and if you look at the 3-D color gamut with colorcloud() you should see 256 unique colors. It's just that when you tried to create an indexed image again from the RGB image, for some reason, it decided that only 36 colors were needed, not 60000 or 256. It's because of the algorithm it uses - Wu quant, or median cut, or whatever algorithm it uses. It's basically a clustering method and maybe some of the colors were so close together than it thought it could represent them with a single index. Like if 3 colors were really close to each other, it might decide that all 3 could be represented by (40, 38, 159) or whatever, and it assigned a single index to that color that represented all 3 colors. I think it's something like that.
Ok, thank you. Your anserws cleared all my doubts. haha

Sign in to comment.