annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirnovelty.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function varargout = mirnovelty(orig,varargin)
Daniel@0 2 % n = mirnovelty(m) evaluates the novelty score from a similarity matrix.
Daniel@0 3 % [n,m] = mirnovelty(m) also return the similarity matrix.
Daniel@0 4 % Optional argument:
Daniel@0 5 % mirnovelty(...,'Distance',f) specifies the name of a dissimilarity
Daniel@0 6 % distance function, from those proposed in the Statistics Toolbox
Daniel@0 7 % (help pdist).
Daniel@0 8 % default value: f = 'cosine'
Daniel@0 9 % mirnovelty(...,'Similarity',f) specifies the name of a similarity
Daniel@0 10 % measure function. This function is applied to the result of the
Daniel@0 11 % distance function. cf. mirsimatrix
Daniel@0 12 % default value: f = 'exponential'
Daniel@0 13 % corresponding to f(x) = exp(-x)
Daniel@0 14 % mirnovelty(...,'KernelSize',s) or more simply mirnovelty(...,s)
Daniel@0 15 % specifies the length of the gaussian kernel, in samples.
Daniel@0 16 % default value: s = 64.
Daniel@0 17 % mirnovelty(...,'Normal',0) does not normalize the novelty curve
Daniel@0 18 % between the values 0 and 1.
Daniel@0 19 %
Daniel@0 20 % Foote, J. & Cooper, M. (2003). Media Segmentation using Self-Similarity
Daniel@0 21 % Decomposition,. In Proc. SPIE Storage and Retrieval for Multimedia
Daniel@0 22 % Databases, Vol. 5021, pp. 167-75.
Daniel@0 23
Daniel@0 24 dist.key = 'Distance';
Daniel@0 25 dist.type = 'String';
Daniel@0 26 dist.default = 'cosine';
Daniel@0 27 option.dist = dist;
Daniel@0 28
Daniel@0 29 sm.key = {'Measure','Similarity'};
Daniel@0 30 sm.type = 'String';
Daniel@0 31 sm.default = 'exponential';
Daniel@0 32 option.sm = sm;
Daniel@0 33
Daniel@0 34 K.key = {'KernelSize','Width'};
Daniel@0 35 K.type = 'Integer';
Daniel@0 36 K.default = 64;
Daniel@0 37 option.K = K;
Daniel@0 38
Daniel@0 39 transf.type = 'String';
Daniel@0 40 transf.default = 'TimeLag';
Daniel@0 41 transf.choice = {'Horizontal','TimeLag'};
Daniel@0 42 option.transf = transf;
Daniel@0 43
Daniel@0 44 normal.key = 'Normal';
Daniel@0 45 normal.type = 'Boolean';
Daniel@0 46 normal.default = 1;
Daniel@0 47 normal.when = 'After';
Daniel@0 48 option.normal = normal;
Daniel@0 49
Daniel@0 50 specif.option = option;
Daniel@0 51 specif.combineframes = @combineframes;
Daniel@0 52 specif.nochunk = 1;
Daniel@0 53 varargout = mirfunction(@mirnovelty,orig,varargin,nargout,specif,@init,@main);
Daniel@0 54
Daniel@0 55
Daniel@0 56 function [x type] = init(x,option)
Daniel@0 57 type = 'mirscalar';
Daniel@0 58 if not(isamir(x,'mirscalar') && strcmp(get(x,'Title'),'Novelty'))
Daniel@0 59 x = mirsimatrix(x,'Distance',option.dist,'Similarity',option.sm,...
Daniel@0 60 'Width',option.K,option.transf);
Daniel@0 61 end
Daniel@0 62 if isa(x,'mirdesign')
Daniel@0 63 x = set(x,'Overlap',ceil(option.K));
Daniel@0 64 end
Daniel@0 65
Daniel@0 66
Daniel@0 67 function y = main(orig,option,postoption)
Daniel@0 68 if iscell(orig)
Daniel@0 69 orig = orig{1};
Daniel@0 70 end
Daniel@0 71 if not(isa(orig,'mirscalar'))
Daniel@0 72 s = get(orig,'Data');
Daniel@0 73 dw = get(orig,'DiagWidth');
Daniel@0 74 for k = 1:length(s)
Daniel@0 75 if isnumeric(dw)
Daniel@0 76 dwk = dw;
Daniel@0 77 else
Daniel@0 78 dwk = dw{k};
Daniel@0 79 end
Daniel@0 80 if option.K
Daniel@0 81 cgs = min(option.K,dwk);
Daniel@0 82 else
Daniel@0 83 cgs = dwk;
Daniel@0 84 end
Daniel@0 85 cg = checkergauss(cgs,option.transf);
Daniel@0 86 disp('Computing convolution, please wait...')
Daniel@0 87 for z = 1:length(s{k})
Daniel@0 88 sz = s{k}{z};
Daniel@0 89 szm = max(max(sz));
Daniel@0 90 for i = find(isnan(sz))
Daniel@0 91 sz(i) = szm;
Daniel@0 92 end
Daniel@0 93 cv = convolve2(sz,cg,'same');
Daniel@0 94 nl = size(cv,1);
Daniel@0 95 nc = size(cv,2);
Daniel@0 96 if nl == 0
Daniel@0 97 warning('WARNING IN NOVELTY: No frame decomposition. The novelty score cannot be computed.');
Daniel@0 98 score{k}{z} = [];
Daniel@0 99 else
Daniel@0 100 sco = cv(floor(size(cv,1)/2),:);
Daniel@0 101 incr = find(diff(sco)>=0);
Daniel@0 102 if not(isempty(incr))
Daniel@0 103 decr = find(diff(sco)<=0);
Daniel@0 104 sco(1:incr(1)-1) = NaN(1,incr(1)-1);
Daniel@0 105 if not(isempty(decr))
Daniel@0 106 sco(decr(end)+1:end) = NaN(1,length(sco)-decr(end));
Daniel@0 107 end
Daniel@0 108 incr = find(diff(sco)>=0);
Daniel@0 109 sco2 = sco;
Daniel@0 110 if not(isempty(incr))
Daniel@0 111 sco2 = sco2(1:incr(end)+1);
Daniel@0 112 end
Daniel@0 113 decr = find(diff(sco)<=0);
Daniel@0 114 if not(isempty(decr)) && decr(1)>2
Daniel@0 115 sco2 = sco2(decr(1)-1:end);
Daniel@0 116 end
Daniel@0 117 mins = min(sco2);
Daniel@0 118 rang = find(sco>= mins);
Daniel@0 119 if not(isempty(rang))
Daniel@0 120 sco(1:rang(1)-1) = NaN(1,rang(1)-1);
Daniel@0 121 sco(rang(end)+1:end) = NaN(1,length(sco)-rang(end));
Daniel@0 122 end
Daniel@0 123 end
Daniel@0 124 score{k}{z} = sco;
Daniel@0 125 end
Daniel@0 126 end
Daniel@0 127 end
Daniel@0 128 else
Daniel@0 129 score = get(orig,'Data');
Daniel@0 130 end
Daniel@0 131 if not(isempty(postoption)) && postoption.normal
Daniel@0 132 for k = 1:length(score)
Daniel@0 133 for l = 1:length(score{k})
Daniel@0 134 sco = score{k}{l};
Daniel@0 135 sco = (sco-min(sco))/(max(sco)-min(sco));
Daniel@0 136 score{k}{l} = sco;
Daniel@0 137 end
Daniel@0 138 end
Daniel@0 139 end
Daniel@0 140 n = mirscalar(orig,'Data',score,'Title','Novelty');
Daniel@0 141 y = {n orig};
Daniel@0 142
Daniel@0 143
Daniel@0 144 function old = combineframes(old,new)
Daniel@0 145 if not(iscell(old))
Daniel@0 146 old = {old};
Daniel@0 147 end
Daniel@0 148 if not(iscell(new))
Daniel@0 149 new = {new};
Daniel@0 150 end
Daniel@0 151 for var = 1:length(new)
Daniel@0 152 ov = old{var};
Daniel@0 153 nv = new{var};
Daniel@0 154 ofp = get(ov,'FramePos');
Daniel@0 155 ofp = ofp{1}{1};
Daniel@0 156 nfp = get(nv,'FramePos');
Daniel@0 157 nfp = nfp{1}{1};
Daniel@0 158 od = get(ov,'Data');
Daniel@0 159 od = od{1}{1};
Daniel@0 160 onan = find(isnan(od));
Daniel@0 161 od(onan) = [];
Daniel@0 162 ofp(:,onan) = [];
Daniel@0 163 nd = get(nv,'Data');
Daniel@0 164 nd = nd{1}{1};
Daniel@0 165 nnan = find(isnan(nd));
Daniel@0 166 nd(nnan) = [];
Daniel@0 167 nfp(:,nnan) = [];
Daniel@0 168 [unused omatch nmatch] = intersect(ofp(1,:),nfp(1,:));
Daniel@0 169 if isempty(omatch)
Daniel@0 170 ov = set(ov,'FramePos',{{[ofp nfp]}},'Data',{{[od nd]}});
Daniel@0 171 else
Daniel@0 172 lm = length(omatch);
Daniel@0 173 ov = set(ov,'FramePos',{{[ofp(:,1:omatch(1)-1) nfp]}},...
Daniel@0 174 'Data',{{[od(1:omatch(1)-1),...
Daniel@0 175 (od(omatch).*(lm:-1:1) + nd(nmatch).*(1:lm))/(lm+1),...
Daniel@0 176 nd(nmatch(end)+1:end)]}});
Daniel@0 177 end
Daniel@0 178 old{var} = ov;
Daniel@0 179 end
Daniel@0 180
Daniel@0 181
Daniel@0 182 function y = checkergauss(N,transf)
Daniel@0 183 hN = ceil(N/2);
Daniel@0 184 if strcmpi(transf,'TimeLag')
Daniel@0 185 y = zeros(hN,N);
Daniel@0 186 for j = 1:N
Daniel@0 187 for i = 1:hN
Daniel@0 188 g = exp(-((i/hN)^2 + (((j-hN)/hN)^2))*4);
Daniel@0 189 if j>hN && j<hN+i
Daniel@0 190 y(hN-i+1,j) = -g;
Daniel@0 191 else
Daniel@0 192 y(hN-i+1,j) = g;
Daniel@0 193 end
Daniel@0 194 end
Daniel@0 195 end
Daniel@0 196 else
Daniel@0 197 y = zeros(N);
Daniel@0 198 for i = 1:N
Daniel@0 199 for j = 1:N
Daniel@0 200 g = exp(-(((i-hN)/hN)^2 + (((j-hN)/hN)^2))*4);
Daniel@0 201 if xor(j>i,j>N-i)
Daniel@0 202 y(i,j) = -g;
Daniel@0 203 else
Daniel@0 204 y(i,j) = g;
Daniel@0 205 end
Daniel@0 206 end
Daniel@0 207 end
Daniel@0 208 end