필터 지우기
필터 지우기

How to save image with custom colormap (imwrite problems)

조회 수: 21 (최근 30일)
gapsna
gapsna 2016년 4월 18일
댓글: gapsna 2016년 5월 2일
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

채택된 답변

Image Analyst
Image Analyst 2016년 4월 18일
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
Image Analyst
Image Analyst 2016년 5월 1일
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
gapsna 2016년 5월 2일
It workeeeed!
Thank you very much for the help!! you are awesome!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Red에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by