Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_clustercolor.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function color=som_clustercolor(m, class, colorcode) | |
2 | |
3 % SOM_CLUSTERCOLOR Sets map unit coloring according to classification | |
4 % | |
5 % syntax 1: color = som_clustercolor(m, class, [colorcode]) | |
6 % syntax 2: color = som_clustercolor(class, colormatrix) | |
7 % | |
8 % Input and output arguments ([]'s are optional): | |
9 % m (struct) map or topol struct | |
10 % (cell array) of form {str,[m1 m2]} where str = 'hexa' | |
11 % or 'rect' and [m1 m2] = msize. | |
12 % class (matrix) Mxn matrix of integers (class labels) | |
13 % where M is the number of map units and each | |
14 % column gives some classification for the units. | |
15 % colorcode (string) 'rgb1', 'rgb2' (default), 'rgb3', 'rgb4', 'hsv'. | |
16 % colormatrix (matrix) Mx3 matrix of RGB triplets giving the | |
17 % initial color code for each unit. | |
18 % color (matrix) size Mx3xn of RGB triplets giving the | |
19 % resulting color code for each unit | |
20 % | |
21 % The function gives a color coding by class and location for the | |
22 % map units. The color is determined by calculating the mean of the | |
23 % initial RGB values of units belonging to the same class. | |
24 % | |
25 % Function has two syntaxes: | |
26 % | |
27 % * If first argument gives the map topology, i.e. is map or topol struct | |
28 % or cell indicating the topology, the initial color coding of the | |
29 % units may be given by a string ('rgb1','rgb2','rgb3','rgb4', or 'hsv') | |
30 % which describe a predefined coloring scheme. (see SOM_COLORCODE). | |
31 % or an initial color matrix of size Mx3 with RGB triplets as rows. | |
32 % * Another possibility is to give just the classification vector | |
33 % of size Mx1 and an initial color matrix of size Mx3 with RGB | |
34 % triplets as rows. | |
35 % | |
36 % EXAMPLE (requires Matlab Statistics Toolbox) | |
37 % | |
38 % % Do a 10-cluster single linkage hierachical clustering for SOM units | |
39 % class=cluster(linkage(pdist(sM.codebook),'single'),10); | |
40 % % Color code the clusters | |
41 % C=som_clustercolor(sM, class, 'rgb2'); | |
42 % % Visualize | |
43 % som_show(sM,'color',C); | |
44 % | |
45 % See also SOM_COLORCODE, SOM_KMEANSCOLOR, SOM_CPLANE, SOM_SHOW | |
46 | |
47 % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Johan Himberg | |
48 % Copyright (c) by Johan Himberg | |
49 % http://www.cis.hut.fi/projects/somtoolbox/ | |
50 | |
51 % Version 2.0beta Johan 100200 | |
52 | |
53 %%% Check arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
54 | |
55 error(nargchk(2, 3, nargin)); % check no. of input args is correct | |
56 | |
57 % Check 1s argument | |
58 | |
59 % Class matrix? | |
60 if vis_valuetype(m, {'nxm'}); | |
61 colorcode=class; | |
62 class=m; | |
63 if ~vis_valuetype(colorcode,{'nx3rgb',[size(class,1) 3]},'all'), | |
64 error(['If map or topol is not specified the colorcode must be a' ... | |
65 ' [size(class,1) 3] sized RGB matrix.']); | |
66 end | |
67 else | |
68 [tmp,ok,tmp]=som_set(m); | |
69 if isstruct(m) & all(ok) | |
70 switch m.type | |
71 case 'som_topol' % topol? | |
72 msize=m.msize; | |
73 lattice=m.lattice; | |
74 case 'som_map' | |
75 msize=m.topol.msize; % map? | |
76 lattice=m.topol.lattice; | |
77 otherwise | |
78 error('Invalid map or topol struct.'); | |
79 end | |
80 % cell? | |
81 elseif iscell(m) & vis_valuetype(size(m),{[1 2]}), | |
82 if vis_valuetype(m{2},{[1 2]}) & vis_valuetype(m{1},{'string'}), | |
83 lattice=m{1}; | |
84 msize=m{2}; | |
85 else | |
86 error('Invalid map size information.'); | |
87 end | |
88 else | |
89 % not known type | |
90 error('Invalid first argument!'); | |
91 end | |
92 % Check map parameters | |
93 switch lattice % lattice | |
94 case 'hexa' | |
95 ; | |
96 case 'rect' | |
97 ; | |
98 otherwise | |
99 error('Unknown lattice type'); | |
100 end | |
101 if length(msize)>2 % dimension | |
102 error('Only 2D maps allowed!'); | |
103 end | |
104 % Check colorcode | |
105 if nargin<3 | isempty(colorcode) | |
106 colorcode='rgb2'; | |
107 end | |
108 end | |
109 | |
110 % Check class | |
111 if any(class~=round(class)) | |
112 error('Class labels must be integer numbers.'); | |
113 end | |
114 | |
115 if min(class)<=0 | |
116 error('Class numbers should be greater than 0'); | |
117 end | |
118 | |
119 if ischar(colorcode), | |
120 switch colorcode | |
121 case{'rgb1','rgb2','rgb3','rgb4','hsv'} | |
122 colorcode=som_colorcode(m, colorcode); | |
123 otherwise | |
124 error(['Color code not known: should be ''rgb1'',''rgb2'',' ... | |
125 ' ''rgb3'',''rgb4'' or ''hsv''.']); | |
126 end | |
127 elseif ~vis_valuetype(colorcode,{'nx3rgb',[size(class,1) 3]},'all'); | |
128 error(['Invalid colorcode matrix: should be a ' ... | |
129 '[length(class) 3] sized RGB matrix.']); | |
130 end | |
131 | |
132 %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
133 | |
134 % Go through all i classifications (columns) | |
135 for i=1:size(class,2), | |
136 % Get unique class labels in ith classification | |
137 c=unique(class(:,i))'; % row vector for loop indexing | |
138 % Go through all class in ith classification | |
139 for j=c; | |
140 index=(class(:,i)==j); | |
141 N=sum(index); | |
142 colors=colorcode(index,:); | |
143 % Calculate the mean color | |
144 meancolor=repmat(mean(colors,1),N,1); | |
145 % Select the original color that is closest to this mean | |
146 dist=sum((meancolor-colors).^2,2); | |
147 [tmp,min_dist_index]=min(dist); | |
148 best_color=repmat(colors(min_dist_index,:),N,1); | |
149 % Set the color to output variable | |
150 color(index,:,i)=best_color; | |
151 end | |
152 end |