How do I save the original image jpg from figure for colormap?
조회 수: 2 (최근 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!