Skip to content

Commit f533ac8

Browse files
committed
add double2rgb.m
1 parent 65733bf commit f533ac8

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

src/@Image/double2rgb.m

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
function rgb = double2rgb(obj, map, bounds, varargin)
2+
% Create a RGB image from values of an intensity image.
3+
%
4+
% RGB = double2rgb(IMG, MAP, BOUNDS)
5+
% Scales the values in IMG between in the interval specified by BOUNDS,
6+
% then convert each value to the corresponding value of the color map
7+
% given in MAP.
8+
% If the image contains inf or NaN values, they are set to white.
9+
%
10+
% RGB = double2rgb(IMG, MAP)
11+
% Assumes extreme values are given by extreme values in image. Only
12+
% finite values are used for computing bounds.
13+
%
14+
% RGB = double2rgb(IMG, MAP, BOUNDS, BACKGROUND)
15+
% Specifies the value of the background value for Nan and Inf values in
16+
% IMG. BACKGROUND should be either a 1-by-3 row vector with values
17+
% between 0 and 1, or one of the color shortcuts 'r', 'b', 'g', 'c', 'y',
18+
% 'm', 'k', 'w'.
19+
%
20+
% Example
21+
% % Distance map of a binary image
22+
% img = Image.read('circles.png');
23+
% dist = distanceMap(img);
24+
% dist(img) = NaN;
25+
% rgb = double2rgb(dist, 'parula', [], 'w');
26+
% show(rgb);
27+
%
28+
% See also
29+
% label2rgb, rgb2hsv
30+
%
31+
32+
% ------
33+
% Author: David Legland
34+
% e-mail: david.legland@inrae.fr
35+
% Created: 2020-01-14, using Matlab 7.9.0.529 (R2009b)
36+
% Copyright 2010 INRA - Cepia Software Platform.
37+
38+
% HISTORY
39+
40+
if nargin < 2 || isempty(map)
41+
map = 'parula';
42+
end
43+
44+
% ensure map is a numeric array
45+
if ischar(map)
46+
map = feval(map, 256); %#ok<FVAL>
47+
end
48+
49+
% extract background value
50+
bgColor = [1 1 1];
51+
if ~isempty(varargin)
52+
bgColor = parseColor(varargin{1});
53+
end
54+
55+
% get valid pixels (finite value)
56+
valid = isfinite(obj.Data);
57+
if ~exist('bounds', 'var') || isempty(bounds)
58+
bounds = [min(obj.Data(valid)) max(obj.Data(valid))];
59+
end
60+
61+
% convert finite values to indices between 1 and map length
62+
n = size(map, 1);
63+
inds = (obj.Data(valid) - bounds(1)) / (bounds(end) - bounds(1)) * (n-1);
64+
inds = floor(min(max(inds, 0), n-1))+1;
65+
66+
% compute the 3 bands
67+
dim = size(obj);
68+
r = ones(dim) * bgColor(1); r(valid) = map(inds, 1);
69+
g = ones(dim) * bgColor(2); g(valid) = map(inds, 2);
70+
b = ones(dim) * bgColor(3); b(valid) = map(inds, 3);
71+
72+
% concatenate the 3 bands to form an rgb image
73+
if length(dim) == 2
74+
% case of 2D image
75+
rgb = cat(3, r, g, b);
76+
77+
else
78+
% case of 3D image: need to play with channels
79+
dim2 = [dim(1:2) 3 dim(3:end)];
80+
rgb = zeros(dim2, class(map));
81+
rgb(:,:,1,:) = r;
82+
rgb(:,:,2,:) = g;
83+
rgb(:,:,3,:) = b;
84+
end
85+
86+
newName = '';
87+
if ~isempty(obj.Name)
88+
newName = sprintf('label2rgb(%s)', obj.Name);
89+
end
90+
91+
% create Image object from data
92+
rgb = Image(permute(rgb, [2 1 3 4:length(dim)]), 'Type', 'color', ...
93+
'Parent', obj, ...
94+
'Name', newName);
95+
96+
function color = parseColor(color)
97+
98+
if ischar(color)
99+
switch(color)
100+
case 'k'
101+
color = [0 0 0];
102+
case 'w'
103+
color = [1 1 1];
104+
case 'r'
105+
color = [1 0 0];
106+
case 'g'
107+
color = [0 1 0];
108+
case 'b'
109+
color = [0 0 1];
110+
case 'c'
111+
color = [0 1 1];
112+
case 'm'
113+
color = [1 0 1];
114+
case 'y'
115+
color = [1 1 0];
116+
otherwise
117+
error('Unknown color string');
118+
end
119+
end

0 commit comments

Comments
 (0)