How do I save the original image jpg from figure for colormap?
8 次查看(过去 30 天)
显示 更早的评论
Dayangku Nur Faizah Pengiran Mohamad
2021-11-16
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 个评论
DGM
2021-11-16
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.
采纳的回答
Dayangku Nur Faizah Pengiran Mohamad
2021-12-5
3 个评论
DGM
2022-12-6
编辑:DGM
2022-12-6
% 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)
更多回答(1 个)
DGM
2021-11-16
编辑:DGM
2021-11-16
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Blue 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!