-
Notifications
You must be signed in to change notification settings - Fork 5
/
intensity2rgb.m
94 lines (82 loc) · 2.92 KB
/
intensity2rgb.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
% convert a 2D intensity image to RGB values according to colormap cmap.
%
% imshow(im) produces identical graphical output to imagesc(mat) if the
% colormap contains the same number of entries as the number of unique
% intensities in mat. If this is not the case there will be minor
% divergences due to different interpolation, but for any sensible
% colormap these divergences will be very small. To minimise any confusion
% it is always safest to use this function with colorbarbetter when
% plotting.
%
% INPUT DEFAULT DEFINITION
% mat - some 2d numerical matrix
% cmap cmap_bwr color map
% limits [] clipping limits for map (2 element vector). If
% undefined we use the data limits. If set to
% 'symmetrical', we balance the positive and
% negative color range by setting the limits to
% [-absmax +absmax]. If 'zerobounded', we set the
% limits to [0,max] (and raise exception if max is
% negative).
% greythresh -Inf values below this threshold are converted to
% greyscale
%
% [im,intmap,cmap] = intensity2rgb(mat,[cmap],[limits],[greythresh])
function [im,intmap,cmap] = intensity2rgb(mat,cmap,limits,greythresh)
assert(ndims(mat)==2,'input mat must be 2d, got %dd',ndims(mat))
if ieNotDefined('greythresh')
% no gray scale
greythresh = -Inf;
end
% get all unique intensities
umat = unique(mat(:));
% (stripping nans)
umat(isnan(umat)) = [];
nu = length(umat);
if ieNotDefined('cmap')
cmap = cmap_bwr;
end
if ieNotDefined('limits')
limits = [min(mat(:)) max(mat(:))];
elseif strcmp(limits,'symmetrical')
m = max(abs(mat(:)));
limits = [-m m];
elseif strcmp(limits,'zerobounded')
m = max(mat(:));
assert(m>0,...
'zerobounded is only possible if there are positive values');
limits = [0 m];
end
if ieNotDefined('nancolor')
nancolor = [1 1 1];
end
ncolor = size(cmap,1);
% these are the intensities in the colour map. We want whatever is closest
intmap = linspace(limits(1),limits(2),ncolor);
% greyscale and muted colormap
gmap = repmat(mean(cmap,2),[1 3]) * .9;
cmap(intmap<greythresh,:) = gmap(intmap<greythresh,:);
% put in final, white entry to represent NaNs
cmap(end+1,:) = [1 1 1];
% convert the image intensities to the range of the color map indices
% matscaled = round(normalizerange(mat,1,ncolor));
% index final, white color for NaNs
% nanmask = isnan(mat);
% matscaled(nanmask) = ncolor+1;
im = zeros([size(mat) 3]);
nanmat = isnan(mat);
for r = 1:size(mat,1)
for c = 1:size(mat,2)
% closest colour
if nanmat(r,c)
ind = ncolor+1;
else
[~,ind] = min(abs(mat(r,c)-intmap));
end
im(r,c,:) = cmap(ind,:);
end
end
if nargout > 2
% also return the colormap, but without the NaN entry
cmap(end,:) = [];
end