]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/label2rgb.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / label2rgb.m
1 ## Copyright (C) 2006  Søren Hauberg
2 ## 
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)
6 ## any later version.
7 ## 
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. 
12 ## 
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/>.
15
16 ## -*- texinfo -*-
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.
22 ##
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.
26 ##
27 ## label2rgb(@var{L}, @var{map}) uses colors from the given colormap.
28 ## @var{map} can be
29 ## @itemize
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.
34 ## @end itemize
35 ##
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
39 ## @table @samp
40 ## @item "b"
41 ## The background color will be blue.
42 ## @item "c"
43 ## The background color will be cyan.
44 ## @item "g"
45 ## The background color will be green.
46 ## @item "k"
47 ## The background color will be black.
48 ## @item "m"
49 ## The background color will be magenta.
50 ## @item "r"
51 ## The background color will be red.
52 ## @item "w"
53 ## The background color will be white. This is the default behavior.
54 ## @item "y"
55 ## The background color will be yellow.
56 ## @end table
57 ##
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
60 ## @table @samp
61 ## @item "noshuffle"
62 ## The colormap is not permuted in any ways. This is the default.
63 ## @item "shuffle"
64 ## The used colormap is permuted randomly.
65 ## @end table
66 ## @seealso{bwlabel, ind2rgb}
67 ## @end deftypefn
68
69 function rgb = label2rgb(L, map = "jet", background = "w", order = "noshuffle")
70   ## Input checking
71   if (nargin < 1)
72     print_usage();
73   endif
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");
76   endif
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");
79   endif
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");
82   endif
83   if ( !any(strcmpi(order, {"noshuffle", "shuffle"})) )
84     error("label2rgb: fourth input argument must be either 'noshuffle' or 'shuffle'");
85   endif
86   
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);
91   endif
92
93   num_colors  = rows(map);
94   if (num_objects > num_colors)
95     warning("label2rgb: number of objects exceeds number of colors in the colormap");
96   endif
97
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];
109       otherwise
110         error("label2rgb: unknown background color '%s'", background);
111     endswitch
112   endif
113   background = background(:)';
114   if (min(background) < 0 || max(background) > 1)
115     error("label2rgb: the background color must be in the interval [0, 1]");
116   endif
117   
118   ## Should we shuffle the colormap?
119   if (strcmpi(order, "shuffle"))
120     r = rows(map);
121     map = map(randperm(r),:);
122   endif
123   
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)));
126   if (!isempty(idx))
127     map(idx, :) = [];
128   endif
129   
130   ## Insert the background color as the first element in the color map
131   map = [background; map];
132   
133   ## Convert L to an RGB image
134   rgb = ind2rgb(L+1, map);
135   rgb /= max(rgb(:));
136   rgb = uint8(255*rgb);
137 endfunction