How do I limit the range of a colormap when using imwrite

13 ビュー (過去 30 日間)
Michael Devereux
Michael Devereux 2018 年 4 月 23 日
コメント済み: Michael Devereux 2018 年 4 月 25 日
I want to save an image with imwrite and get the same output as the code below displays on screen
figure,imagesc(rhoi),
colormap(jet)
caxis([.45*min(rhoi(:)) 0*.55*max(rhoi(:))])
Is it possible to set a caxis property for imwrite?
imwrite(rhoi,jet,'out.png')
EDIT: I have included the full code. Basically, I want the last image RGB to look the same as that displayed by imagesc at each stage.
clear all
close all
clc
Inner = 0; % inner radius of the colour ring
rOuter = 150; % outer radius of the colour ring
[x, y] = meshgrid(-rOuter:.1:rOuter);
[theta, rho] = cart2pol(x, y);
rhoi=imcomplement(rho);
cmap=jet(256);
rhoi2=rhoi;
rhoi2=(rhoi2-min(rhoi2(:)))/(max(rhoi2(:))-min(rhoi2(:)));
figure;imagesc(rhoi2),colormap(cmap);
caxis([.55 .95])
rhoi2(rhoi2<.55) = .55;
rhoi2(rhoi2> .95) = .95;
figure,imagesc(rhoi2)
colormap( cmap);
X=uint8(256*rhoi2);
figure;imagesc(X),colormap( cmap);
RGB=ind2rgb(X,cmap);
figure;imshow(RGB)
  3 件のコメント
Michael Devereux
Michael Devereux 2018 年 4 月 24 日
I am scaling the matrix to a range of 0-255 and changing the range accordingly
Michael Devereux
Michael Devereux 2018 年 4 月 24 日
I have added the full code to demonstrate the problem

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 4 月 24 日
Before the call to ind2rgb (or imwrite), you need to rescale your matrix as colour indices so that the minimum is index 1 (for type double, 0 for type uint8) and maximum is index 256 (255 for uint8):
X = round(1 + (rhoi2 - min(rhoi2(:))) * 255 / (max(rhoi2(:)) - min(rhoi2(:)))); %using double
RGB = ind2rgb(X, cmap);
figure; imshow(RGB);
  1 件のコメント
Michael Devereux
Michael Devereux 2018 年 4 月 25 日
Thanks! This did what I need.

サインインしてコメントする。

その他の回答 (1 件)

Ahmet Cecen
Ahmet Cecen 2018 年 4 月 23 日
You should get the same effect if you just did:
rhoi(rhoi<.45*min(rhoi(:))) = .45*min(rhoi(:));
rhoi(rhoi> 0.55*max(rhoi(:)) = 0.55*max(rhoi(:));
  1 件のコメント
Michael Devereux
Michael Devereux 2018 年 4 月 24 日
Thanks! This works for viewing with imagesc without specifying caxis but does not work with imwrite or ind2rgb.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by