Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_colorcode.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 colors=som_colorcode(m, colorcode, scaling) | |
| 2 | |
| 3 %SOM_COLORCODE Calculates a heuristic color coding for the SOM grid | |
| 4 % | |
| 5 % colors = som_colorcode(m, colorcode, scaling) | |
| 6 % | |
| 7 % Input and output arguments ([]'s are optional): | |
| 8 % m (struct) map or topol struct | |
| 9 % (cell array) of form {str,[m1 m2]} where | |
| 10 % str = 'hexa' or 'rect' and [m1 m2] = msize | |
| 11 % (matrix) size N x 2, unit coordinates | |
| 12 % [colorcode] (string) 'rgb1' (default),'rgb2','rgb3','rgb4','hsv' | |
| 13 % [scaling] (scalar) 1=on (default), 0=off. Has effect only | |
| 14 % if m is a Nx2 matrix of coordinates: | |
| 15 % controls whether these are scaled to | |
| 16 % range [0,1] or not. | |
| 17 % | |
| 18 % colors (matrix) size N x 3, RGB colors for each unit (or point) | |
| 19 % | |
| 20 % The function gives a color coding by location for the map grid | |
| 21 % (or arbitrary set of points). Map grid coordinates are always linearly | |
| 22 % normalized to a unit square (x and y coordinates between [0,1]), except | |
| 23 % if m is a Nx2 matrix and scaling=0. In that case too, the coordinates | |
| 24 % must be in range [0,1]. | |
| 25 % | |
| 26 % Following heuristic color codings are available: | |
| 27 % | |
| 28 % 'rgb1' slice of RGB-cube so that green - yellow | |
| 29 % the corners have colors: | | | |
| 30 % blue - magenta | |
| 31 % | |
| 32 % 'rgb2' slice of RGB-cube so that red - yellow | |
| 33 % the corners have colors: | | | |
| 34 % blue - cyan | |
| 35 % | |
| 36 % 'rgb3' slice of RGB-cube so that mixed_green - orange | |
| 37 % the corners have colors: | | | |
| 38 % light_blue - pink | |
| 39 % | |
| 40 % 'rgb4' has 'rgb1' on the diagonal + additional colors in corners | |
| 41 % (more resolution but visually strongly discontinuous) | |
| 42 % | |
| 43 % 'hsv' angle and radius from map centre are coded by hue and | |
| 44 % intensity (more resoluton but visually discontinuous) | |
| 45 % | |
| 46 % See also SOM_CPLANE, SOM_SHOW, SOM_CLUSTERCOLOR, SOM_KMEANSCOLOR, | |
| 47 % SOM_BMUCOLOR. | |
| 48 | |
| 49 % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Johan Himberg | |
| 50 % Copyright (c) by Johan Himberg | |
| 51 % http://www.cis.hut.fi/projects/somtoolbox/ | |
| 52 | |
| 53 % Version 2.0 Johan 140799 | |
| 54 | |
| 55 %%% Check arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 56 | |
| 57 error(nargchk(1, 3, nargin)); % check no. of input args is correct | |
| 58 | |
| 59 %% Check m: map, topol, cell or data? | |
| 60 | |
| 61 if vis_valuetype(m,{'nx2'}), | |
| 62 p=m; % explicit coordinates | |
| 63 | |
| 64 else | |
| 65 | |
| 66 % map, topol, cell | |
| 67 | |
| 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 | |
| 81 % cell | |
| 82 | |
| 83 elseif iscell(m) & vis_valuetype(size(m),{[1 2]}), | |
| 84 if vis_valuetype(m{2},{[1 2]}) & vis_valuetype(m{1},{'string'}), | |
| 85 lattice=m{1}; | |
| 86 msize=m{2}; | |
| 87 else | |
| 88 error('Invalid map size information.'); | |
| 89 end | |
| 90 end | |
| 91 | |
| 92 %% Check map parameters | |
| 93 | |
| 94 switch lattice % lattice | |
| 95 case 'hexa' | |
| 96 ; | |
| 97 case 'rect' | |
| 98 ; | |
| 99 otherwise | |
| 100 error('Unknown lattice type'); | |
| 101 end | |
| 102 | |
| 103 if length(msize)>2 % dimension | |
| 104 error('Only 2D maps allowed!'); | |
| 105 end | |
| 106 | |
| 107 | |
| 108 % Calculate coordinates | |
| 109 p=som_unit_coords(msize,lattice,'sheet'); | |
| 110 | |
| 111 % Set scaling to 1 as it is done always in this case | |
| 112 scaling=1; | |
| 113 end | |
| 114 | |
| 115 % Check colorcode | |
| 116 | |
| 117 if nargin < 2 | isempty(colorcode), | |
| 118 colorcode='rgb1'; | |
| 119 end | |
| 120 if ~ischar(colorcode) | |
| 121 error('String value for colorcode mode expected.'); | |
| 122 else | |
| 123 switch colorcode | |
| 124 case { 'rgb1', 'rgb2', 'rgb3' , 'rgb4' ,'hsv'} | |
| 125 otherwise | |
| 126 error([ 'Colorcode mode ' colorcode ' not implemented.']); | |
| 127 end | |
| 128 end | |
| 129 | |
| 130 % Check scaling | |
| 131 | |
| 132 if nargin < 3 | isempty(scaling) | |
| 133 scaling=1; | |
| 134 end | |
| 135 | |
| 136 if ~vis_valuetype(scaling,{'1x1'}) | |
| 137 error('Scaling should be 0 (off) or 1 (on).'); | |
| 138 end | |
| 139 | |
| 140 %% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 141 | |
| 142 % scale coordintes between [0,1] | |
| 143 | |
| 144 if scaling | |
| 145 n=size(p,1); | |
| 146 mn=min(p); | |
| 147 e=max(p)-mn; | |
| 148 p=(p-repmat(mn,n,1))./repmat(e,n,1); | |
| 149 elseif sum(p(:,1)>1+p(:,1)<0+p(:,2)>1+p(:,2)<0), | |
| 150 error('Coordinates out of range [0,1].'); | |
| 151 end | |
| 152 | |
| 153 switch colorcode | |
| 154 case 'rgb1' | |
| 155 h(:,1)=p(:,1); | |
| 156 h(:,2)=1-p(:,2); | |
| 157 h(:,3)=p(:,2); | |
| 158 case 'rgb2' | |
| 159 h(:,1)=p(:,1); | |
| 160 h(:,2)=1-p(:,2); | |
| 161 h(:,3)=1-p(:,1); | |
| 162 case 'rgb3' | |
| 163 h(:,1)=p(:,1); | |
| 164 h(:,2)=.5; | |
| 165 h(:,3)=p(:,2); | |
| 166 case 'rgb4' | |
| 167 p=rgb4(p); | |
| 168 h(:,1)=p(:,1); | |
| 169 h(:,2)=1-p(:,2); | |
| 170 h(:,3)=p(:,3); | |
| 171 case 'hsv' | |
| 172 munits = n; | |
| 173 Hsv = zeros(munits,3); | |
| 174 for i=1:n, | |
| 175 dx = .5-p(i,1); | |
| 176 dy = .5-p(i,2); | |
| 177 r = sqrt(dx^2+dy^2); | |
| 178 if r==0, | |
| 179 h=1; | |
| 180 elseif dx==0, | |
| 181 h=.5; %h=ay; | |
| 182 elseif dy==0, | |
| 183 h=.5; %h=ax; | |
| 184 else | |
| 185 h = min(abs(.5/(dx/r)),abs(.5/(dy/r))); | |
| 186 end | |
| 187 | |
| 188 if r==0, | |
| 189 angle = 0; | |
| 190 else | |
| 191 angle = acos(dx/r); | |
| 192 if dy<0, | |
| 193 angle = 2*pi-angle; | |
| 194 end | |
| 195 end | |
| 196 | |
| 197 Hsv(i,1) = 1-sin(angle/4); | |
| 198 Hsv(i,2) = 1; | |
| 199 Hsv(i,3) = r/h; | |
| 200 h = hsv2rgb(Hsv); | |
| 201 end | |
| 202 end | |
| 203 | |
| 204 | |
| 205 %% Build output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 206 | |
| 207 colors=h; | |
| 208 | |
| 209 %% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% juha %%%% | |
| 210 | |
| 211 function p=rgb4(coord) | |
| 212 | |
| 213 for i=1:size(coord,1); | |
| 214 p(i,:)=get_coords(coord(i,:))'; | |
| 215 end | |
| 216 | |
| 217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 218 | |
| 219 function coords=get_coords(coords) | |
| 220 | |
| 221 %GET_COORDS | |
| 222 % | |
| 223 % get_coords(coords) | |
| 224 % | |
| 225 % ARGUMENTS | |
| 226 % | |
| 227 % coords (1x2 or 2x1 vector) coords(1) is an x-coordinate and coords(2) | |
| 228 % y-coordinate. | |
| 229 % | |
| 230 % | |
| 231 % RETURNS | |
| 232 % | |
| 233 % coords (3x1 vector) x,y and z-coordinates. | |
| 234 % | |
| 235 | |
| 236 if ~(all(size(coords) == [1 2]) | all(size(coords) == [2 1])) | |
| 237 error('Argument ''coords'' must be an 2x1 or 1x2 vector.'); | |
| 238 end | |
| 239 | |
| 240 if all(size(coords) == [1 2]) | |
| 241 coords=coords'; | |
| 242 end | |
| 243 | |
| 244 if any(coords > 1) any(coords < 0) | |
| 245 error('Coordinates must lay inside the interval [0,1].'); | |
| 246 end | |
| 247 | |
| 248 if coords(1) <= 1/(sqrt(2)+1), | |
| 249 if coords(2) <= line3(coords(1)) | |
| 250 coords=coords_in_base(4,coords); | |
| 251 elseif coords(2) <= line2(coords(1)) | |
| 252 coords=coords_in_base(1,coords); | |
| 253 else | |
| 254 coords=coords_in_base(2,coords); | |
| 255 end | |
| 256 elseif coords(1) <= sqrt(2)/(sqrt(2)+1) | |
| 257 if coords(2) <= line1(coords(1)) | |
| 258 coords=coords_in_base(3,coords); | |
| 259 elseif coords(2) <= line2(coords(1)) | |
| 260 coords=coords_in_base(1,coords); | |
| 261 else | |
| 262 coords=coords_in_base(2,coords); | |
| 263 end | |
| 264 else | |
| 265 if coords(2) <= line1(coords(1)), | |
| 266 coords=coords_in_base(3,coords); | |
| 267 elseif coords(2) <= line4(coords(1)) | |
| 268 coords=coords_in_base(1,coords); | |
| 269 else | |
| 270 coords=coords_in_base(5,coords); | |
| 271 end | |
| 272 end | |
| 273 | |
| 274 | |
| 275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 276 | |
| 277 function coords=coords_in_base(base_no,coords) | |
| 278 | |
| 279 A=[0;1/(sqrt(2)+1)]; | |
| 280 E=[1;1]; | |
| 281 F=[0;0]; | |
| 282 G=[1;0]; | |
| 283 H=[0;1]; | |
| 284 | |
| 285 const=1+1/sqrt(2); | |
| 286 | |
| 287 switch base_no | |
| 288 case 1 | |
| 289 x=(coords-A)*const; | |
| 290 coords=[(1/sqrt(2))*(x(1)-x(2));0.5*(x(1)+x(2));0.5*(x(1)+x(2))]; | |
| 291 case 2 | |
| 292 x=(coords-H)*const; | |
| 293 coords=[0;x(1);1+x(2)]; | |
| 294 case 3 | |
| 295 x=(coords-G)*const; | |
| 296 coords=[1;1+x(1);x(2)]; | |
| 297 case 4 | |
| 298 x=(coords-F)*const; | |
| 299 coords=[0.5+(1/sqrt(2))*(x(1)-x(2));... | |
| 300 0.5-(1/sqrt(2))*(x(1)+x(2));... | |
| 301 0]; | |
| 302 case 5 | |
| 303 x=(coords-E)*const; | |
| 304 coords=[0.5+(1/sqrt(2))*(x(1)-x(2));... | |
| 305 0.5-(1/sqrt(2))*(x(1)+x(2));... | |
| 306 1]; | |
| 307 end | |
| 308 | |
| 309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 310 | |
| 311 function y=line1(x) | |
| 312 | |
| 313 y = x-1/(sqrt(2)+1); | |
| 314 | |
| 315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 316 | |
| 317 function y=line2(x) | |
| 318 | |
| 319 y = x+1/(sqrt(2)+1); | |
| 320 | |
| 321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 322 | |
| 323 function y=line3(x) | |
| 324 | |
| 325 y = -x+1/(sqrt(2)+1); | |
| 326 | |
| 327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
| 328 | |
| 329 function y= line4(x) | |
| 330 | |
| 331 y = -x+(2*sqrt(2)+1)/(sqrt(2)+1); | |
| 332 | |
| 333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
