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