Create colormap to represent value intervals [0 1]

18 views (last 30 days)
Hi all,
I have been struggling with this for a while and seriously would appreciate any help...
So I have a matrix size [192 192 10] with values between [0 1], outside of the ROI I have values of NaN. 80% of the pixel values are within [0 0.4]. I'm trying different threshold to display the matrix and would typically like the following:
NaN = White
< 0.05 = red
[0.05 0.2] = yellow
> 0.2 = green
The colormapeditor will not allow me to select exact value ie 0.05 and 0.2, instead it only fits in figures like 0.04994 and the next one is 0.05034. Is there anyway I can manually change this?
Many thanks!

Accepted Answer

Shunichi Kusano
Shunichi Kusano on 11 Sep 2019
Hi Fiona,
how about labeling image? The following code is the example.
%% image load
img = double(rgb2gray(imread('coloredChips.png'))) / 255.;
img(randperm(numel(img), round(numel(img)/100))) = NaN;
figure;
imshow(img);
%% labeling
label = zeros(size(img), 'uint8');
label(~finite(img)) = 0;
label(img < 0.05) = 1;
label(img >= 0.05 & img <= 0.2) = 2;
label(img > 0.2) = 3;
figure;
imshow(label, []); colormap(jet);
%% make colormap
map = [1 1 1; % white for 0
1 0 0; % red for 1
1 1 0; % yellow for 2
0 1 0 ]; % green for 3
%% converting to rgb image
rgb = ind2rgb(label, map);
figure;
imshow(rgb);
Hope this helps.
  5 Comments
Shunichi Kusano
Shunichi Kusano on 18 Sep 2019
Hi Fiona,
sorry for late reply. In the case, you need to make custom colormap. Please be careful that the range of colormap (caxis) and the row number of colormap must be selected so that the separation values match the values you want to change color (in this case 0.05 and 0.2).
Please try this code to your image.
% dummy image
img = double(rgb2gray(imread('coloredChips.png'))) / 255.;
img(randperm(numel(img), round(numel(img)/100))) = NaN;
%% create colormap
mycolormap = zeros(20,3); % 20 comes from 1 / 0.05
mycolormap(1,1) = 1; % red for < 0.05
mycolormap(2:4,1:2) = 1; % yellow for >=0.05 and <0.2
mycolormap(5:end,2) = 1; % green for => 0.2
%% display
s = imshow(img);
colormap(mycolormap);
caxis([0 1]); % colormap range
s.AlphaData = finite(img); % NaN to transparent (white)
One concern is that this procedure does not control "include or not include separation values". I mean, in this case, red is for < 0.05, yellow for >=0.05 AND < 0.2, green for >= 0.2. Is this accpeptable?
Fiona Gong
Fiona Gong on 18 Sep 2019
That's brilliant thanks so much Shunichi, the method works really well. It looks like I need NaNs to be white rather than transparent but I'm sure I can sort this out. Thanks!!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!