MATLAB Answers

0

Create colormap to represent value intervals [0 1]

Asked by Fiona Gong on 11 Sep 2019
Latest activity Commented on by Fiona Gong on 18 Sep 2019
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!

  0 Comments

Sign in to comment.

1 Answer

Answer by Shunichi Kusano on 11 Sep 2019
 Accepted Answer

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

Hi Shunichi,
Your method works really well, dealt with NaN, also dealt with the colormap issue I had. Also the data I had is indeed 2D slice and 10 slice in total, I don't need a volum show at the moment. The only problem is that in the figure I produced, there's no original value so when I need to find the exact value of the matrix, there's nothing there apart from the label values. I was hoping I could create a colormap which shows all the data range with the color I need, as well as retaining the original value when we need to look at it. Is there a way around for this?
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?
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.