Clear Filters
Clear Filters

How do I save the original image jpg from figure for colormap?

4 views (last 30 days)
Hi. I want ask a question.
How do I save the original image jpg from figure for colormap?
Here my codes:
I=imread('M_92_ar_2001743_st01_currentsize_200x200.jpg');
imshow(I);
imfinfo('M_92_ar_2001743_st01_currentsize_200x200.jpg');
I2=I(:,:,1);
imshow(I2);
map = [0, 0.3, 0
0, 0.4, 0
0, 0.5, 0
0, 0.6, 0
0, 0.8, 0
0, 1.0, 0];
imshow(I2);
colormap(map);
imwrite(I2,map,'cmalignant200.jpg');
  3 Comments
Dayangku Nur Faizah Pengiran Mohamad
I cannot save the figure colormap as image jpg. Thats the problem.
The codes imwrite(I2,map,'cmalignant200.jpg'); only save as I2 image jpg not the colormap image jpg in figure
DGM
DGM on 16 Nov 2021
imshow's implicit handling of colormaps is different than how ind2rgb or imwrite handles things, particularly when the image is integer-class. The nominal value range in a uint8 image is 0-255 (256 levels). When given the above syntax, imwrite() treats these pixel values as indices into the given colormap. The colormap you're giving it only has 6 entries; correspondingly, the vast majority of the output image will correspond to the last colormap entry.

Sign in to comment.

Accepted Answer

Dayangku Nur Faizah Pengiran Mohamad
Hello, I wanna to ask about other colors.
For example, I want to red colormap, so the codes is:
z = zeros(256,1);
map = [z linspace(256,0.3,0.3).' z]; ?
Is this correct codes for red colormap?
How about blue colormap?
  3 Comments
Dayangku Nur Faizah Pengiran Mohamad
Moved: DGM on 6 Dec 2022
Hi. What if I want to test yellow colormap? Is this the correct way to write the code?
map = [linspace(0.3,1,256).' ' z]; ?
DGM
DGM on 6 Dec 2022
Edited: DGM on 6 Dec 2022
% a test image
I = imread('cameraman.tif');
I = I(:,:,1);
% a 256-level yellow colormap from 0.3 to 1
z = zeros(256,1);
map = [linspace(0.3,1,256).' linspace(0.3,1,256).' z];
imshow(I,map)
... or you could generalize
% generate a linear colormap between any two color tuples
colorA = [0.3 0.3 0];
colorB = [1 1 0];
numcolors = 256;
map = interp1([1/numcolors 1],[colorA; colorB],(1:numcolors)/numcolors,'linear','extrap');
% apply it to an image
inpict = imread('cameraman.tif');
imshow(inpict,map)

Sign in to comment.

More Answers (1)

DGM
DGM on 16 Nov 2021
Edited: DGM on 16 Nov 2021
A few things.
  • Imwrite doesn't support indexed outputs for JPG files, so if you're trying to save the output as an indexed image, JPG won't work.
  • Your image ostensibly is uint8, and thus has 256 levels. Your colormap only has 6. You either need to quantize the image or use a longer colormap.
  • If all you want is an RGB image (all that JPG supports), and all you want is a strictly green colormap, then you don't even need to mess with a colormap or indexing.
% a test image
I = imread('cameraman.tif');
I = I(:,:,1);
% a 6-level green colormap from 0.3 to 1
z = zeros(6,1);
map = [z linspace(0.3,1,6).' z];
I6 = gray2ind(I,6); % quantize the image to 6 gray levels
imwrite(I6,map,'greencm1.jpg'); % write it
imshow(I6,map)
% a 256-level green colormap from 0.3 to 1
z = zeros(256,1);
map = [z linspace(0.3,1,256).' z];
imwrite(I,map,'greencm2.jpg'); % write it
clf; imshow(I,map)
% using some predefined colormap
imwrite(I,jet(256),'jetcm.jpg'); % write it
clf; imshow(I,jet(256))
% or just don't bother with colormaps if all you want is green
Igr = mat2gray(I)*0.7 + 0.3; % normalize, scale, offset
z = zeros(size(I));
Igr = cat(3,z,Igr,z); % green only
imwrite(Igr,'nocm.jpg'); % write it
imshow(Igr)

Community Treasure Hunt

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

Start Hunting!