1 ## Copyright (C) 2006 Søren Hauberg
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2, or (at your option)
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ## General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this file. If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} @var{RGB} = label2rgb(@var{L})
18 ## @deftypefnx{Function File} @var{RGB} = label2rgb(@var{L}, @var{map})
19 ## @deftypefnx{Function File} @var{RGB} = label2rgb(@var{L}, @var{map}, @var{background})
20 ## @deftypefnx{Function File} @var{RGB} = label2rgb(@var{L}, @var{map}, @var{background}, @var{order})
21 ## Converts a labeled image to an RGB image.
23 ## label2rgb(@var{L}) returns a color image, where the background color
24 ## (the background is the zero-labeled pixels) is white, and all other
25 ## colors come from the @code{jet} colormap.
27 ## label2rgb(@var{L}, @var{map}) uses colors from the given colormap.
30 ## @item A string containing the name of a function to be called to
31 ## produce a colormap. The default value is "jet".
32 ## @item A handle to a function to be called to produce a colormap.
33 ## @item A @var{N}-by-3 colormap matrix.
36 ## label2rgb(@var{L}, @var{map}, @var{background}) sets the background
37 ## color. @var{background} can be a 3-vector corresponding to the wanted
38 ## RGB color, or one of the following strings
41 ## The background color will be blue.
43 ## The background color will be cyan.
45 ## The background color will be green.
47 ## The background color will be black.
49 ## The background color will be magenta.
51 ## The background color will be red.
53 ## The background color will be white. This is the default behavior.
55 ## The background color will be yellow.
58 ## label2rgb(@var{L}, @var{map}, @var{background}, @var{order}) allows for random
59 ## permutations of the colormap. @var{order} must be one of the following strings
62 ## The colormap is not permuted in any ways. This is the default.
64 ## The used colormap is permuted randomly.
66 ## @seealso{bwlabel, ind2rgb}
69 function rgb = label2rgb(L, map = "jet", background = "w", order = "noshuffle")
74 if ( !ismatrix(L) || ndims(L) != 2 || any(L(:) != round(L(:))) || any(L(:) < 0) )
75 error("label2rgb: first input argument must be a labelled image");
77 if ( !ischar(map) && !isa(map, "function_handle") && !(ismatrix(map) && ndims(map)==2 && columns(map)==3) )
78 error("label2rgb: second input argument must be a color map or a function that can generate a colormap");
80 if ( !ischar(background) && !(isreal(background) && numel(background)==3) )
81 error("label2rgb: third input argument must be a color given as a string or a 3-vector");
83 if ( !any(strcmpi(order, {"noshuffle", "shuffle"})) )
84 error("label2rgb: fourth input argument must be either 'noshuffle' or 'shuffle'");
87 ## Convert map to a matrix if needed
88 num_objects = max(L(:));
89 if (ischar(map) || isa(map, "function_handle"))
90 map = feval(map, num_objects+1);
93 num_colors = rows(map);
94 if (num_objects > num_colors)
95 warning("label2rgb: number of objects exceeds number of colors in the colormap");
98 ## Handle the background color
99 if (ischar(background))
100 switch (background(1))
101 case 'b' background = [0, 0, 1];
102 case 'c' background = [0, 1, 1];
103 case 'g' background = [0, 1, 0];
104 case 'k' background = [0, 0, 0];
105 case 'm' background = [1, 0, 1];
106 case 'r' background = [1, 0, 0];
107 case 'w' background = [1, 1, 1];
108 case 'y' background = [1, 1, 0];
110 error("label2rgb: unknown background color '%s'", background);
113 background = background(:)';
114 if (min(background) < 0 || max(background) > 1)
115 error("label2rgb: the background color must be in the interval [0, 1]");
118 ## Should we shuffle the colormap?
119 if (strcmpi(order, "shuffle"))
121 map = map(randperm(r),:);
124 ## If the background color is in the color map: remove it
125 idx = find((map(:,1) == background(1)) & (map(:,2) == background(2)) & (map(:,3) == background(3)));
130 ## Insert the background color as the first element in the color map
131 map = [background; map];
133 ## Convert L to an RGB image
134 rgb = ind2rgb(L+1, map);
136 rgb = uint8(255*rgb);