How to save image with custom colormap (imwrite problems)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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
Accepted Answer
Image Analyst
on 18 Apr 2016
1 vote
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 Comments
gapsna
on 18 Apr 2016
sorry, I was using imwrite, not imsave. imwrite is the one that saves with the wrong colors
Image Analyst
on 18 Apr 2016
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);
gapsna
on 18 Apr 2016
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)
Image Analyst
on 18 Apr 2016
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.
gapsna
on 19 Apr 2016
I thought I was doing something wrong.
does the saved image in your computer looks in all the colors?
Image Analyst
on 19 Apr 2016
For the indexed image, not with other programs, unless I convert it to RGB then it does.
gapsna
on 21 Apr 2016
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.
gapsna
on 28 Apr 2016
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
Image Analyst
on 28 Apr 2016
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.
gapsna
on 30 Apr 2016
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.
Image Analyst
on 1 May 2016
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)
gapsna
on 2 May 2016
It workeeeed!
Thank you very much for the help!! you are awesome!!
More Answers (0)
Categories
Find more on Red in Help Center and File Exchange
Products
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)