wolffd@0: function [color,best,kmeans]=som_kmeanscolor(sM,C,initRGB,contrast) wolffd@0: wolffd@0: % SOM_KMEANSCOLOR Map unit color code according to K-means clustering wolffd@0: % wolffd@0: % [color, best, kmeans] = som_kmeanscolor(sM, C, [initRGB],[contrast]) wolffd@0: % wolffd@0: % color = som_kmeanscolor(sM,15,som_colorcode(sM,'rgb1'),'enhance'); wolffd@0: % [color,best] = som_kmeanscolor(sM,15,[],'normal'); wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional): wolffd@0: % sM (struct) map struct wolffd@0: % C (scalar) maximum number of clusters wolffd@0: % initRGB (string, matrix) color code string accepted by SOM_COLORCODE wolffd@0: % or an Mx3 matrix of RGB triples, where M is the number wolffd@0: % of map units. Default: SOM_COLORCODEs default wolffd@0: % contrast (string) 'flat', 'enhanced' color contrast mode, default: wolffd@0: % 'enhanced' wolffd@0: % wolffd@0: % color (matrix) MxCx3 of RGB triples wolffd@0: % best (scalar) index for "best" clustering according to wolffd@0: % Davies-Boulding index; color(:,:,best) includes the wolffd@0: % corresponding color code. wolffd@0: % kmeans (cell) output of KMEANS_CLUSTERS in a cell array. wolffd@0: % wolffd@0: % The function gives a set of color codings according to K-means wolffd@0: % clustering. For clustering, it uses function KMEANS_CLUSTERS for map units, wolffd@0: % and it calculates color codings for 1,2,...,C clusters. wolffd@0: % The idea of coloring is that the color of a cluster is the mean of the wolffd@0: % original colors (RGB values) of the map units belonging to that cluster, wolffd@0: % see SOM_CLUSTERCOLOR. The original colors are defined by SOM_COLORCODE wolffd@0: % by default. Input 'contrast' simply specifies whether or not wolffd@0: % to linearly redistribute R,G, and B values so that minimum is 0 and wolffd@0: % maximum 1 ('enahanced') or to use directly the output of wolffd@0: % SOM_CLUSTERCOLOR ('flat'). KMEANS_CLUSTERS uses certain heuristics to wolffd@0: % select the best of 5 trials for each number of clusters. Evaluating the wolffd@0: % clustering multiple times may take some time. wolffd@0: % wolffd@0: % EXAMPLE wolffd@0: % wolffd@0: % load iris; % or any other map struct sM wolffd@0: % [color,b]=som_kmeanscolor(sM,10); wolffd@0: % som_show(sM,'color',color,'color',{color(:,:,b),'"Best clustering"'); wolffd@0: % wolffd@0: % See also SOM_SHOW, SOM_COLORCODE, SOM_CLUSTERCOLOR, KMEANS_CLUSTERS wolffd@0: wolffd@0: % Contributed to SOM Toolbox 2.0, April 1st, 2000 by Johan Himberg wolffd@0: % Copyright (c) by Johan Himberg wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % corrected help text 11032005 johan wolffd@0: wolffd@0: %%% Check number of inputs wolffd@0: wolffd@0: error(nargchk(2, 4, nargin)); % check no. of input args wolffd@0: wolffd@0: %%% Check input args & set defaults wolffd@0: wolffd@0: if isstruct(sM) & isfield(sM,'type') & strcmp(sM.type,'som_map'), wolffd@0: [tmp,lattice,msize]=vis_planeGetArgs(sM); wolffd@0: munits=prod(msize); wolffd@0: if length(msize)>2 wolffd@0: error('Does not work with 3D maps.') wolffd@0: end wolffd@0: else wolffd@0: error('Map struct requires for first input argument!'); wolffd@0: end wolffd@0: wolffd@0: if ~vis_valuetype(C,{'1x1'}), wolffd@0: error('Scalar value expect for maximum number of clusters.'); wolffd@0: end wolffd@0: wolffd@0: % check initial color coding wolffd@0: if nargin<3 | isempty(initRGB) wolffd@0: initRGB=som_colorcode(sM); wolffd@0: end wolffd@0: wolffd@0: % check contrast checking wolffd@0: if nargin<4 | isempty(contrast), wolffd@0: contrast='enhanced'; wolffd@0: end wolffd@0: wolffd@0: if ~ischar(contrast), wolffd@0: error('String input expected for input arg. ''contrast''.'); wolffd@0: else wolffd@0: switch lower(contrast) wolffd@0: case {'flat','enhanced'} wolffd@0: ; wolffd@0: otherwise wolffd@0: error(['''flat'' or ''enhanced'' expected for '... wolffd@0: 'input argument ''contrast''.']); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if ischar(initRGB), wolffd@0: try wolffd@0: initRGB=som_colorcode(sM,initRGB); wolffd@0: catch wolffd@0: error(['Color code ' initRGB ... wolffd@0: 'was not recognized by SOM_COLORCODE.']); wolffd@0: end wolffd@0: elseif vis_valuetype(initRGB,{'nx3rgb',[munits 3]},'all'), wolffd@0: ; wolffd@0: else wolffd@0: error(['The initial color code must be a string '... wolffd@0: 'or an Mx3 matrix of RGB triples.']); wolffd@0: end wolffd@0: wolffd@0: %%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: disp('Wait...'); wolffd@0: [c,p,err,ind]=kmeans_clusters(sM,C,5,0); % use 5 trials, verbose off wolffd@0: wolffd@0: % Store outputs to kmeans wolffd@0: kmeans{1}=c; wolffd@0: kmeans{2}=p; wolffd@0: kmeans{3}=err; wolffd@0: kmeans{4}=ind; wolffd@0: wolffd@0: %%% Build output wolffd@0: color=som_clustercolor(sM,cat(2,p{:}),initRGB); wolffd@0: [tmp,best]=min(ind); wolffd@0: wolffd@0: switch contrast wolffd@0: case 'flat' wolffd@0: ; wolffd@0: case 'enhanced' wolffd@0: warning off; wolffd@0: ncolor=maxnorm(color); wolffd@0: ncolor(~isfinite(ncolor))=color(~isfinite(ncolor)); wolffd@0: color=ncolor; wolffd@0: warning on; wolffd@0: end wolffd@0: wolffd@0: %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: function X=maxnorm(x) wolffd@0: % normalize columns of x between [0,1] wolffd@0: wolffd@0: x=x-repmat(min(x),[size(x,1) 1 1]); wolffd@0: X=x./repmat(max(x),[size(x,1) 1 1]);