How do I save the original image jpg from figure for colormap?
4 views (last 30 days)
Show older comments
Dayangku Nur Faizah Pengiran Mohamad
on 16 Nov 2021
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
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.
Accepted Answer
Dayangku Nur Faizah Pengiran Mohamad
on 5 Dec 2021
3 Comments
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)
More Answers (1)
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)
See Also
Categories
Find more on Blue in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!