How to save image with custom colormap (imwrite problems)

Hello everyone, I am trying to save a large matrix who's cells contain one of 6 possible values (0, 50, 100, 150, 200, 255)(if needed those values can be changed). I also have a 6 by 3 matrix as color map: cmap=[153 76 0; 0 128 255; 96 96 96; 224 224 224; 255 255 255; 0 0 0]/255; (those colors need to stay as they are).
I try to save the image like this
imwrite(matrix, cmap, 'name.png');
but the image is saved with only two colors. I have tried different image types and different rang values for the matrix but no solution.
Can anyone help me with this?
Thank you very much.
Cheers,
Camilo

 Réponse acceptée

Image Analyst
Image Analyst le 18 Avr 2016
Try imwrite() and see if that works. Use imread() to see if the saved colormap comes back with only 2 colors, or all 6.

12 commentaires

sorry, I was using imwrite, not imsave. imwrite is the one that saves with the wrong colors
Sorry, I'm not able to reproduce. This works just fine for me - I'm getting back all 6 colors.
indexedImage = [0, 50, 100; 150, 200, 255]
cmap=[153 76 0; 0 128 255; 96 96 96; 224 224 224; 255 255 255; 0 0 0]/255
imshow(indexedImage, 'InitialMagnification', 16000);
title('Original Indexed Image', 'FontSize', 30);
colormap(cmap);
caxis([0, 255]);
colorbar;
axis on;
% Save the indexed image.
filename = 'delete_me.png';
imwrite(indexedImage, cmap, filename);
[recalledImage, recalledColorMap] = imread(filename);
figure;
imshow(indexedImage, 'InitialMagnification', 16000);
title('Recalled Image', 'FontSize', 30);
% Cehck to see if we recalled all 6 colors.
whos cmap
% Display it the same way.
colormap(cmap);
caxis([0, 255]);
colorbar;
axis on;
% Get rid of temporary image.
delete(filename);
Thanks for the help. The recalled image from your code (i.e. opened with matlab) looks fine, but the saved image (opened with any program like windows photo viewer) appears only in two of the colors (first pixel brown, the rest black)
It's not MATLAB's fault if other programs do not know how to handle the stored color map. In that case you should probably not save as an indexed image but convert to an rgb image with ind2rgb() before saving, and then just save the RGB image instead.
I thought I was doing something wrong.
does the saved image in your computer looks in all the colors?
For the indexed image, not with other programs, unless I convert it to RGB then it does.
I'm trying like this: cmap=[153 76 0; 0 128 255; 96 96 96; 224 224 224; 255 255 255; 0 0 0]/255; cs_save = ind2rgb(cs_plot,cmap);
imwrite(cs_save,'test_rgb.png');
cs_plot is a 8051x7941 uint8 matrix with the values I previously talked about. Even if I use imshow(cs_save) appears all black except for the brown color.
still having problems with this. My tutor needs the have the image saved in colors, and they have to be visible in other programs (like windows image viewer) :S
I suspect your cs_plot is not a 2D uint8 or uint16 image. What does this show:
whos cs_plot
Can you save that into a mat file and attach it here?
save('cs_plot.mat', 'cs_plot');
Attach the cs_plot.mat file it makes to your next comment.
Hi, Thanks for your help, And sorry for the late reply, I was away. This is cs_plot:
Name Size Bytes Class Attributes
cs_plot 8051x7941 63932991 uint8
Attached file is what I get with imwrite(cs_save,[pth num2str(clr_pct) 'test_rgb.png']);
and the matrix is also attached.
You need to use a different colormap when you're creating the RGB image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s= load('cs_plot.mat')
cs_plot = s.cs_plot;
whos cs_plot
subplot(3, 2, 1);
imshow(cs_plot);
axis on;
title('Indexed Image with no colormap', 'FontSize', fontSize);
unique(cs_plot(:))
subplot(3, 2, 2);
histogram(cs_plot);
grid on;
title('Histogram', 'FontSize', fontSize);
cmap=[153 76 0; 0 128 255; 96 96 96; 224 224 224; 255 255 255; 0 0 0]/255;
h3 = subplot(3, 2, 3);
imshow(cs_plot);
title('Indexed Image', 'FontSize', fontSize);
colormap(h3, cmap);
colorbar
drawnow;
% Get an RGB image
colorMapForSaving = imresize(cmap,[256, 3], 'nearest');
rgbImage = ind2rgb(cs_plot, colorMapForSaving);
h4 = subplot(3, 2, 4);
imshow(rgbImage);
title('RGB Image', 'FontSize', fontSize);
drawnow;
filename = 'delete_me.png'
imwrite(rgbImage, filename);
% Recall from disk and see if it's okay.
rgbImage = imread(filename);
h4 = subplot(3, 2, 5);
imshow(rgbImage);
title('Recalled Image', 'FontSize', fontSize);
% Delete temporary file
delete(filename)
It workeeeed!
Thank you very much for the help!! you are awesome!!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by