How do I give the spectrogram a colormap option and save it as it is when plotted?

조회 수: 45 (최근 30일)
Here's my code
% feature_size :[257,7]
figure(1)
imagesc( some_image_smaple )
colormap(jet(128))
set(gca,'YDir','normal')
above code shows like this :
But when I try to save this feature, if I use the code below,
cmap=jet(128);
imwrite( some_image_sample, cmap, 'sample.jpg');
it shows like this :
I'm not sure what's wrong, but I'd appreciate it if you let me know.

채택된 답변

Nagasai Bharat
Nagasai Bharat 2020년 8월 27일
편집: Nagasai Bharat 2020년 8월 28일
Hi,
In general, “imwrite” save an image which is of the format RGB. So, there is necessity to change the image displayed by “imagesc” (which scales the image such that the full range of colormap) in order to save the same image. This change is such that the normalized image array with colormap indices should be passed into imwrite function.
The following code solves your issue.
figure(1)
im = imagesc(some_image_matrix);
colormap(jet(128))
set(gca,'YDir','normal')
cmap=jet(128);
img_o = im.CData; % im is an image object and im.CData gives the image matrix
min_ = min(img_o(:));
max_ = max(img_o(:));
imgr = round((single((im.CData)-min_)./max_-min_)*128); % normalized and converted into colormap indices
img1 = ind2rgb(imgr,cmap); % Converts the indexed array into rgb format
imwrite(flip(img1), 'sample.jpg'); % flip is used as 'imagesc' inverts the axes when displayed
  댓글 수: 2
jinwoo lee
jinwoo lee 2020년 8월 28일
Actually, Some_image_sample I mentioned above is a spectrogram.
I've tried using your code, but it doesn't seem to work.
Before you reply, I succeeded in saving the image with below code.
Could it be the code that has the same meaning as you wrote?
colormap = jet(128);
% some_image_matrix shpae = [1,1,n,m]
im = ind2rgb ( im2uint8 ( rescale ( squeeze( some_image_matrix ))) , colormap);
imwrite(flip(im), 'sample.jpg')
Thanks to reply.
Nagasai Bharat
Nagasai Bharat 2020년 8월 28일
편집: Nagasai Bharat 2020년 8월 28일
Hi,
One small change is instead of 249 divide it by max_-min_.I have changed it in the code try to execute it again. It is the the same as rescale operation. Only difference in your code and mine is you have converted the value to 0 - 255 whereas I have done into 0 - 127 which is much more inlined with the colormap of 128 levels.
Otherthan that both have similar results. Please do let me know the how code is not working.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by