wolffd@0
|
1 function [color,best,kmeans]=som_kmeanscolor(sM,C,initRGB,contrast)
|
wolffd@0
|
2
|
wolffd@0
|
3 % SOM_KMEANSCOLOR Map unit color code according to K-means clustering
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % [color, best, kmeans] = som_kmeanscolor(sM, C, [initRGB],[contrast])
|
wolffd@0
|
6 %
|
wolffd@0
|
7 % color = som_kmeanscolor(sM,15,som_colorcode(sM,'rgb1'),'enhance');
|
wolffd@0
|
8 % [color,best] = som_kmeanscolor(sM,15,[],'normal');
|
wolffd@0
|
9 %
|
wolffd@0
|
10 % Input and output arguments ([]'s are optional):
|
wolffd@0
|
11 % sM (struct) map struct
|
wolffd@0
|
12 % C (scalar) maximum number of clusters
|
wolffd@0
|
13 % initRGB (string, matrix) color code string accepted by SOM_COLORCODE
|
wolffd@0
|
14 % or an Mx3 matrix of RGB triples, where M is the number
|
wolffd@0
|
15 % of map units. Default: SOM_COLORCODEs default
|
wolffd@0
|
16 % contrast (string) 'flat', 'enhanced' color contrast mode, default:
|
wolffd@0
|
17 % 'enhanced'
|
wolffd@0
|
18 %
|
wolffd@0
|
19 % color (matrix) MxCx3 of RGB triples
|
wolffd@0
|
20 % best (scalar) index for "best" clustering according to
|
wolffd@0
|
21 % Davies-Boulding index; color(:,:,best) includes the
|
wolffd@0
|
22 % corresponding color code.
|
wolffd@0
|
23 % kmeans (cell) output of KMEANS_CLUSTERS in a cell array.
|
wolffd@0
|
24 %
|
wolffd@0
|
25 % The function gives a set of color codings according to K-means
|
wolffd@0
|
26 % clustering. For clustering, it uses function KMEANS_CLUSTERS for map units,
|
wolffd@0
|
27 % and it calculates color codings for 1,2,...,C clusters.
|
wolffd@0
|
28 % The idea of coloring is that the color of a cluster is the mean of the
|
wolffd@0
|
29 % original colors (RGB values) of the map units belonging to that cluster,
|
wolffd@0
|
30 % see SOM_CLUSTERCOLOR. The original colors are defined by SOM_COLORCODE
|
wolffd@0
|
31 % by default. Input 'contrast' simply specifies whether or not
|
wolffd@0
|
32 % to linearly redistribute R,G, and B values so that minimum is 0 and
|
wolffd@0
|
33 % maximum 1 ('enahanced') or to use directly the output of
|
wolffd@0
|
34 % SOM_CLUSTERCOLOR ('flat'). KMEANS_CLUSTERS uses certain heuristics to
|
wolffd@0
|
35 % select the best of 5 trials for each number of clusters. Evaluating the
|
wolffd@0
|
36 % clustering multiple times may take some time.
|
wolffd@0
|
37 %
|
wolffd@0
|
38 % EXAMPLE
|
wolffd@0
|
39 %
|
wolffd@0
|
40 % load iris; % or any other map struct sM
|
wolffd@0
|
41 % [color,b]=som_kmeanscolor(sM,10);
|
wolffd@0
|
42 % som_show(sM,'color',color,'color',{color(:,:,b),'"Best clustering"');
|
wolffd@0
|
43 %
|
wolffd@0
|
44 % See also SOM_SHOW, SOM_COLORCODE, SOM_CLUSTERCOLOR, KMEANS_CLUSTERS
|
wolffd@0
|
45
|
wolffd@0
|
46 % Contributed to SOM Toolbox 2.0, April 1st, 2000 by Johan Himberg
|
wolffd@0
|
47 % Copyright (c) by Johan Himberg
|
wolffd@0
|
48 % http://www.cis.hut.fi/projects/somtoolbox/
|
wolffd@0
|
49
|
wolffd@0
|
50 % corrected help text 11032005 johan
|
wolffd@0
|
51
|
wolffd@0
|
52 %%% Check number of inputs
|
wolffd@0
|
53
|
wolffd@0
|
54 error(nargchk(2, 4, nargin)); % check no. of input args
|
wolffd@0
|
55
|
wolffd@0
|
56 %%% Check input args & set defaults
|
wolffd@0
|
57
|
wolffd@0
|
58 if isstruct(sM) & isfield(sM,'type') & strcmp(sM.type,'som_map'),
|
wolffd@0
|
59 [tmp,lattice,msize]=vis_planeGetArgs(sM);
|
wolffd@0
|
60 munits=prod(msize);
|
wolffd@0
|
61 if length(msize)>2
|
wolffd@0
|
62 error('Does not work with 3D maps.')
|
wolffd@0
|
63 end
|
wolffd@0
|
64 else
|
wolffd@0
|
65 error('Map struct requires for first input argument!');
|
wolffd@0
|
66 end
|
wolffd@0
|
67
|
wolffd@0
|
68 if ~vis_valuetype(C,{'1x1'}),
|
wolffd@0
|
69 error('Scalar value expect for maximum number of clusters.');
|
wolffd@0
|
70 end
|
wolffd@0
|
71
|
wolffd@0
|
72 % check initial color coding
|
wolffd@0
|
73 if nargin<3 | isempty(initRGB)
|
wolffd@0
|
74 initRGB=som_colorcode(sM);
|
wolffd@0
|
75 end
|
wolffd@0
|
76
|
wolffd@0
|
77 % check contrast checking
|
wolffd@0
|
78 if nargin<4 | isempty(contrast),
|
wolffd@0
|
79 contrast='enhanced';
|
wolffd@0
|
80 end
|
wolffd@0
|
81
|
wolffd@0
|
82 if ~ischar(contrast),
|
wolffd@0
|
83 error('String input expected for input arg. ''contrast''.');
|
wolffd@0
|
84 else
|
wolffd@0
|
85 switch lower(contrast)
|
wolffd@0
|
86 case {'flat','enhanced'}
|
wolffd@0
|
87 ;
|
wolffd@0
|
88 otherwise
|
wolffd@0
|
89 error(['''flat'' or ''enhanced'' expected for '...
|
wolffd@0
|
90 'input argument ''contrast''.']);
|
wolffd@0
|
91 end
|
wolffd@0
|
92 end
|
wolffd@0
|
93
|
wolffd@0
|
94 if ischar(initRGB),
|
wolffd@0
|
95 try
|
wolffd@0
|
96 initRGB=som_colorcode(sM,initRGB);
|
wolffd@0
|
97 catch
|
wolffd@0
|
98 error(['Color code ' initRGB ...
|
wolffd@0
|
99 'was not recognized by SOM_COLORCODE.']);
|
wolffd@0
|
100 end
|
wolffd@0
|
101 elseif vis_valuetype(initRGB,{'nx3rgb',[munits 3]},'all'),
|
wolffd@0
|
102 ;
|
wolffd@0
|
103 else
|
wolffd@0
|
104 error(['The initial color code must be a string '...
|
wolffd@0
|
105 'or an Mx3 matrix of RGB triples.']);
|
wolffd@0
|
106 end
|
wolffd@0
|
107
|
wolffd@0
|
108 %%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
109
|
wolffd@0
|
110 disp('Wait...');
|
wolffd@0
|
111 [c,p,err,ind]=kmeans_clusters(sM,C,5,0); % use 5 trials, verbose off
|
wolffd@0
|
112
|
wolffd@0
|
113 % Store outputs to kmeans
|
wolffd@0
|
114 kmeans{1}=c;
|
wolffd@0
|
115 kmeans{2}=p;
|
wolffd@0
|
116 kmeans{3}=err;
|
wolffd@0
|
117 kmeans{4}=ind;
|
wolffd@0
|
118
|
wolffd@0
|
119 %%% Build output
|
wolffd@0
|
120 color=som_clustercolor(sM,cat(2,p{:}),initRGB);
|
wolffd@0
|
121 [tmp,best]=min(ind);
|
wolffd@0
|
122
|
wolffd@0
|
123 switch contrast
|
wolffd@0
|
124 case 'flat'
|
wolffd@0
|
125 ;
|
wolffd@0
|
126 case 'enhanced'
|
wolffd@0
|
127 warning off;
|
wolffd@0
|
128 ncolor=maxnorm(color);
|
wolffd@0
|
129 ncolor(~isfinite(ncolor))=color(~isfinite(ncolor));
|
wolffd@0
|
130 color=ncolor;
|
wolffd@0
|
131 warning on;
|
wolffd@0
|
132 end
|
wolffd@0
|
133
|
wolffd@0
|
134 %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
135 function X=maxnorm(x)
|
wolffd@0
|
136 % normalize columns of x between [0,1]
|
wolffd@0
|
137
|
wolffd@0
|
138 x=x-repmat(min(x),[size(x,1) 1 1]);
|
wolffd@0
|
139 X=x./repmat(max(x),[size(x,1) 1 1]);
|