diff options
author | leochanj <jbakita@cs.unc.edu> | 2020-10-21 01:52:54 -0400 |
---|---|---|
committer | leochanj <jbakita@cs.unc.edu> | 2020-10-21 01:52:54 -0400 |
commit | 25d94aa8aabb8ac3e8bbea0bc439ea6148444cc8 (patch) | |
tree | ba80e76d25d9ca9486092e2f6b6d76f0e3352bf7 /SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m | |
parent | e2b50015cebdfba68699abd6e8575e38230f5a78 (diff) |
debug libextra and remove matlab
Diffstat (limited to 'SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m')
-rwxr-xr-x | SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m | 436 |
1 files changed, 0 insertions, 436 deletions
diff --git a/SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m b/SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m deleted file mode 100755 index 38fce5a..0000000 --- a/SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m +++ /dev/null | |||
@@ -1,436 +0,0 @@ | |||
1 | function [im,snrP,imS] = textureSynthesis(params, im0, Niter, cmask, imask) | ||
2 | |||
3 | % [res,snrP,imS] = textureSynthesis(params, initialIm, Niter, cmask, imask) | ||
4 | % | ||
5 | % Synthesize texture applying Portilla-Simoncelli model/algorithm. | ||
6 | % | ||
7 | % params: structure containing texture parameters (as returned by textureAnalysis). | ||
8 | % | ||
9 | % im0: initial image, OR a vector (Ydim, Xdim, [SEED]) containing | ||
10 | % dimensions of desired image and an optional seed for the random | ||
11 | % number generator. If dimensions are passed, initial image is | ||
12 | % Gaussian white noise. | ||
13 | % | ||
14 | % Niter (optional): Number of iterations. Default = 50. | ||
15 | % | ||
16 | % cmask (optional): binary column vector (4x1) indicating which sets of | ||
17 | % constraints we want to apply in the synthesis. The four sets are: | ||
18 | % 1) Marginal statistics (mean, var, skew, kurt, range) | ||
19 | % 2) Correlation of subbands (space, orientation, scale) | ||
20 | % 3) Correlation of magnitude responses (sp, or, sc) | ||
21 | % 4) Relative local phase | ||
22 | % | ||
23 | % imask (optional): imsizex2 matrix. First column is a mask, second | ||
24 | % column contains the image values to be imposed. If only one column is | ||
25 | % provided, it assumes it corresponds to the image values, and it uses | ||
26 | % a raised cosine square for the mask. | ||
27 | % snrP (optional): Set of adjustment values (in dB) of the parameters. | ||
28 | % imS (optional): Sequence of synthetic images, from niter = 1 to 2^n, being | ||
29 | % n = floor(log2(Niter)). | ||
30 | |||
31 | % Javier Portilla and Eero Simoncelli. | ||
32 | % Work described in: | ||
33 | % "A Parametric Texture Model based on Joint Statistics of Complex Wavelet Coefficients". | ||
34 | % J Portilla and E P Simoncelli. Int'l Journal of Computer Vision, | ||
35 | % vol.40(1), pp. 49-71, Dec 2000. | ||
36 | % | ||
37 | % Please refer to this publication if you use the program for research or | ||
38 | % for technical applications. Thank you. | ||
39 | % | ||
40 | % Copyright, Center for Neural Science, New York University, January 2001. | ||
41 | % All rights reserved. | ||
42 | |||
43 | Warn = 0; % Set to 1 if you want to see warning messages | ||
44 | |||
45 | %% Check required args are passed: | ||
46 | if (nargin < 2) | ||
47 | error('Function called with too few input arguments'); | ||
48 | end | ||
49 | |||
50 | if ( ~exist('Niter') | isempty(Niter) ) | ||
51 | Niter = 50; | ||
52 | end | ||
53 | |||
54 | if (exist('cmask') & ~isempty(cmask) ) | ||
55 | cmask = (cmask > 0.5); % indices of ones in mask | ||
56 | else | ||
57 | cmask = ones(4,1); | ||
58 | end | ||
59 | |||
60 | %% Extract parameters | ||
61 | statg0 = params.pixelStats; | ||
62 | mean0 = statg0(1); var0 = statg0(2); | ||
63 | skew0 = statg0(3); kurt0 = statg0(4); | ||
64 | mn0 = statg0(5); mx0 = statg0(6); | ||
65 | statsLPim = params.pixelLPStats; | ||
66 | skew0p = statsLPim(:,1); | ||
67 | kurt0p = statsLPim(:,2); | ||
68 | vHPR0 = params.varianceHPR; | ||
69 | acr0 = params.autoCorrReal; | ||
70 | ace0 = params.autoCorrMag; | ||
71 | magMeans0 = params.magMeans; | ||
72 | C0 = params.cousinMagCorr; | ||
73 | Cx0 = params.parentMagCorr; | ||
74 | Crx0 = params.parentRealCorr; | ||
75 | |||
76 | %% Extract {Nsc, Nor, Na} from params | ||
77 | tmp = size(params.autoCorrMag); | ||
78 | Na = tmp(1); Nsc = tmp(3); | ||
79 | Nor = tmp(length(tmp))*(length(tmp)==4) + (length(tmp)<4); | ||
80 | la = (Na-1)/2; | ||
81 | |||
82 | %% If im0 is a vector of length 2, create Gaussian white noise image of this | ||
83 | %% size, with desired pixel mean and variance. If vector length is | ||
84 | %% 3, use the 3rd element to seed the random number generator. | ||
85 | if ( length(im0) <= 3 ) | ||
86 | if ( length(im0) == 3) | ||
87 | randn('state', im0(3)); % Reset Seed | ||
88 | im0 = im0(1:2); | ||
89 | end | ||
90 | im = mean0 + sqrt(var0)*randn(im0); | ||
91 | else | ||
92 | im = im0; | ||
93 | end | ||
94 | |||
95 | %% If the spatial neighborhood Na is too big for the lower scales, | ||
96 | %% "modacor22.m" will make it as big as the spatial support at | ||
97 | %% each scale: | ||
98 | [Ny,Nx] = size(im); | ||
99 | nth = log2(min(Ny,Nx)/Na); | ||
100 | if nth<Nsc+1 & Warn, | ||
101 | fprintf(1,'Warning: Na will be cut off for levels above #%d !\n',floor(nth)); | ||
102 | end | ||
103 | |||
104 | if exist('imask') & ~isempty(imask), | ||
105 | if size(imask,1) ~= prod(size(im)) | ||
106 | error(sprintf('imask size %d does not match image dimensions [%d,%d]',... | ||
107 | size(imask,1), size(im,1), size(im,2))); | ||
108 | end | ||
109 | if size(imask,2) == 1, | ||
110 | mask = (cos(-pi/2:2*pi/Ny:pi*(1-2/Ny)/2)).'*cos(-pi/2:2*pi/Nx:pi*(1-2/Nx)/2); | ||
111 | mask = mask.^2; | ||
112 | aux = zeros(size(im)); | ||
113 | aux(Ny/4+1:Ny/4+Ny/2,Nx/4+1:Nx/4+Nx/2)=mask; | ||
114 | mask = aux; | ||
115 | else | ||
116 | mask = reshape(imask(:,1),size(im)); | ||
117 | end | ||
118 | end | ||
119 | |||
120 | imf = max(1,gcf-1); snrf = imf+1; | ||
121 | figure(imf); clf | ||
122 | subplot(1,2,1); grayRange = showIm(im,'auto',1); title('Starting image'); | ||
123 | drawnow | ||
124 | |||
125 | prev_im=im; | ||
126 | |||
127 | imf = max(1,gcf-1); | ||
128 | figure(imf); | ||
129 | clf;showIm(im,'auto',1); title(sprintf('iteration 0')); | ||
130 | |||
131 | nq = 0; | ||
132 | Nq = floor(log2(Niter)); | ||
133 | imS = zeros(Ny,Nx,Nq); | ||
134 | |||
135 | %% MAIN LOOP | ||
136 | for niter = 1:Niter | ||
137 | |||
138 | %p = niter/Niter; | ||
139 | p = 1; | ||
140 | |||
141 | %% Build the steerable pyramid | ||
142 | [pyr,pind] = buildSCFpyr(im,Nsc,Nor-1); | ||
143 | |||
144 | if ( any(vector(mod(pind,4))) ) | ||
145 | error('Algorithm will fail: band dimensions are not all multiples of 4!'); | ||
146 | end | ||
147 | |||
148 | %% Subtract mean of lowBand: | ||
149 | nband = size(pind,1); | ||
150 | pyr(pyrBandIndices(pind,nband)) = ... | ||
151 | pyrBand(pyr,pind,nband) - mean2(pyrBand(pyr,pind,nband)); | ||
152 | |||
153 | apyr = abs(pyr); | ||
154 | |||
155 | %% Adjust autoCorr of lowBand | ||
156 | nband = size(pind,1); | ||
157 | ch = pyrBand(pyr,pind,nband); | ||
158 | Sch = min(size(ch)/2); | ||
159 | nz = sum(sum(~isnan(acr0(:,:,Nsc+1)))); | ||
160 | lz = (sqrt(nz)-1)/2; | ||
161 | le = min(Sch/2-1,lz); | ||
162 | im = real(ch); %Reconstructed image: initialize to lowband | ||
163 | [mpyr,mpind] = buildSFpyr(im,0,0); | ||
164 | im = pyrBand(mpyr,mpind,2); | ||
165 | vari = acr0(la+1:la+1,la+1:la+1,Nsc+1); | ||
166 | if cmask(2), | ||
167 | if vari/var0 > 1e-4, | ||
168 | [im, snr2(niter,Nsc+1)] = ... | ||
169 | modacor22(im, acr0(la-le+1:la+le+1,la-le+1:la+le+1,Nsc+1),p); | ||
170 | else | ||
171 | im = im*sqrt(vari/var2(im)); | ||
172 | end | ||
173 | if (var2(imag(ch))/var2(real(ch)) > 1e-6) | ||
174 | fprintf(1,'Discarding non-trivial imaginary part, lowPass autoCorr!'); | ||
175 | end | ||
176 | im = real(im); | ||
177 | end % cmask(2) | ||
178 | if cmask(1), | ||
179 | if vari/var0 > 1e-4, | ||
180 | [im,snr7(niter,2*(Nsc+1)-1)] = modskew(im,skew0p(Nsc+1),p); % Adjusts skewness | ||
181 | [im,snr7(niter,2*(Nsc+1))] = modkurt(im,kurt0p(Nsc+1),p); % Adjusts kurtosis | ||
182 | end | ||
183 | end % cmask(2) | ||
184 | |||
185 | %% Subtract mean of magnitude | ||
186 | if cmask(3), | ||
187 | magMeans = zeros(size(pind,1), 1); | ||
188 | for nband = 1:size(pind,1) | ||
189 | indices = pyrBandIndices(pind,nband); | ||
190 | magMeans(nband) = mean2(apyr(indices)); | ||
191 | apyr(indices) = apyr(indices) - magMeans(nband); | ||
192 | end | ||
193 | end % cmask(3) | ||
194 | |||
195 | %% Coarse-to-fine loop: | ||
196 | for nsc = Nsc:-1:1 | ||
197 | |||
198 | firstBnum = (nsc-1)*Nor+2; | ||
199 | cousinSz = prod(pind(firstBnum,:)); | ||
200 | ind = pyrBandIndices(pind,firstBnum); | ||
201 | cousinInd = ind(1) + [0:Nor*cousinSz-1]; | ||
202 | |||
203 | %% Interpolate parents | ||
204 | if (cmask(3) | cmask(4)), | ||
205 | if (nsc<Nsc) | ||
206 | parents = zeros(cousinSz,Nor); | ||
207 | rparents = zeros(cousinSz,Nor*2); | ||
208 | for nor = 1:Nor | ||
209 | nband = (nsc+1-1)*Nor+nor+1; | ||
210 | |||
211 | tmp = expand(pyrBand(pyr, pind, nband),2)/4; | ||
212 | rtmp = real(tmp); itmp = imag(tmp); | ||
213 | tmp = sqrt(rtmp.^2 + itmp.^2) .* exp(2 * sqrt(-1) * atan2(rtmp,itmp)); | ||
214 | rparents(:,nor) = vector(real(tmp)); | ||
215 | rparents(:,Nor+nor) = vector(imag(tmp)); | ||
216 | |||
217 | tmp = abs(tmp); | ||
218 | parents(:,nor) = vector(tmp - mean2(tmp)); | ||
219 | end | ||
220 | else | ||
221 | rparents = []; | ||
222 | parents = []; | ||
223 | end | ||
224 | end % if (cmask(3) | cmask(4)) | ||
225 | |||
226 | if cmask(3), | ||
227 | %% Adjust cross-correlation with MAGNITUDES at other orientations/scales: | ||
228 | cousins = reshape(apyr(cousinInd), [cousinSz Nor]); | ||
229 | nc = size(cousins,2); np = size(parents,2); | ||
230 | if (np == 0) | ||
231 | [cousins, snr3(niter,nsc)] = adjustCorr1s(cousins, C0(1:nc,1:nc,nsc), 2, p); | ||
232 | else | ||
233 | [cousins, snr3(niter,nsc), snr4(niter,nsc)] = ... | ||
234 | adjustCorr2s(cousins, C0(1:nc,1:nc,nsc), parents, Cx0(1:nc,1:np,nsc), 3, p); | ||
235 | end | ||
236 | if (var2(imag(cousins))/var2(real(cousins)) > 1e-6) | ||
237 | fprintf(1,'Non-trivial imaginary part, mag crossCorr, lev=%d!\n',nsc); | ||
238 | else | ||
239 | cousins = real(cousins); | ||
240 | ind = cousinInd; | ||
241 | apyr(ind) = vector(cousins); | ||
242 | end | ||
243 | |||
244 | %% Adjust autoCorr of mag responses | ||
245 | nband = (nsc-1)*Nor+2; | ||
246 | Sch = min(pind(nband,:)/2); | ||
247 | nz = sum(sum(~isnan(ace0(:,:,nsc,1)))); | ||
248 | lz = (sqrt(nz)-1)/2; | ||
249 | le = min(Sch/2-1,lz); | ||
250 | for nor = 1:Nor, | ||
251 | nband = (nsc-1)*Nor+nor+1; | ||
252 | ch = pyrBand(apyr,pind,nband); | ||
253 | [ch, snr1(niter,nband-1)] = modacor22(ch,... | ||
254 | ace0(la-le+1:la+le+1,la-le+1:la+le+1,nsc,nor), p); | ||
255 | ch = real(ch); | ||
256 | ind = pyrBandIndices(pind,nband); | ||
257 | apyr(ind) = ch; | ||
258 | %% Impose magnitude: | ||
259 | mag = apyr(ind) + magMeans0(nband); | ||
260 | mag = mag .* (mag>0); | ||
261 | pyr(ind) = pyr(ind) .* (mag./(abs(pyr(ind))+(abs(pyr(ind))<eps))); | ||
262 | end | ||
263 | end % cmask(3) | ||
264 | |||
265 | %% Adjust cross-correlation of REAL PARTS at other orientations/scales: | ||
266 | cousins = reshape(real(pyr(cousinInd)), [cousinSz Nor]); | ||
267 | Nrc = size(cousins,2); Nrp = size(rparents,2); | ||
268 | if cmask(4) & (Nrp ~= 0) | ||
269 | a3 = 0; a4 = 0; | ||
270 | for nrc = 1:Nrc, | ||
271 | cou = cousins(:,nrc); | ||
272 | [cou, s3, s4] = ... | ||
273 | adjustCorr2s(cou,mean(cou.^2),rparents,Crx0(nrc,1:Nrp,nsc), 3); | ||
274 | a3 = s3 + a3; | ||
275 | a4 = s4 + a4; | ||
276 | cousins(:,nrc) = cou; | ||
277 | end | ||
278 | snr4r(niter,nsc) = a4/Nrc; | ||
279 | end | ||
280 | if (var2(imag(cousins))/var2(real(cousins)) > 1e-6) | ||
281 | fprintf(1,'Non-trivial imaginary part, real crossCorr, lev=%d!\n',nsc); | ||
282 | else | ||
283 | %%% NOTE: THIS SETS REAL PART ONLY - signal is now NONANALYTIC! | ||
284 | pyr(cousinInd) = vector(cousins(1:Nor*cousinSz)); | ||
285 | end | ||
286 | |||
287 | %% Re-create analytic subbands | ||
288 | dims = pind(firstBnum,:); | ||
289 | ctr = ceil((dims+0.5)/2); | ||
290 | ang = mkAngle(dims, 0, ctr); | ||
291 | ang(ctr(1),ctr(2)) = -pi/2; | ||
292 | for nor = 1:Nor, | ||
293 | nband = (nsc-1)*Nor+nor+1; | ||
294 | ind = pyrBandIndices(pind,nband); | ||
295 | ch = pyrBand(pyr, pind, nband); | ||
296 | ang0 = pi*(nor-1)/Nor; | ||
297 | xang = mod(ang-ang0+pi, 2*pi) - pi; | ||
298 | amask = 2*(abs(xang) < pi/2) + (abs(xang) == pi/2); | ||
299 | amask(ctr(1),ctr(2)) = 1; | ||
300 | amask(:,1) = 1; | ||
301 | amask(1,:) = 1; | ||
302 | amask = fftshift(amask); | ||
303 | ch = ifft2(amask.*fft2(ch)); % "Analytic" version | ||
304 | pyr(ind) = ch; | ||
305 | end | ||
306 | |||
307 | %% Combine ori bands | ||
308 | bandNums = [1:Nor] + (nsc-1)*Nor+1; %ori bands only | ||
309 | ind1 = pyrBandIndices(pind, bandNums(1)); | ||
310 | indN = pyrBandIndices(pind, bandNums(Nor)); | ||
311 | bandInds = [ind1(1):indN(length(indN))]; | ||
312 | %% Make fake pyramid, containing dummy hi, ori, lo | ||
313 | fakePind = pind([bandNums(1), bandNums, bandNums(Nor)+1],:); | ||
314 | fakePyr = [zeros(prod(fakePind(1,:)),1);... | ||
315 | real(pyr(bandInds)); zeros(prod(fakePind(size(fakePind,1),:)),1)]; | ||
316 | ch = reconSFpyr(fakePyr, fakePind, [1]); % recon ori bands only | ||
317 | im = real(expand(im,2))/4; | ||
318 | im = im + ch; | ||
319 | vari = acr0(la+1:la+1,la+1:la+1,nsc); | ||
320 | if cmask(2), | ||
321 | if vari/var0 > 1e-4, | ||
322 | [im, snr2(niter,nsc)] = ... | ||
323 | modacor22(im, acr0(la-le+1:la+le+1,la-le+1:la+le+1,nsc), p); | ||
324 | else | ||
325 | im = im*sqrt(vari/var2(im)); | ||
326 | end | ||
327 | end % cmask(2) | ||
328 | im = real(im); | ||
329 | |||
330 | if cmask(1), | ||
331 | %% Fix marginal stats | ||
332 | if vari/var0 > 1e-4, | ||
333 | [im,snr7(niter,2*nsc-1)] = modskew(im,skew0p(nsc),p); % Adjusts skewness | ||
334 | [im,snr7(niter,2*nsc)] = modkurt(im,kurt0p(nsc),p); % Adjusts kurtosis | ||
335 | end | ||
336 | end % cmask(1) | ||
337 | |||
338 | end %END Coarse-to-fine loop | ||
339 | |||
340 | %% Adjust variance in HP, if higher than desired | ||
341 | if (cmask(2)|cmask(3)|cmask(4)), | ||
342 | ind = pyrBandIndices(pind,1); | ||
343 | ch = pyr(ind); | ||
344 | vHPR = mean2(ch.^2); | ||
345 | if vHPR > vHPR0, | ||
346 | ch = ch * sqrt(vHPR0/vHPR); | ||
347 | pyr(ind) = ch; | ||
348 | end | ||
349 | end % cmask | ||
350 | im = im + reconSFpyr(real(pyr), pind, [0]); %recon hi only | ||
351 | |||
352 | %% Pixel statistics | ||
353 | means = mean2(im); | ||
354 | vars = var2(im, means); | ||
355 | snr7(niter,2*(Nsc+1)+1) = snr(var0,var0-vars); | ||
356 | im = im-means; % Adjusts mean and variance | ||
357 | [mns mxs] = range2(im + mean0); | ||
358 | snr7(niter,2*(Nsc+1)+2) = snr(mx0-mn0,sqrt((mx0-mxs)^2+(mn0-mns)^2)); | ||
359 | if cmask(1), | ||
360 | im = im*sqrt(((1-p)*vars + p*var0)/vars); | ||
361 | end % cmaks(1) | ||
362 | im = im+mean0; | ||
363 | if cmask(1), | ||
364 | [im, snr7(niter,2*(Nsc+1)+3)] = modskew(im,skew0,p); % Adjusts skewness (keep mean and variance) | ||
365 | [im, snr7(niter,2*(Nsc+1)+4)] = modkurt(im,kurt0,p); % Adjusts kurtosis (keep mean and variance, | ||
366 | % but not skewness) | ||
367 | im = max(min(im,(1-p)*max(max(im))+p*mx0),... | ||
368 | (1-p)*min(min(im))+p*mn0); % Adjusts range (affects everything) | ||
369 | else | ||
370 | snr7(niter,2*(Nsc+1)+3) = snr(skew0,skew0-skew2(im)); | ||
371 | snr7(niter,2*(Nsc+1)+4) = snr(kurt0,kurt0-kurt2(im)); | ||
372 | end % cmask(1) | ||
373 | |||
374 | %% Force pixels specified by image mask | ||
375 | if (exist('imask') & ~isempty(imask) ) | ||
376 | im = mask.*reshape(imask(:,2 - (size(imask,2)==1)),size(im)) + ... | ||
377 | (1-mask).*im; | ||
378 | end | ||
379 | |||
380 | snr6(niter,1) = snr(im-mean0,im-prev_im); | ||
381 | |||
382 | if floor(log2(niter))==log2(niter), | ||
383 | nq = nq + 1; | ||
384 | imS(:,:,nq) = im; | ||
385 | end | ||
386 | |||
387 | tmp = prev_im; | ||
388 | prev_im=im; | ||
389 | |||
390 | figure(imf); | ||
391 | subplot(1,2,1); | ||
392 | showIm(im-tmp,'auto',1); title('Change'); | ||
393 | subplot(1,2,2); | ||
394 | showIm(im,'auto',1); title(sprintf('iteration %d/%d',niter,Niter)); | ||
395 | drawnow | ||
396 | |||
397 | % accelerator | ||
398 | alpha = 0.8; | ||
399 | im = im + alpha*(im - tmp); | ||
400 | |||
401 | commented = 1; % set it to 0 for displaying convergence of parameters in SNR (dB) | ||
402 | if ~commented, | ||
403 | |||
404 | % The graphs that appear reflect | ||
405 | % the relative distance of each parameter or group | ||
406 | % of parametersi, to the original's, in decibels. | ||
407 | % Note, however, that when the original parameters | ||
408 | % are close to zero, this measurement is meaningless. | ||
409 | % This is why in some cases it seems that some of | ||
410 | % the parameters do not converge at all. | ||
411 | |||
412 | figure(snrf); | ||
413 | if cmask(1) | ||
414 | subplot(171); plot(snr7); title('Mrgl stats'); | ||
415 | end | ||
416 | if cmask(2), | ||
417 | subplot(172); plot(snr2); title('Raw auto'); | ||
418 | end | ||
419 | if cmask(3), | ||
420 | subplot(173); plot(snr1); title('Mag auto'); | ||
421 | subplot(174); plot(snr3); title('Mag ori'); | ||
422 | subplot(175); plot(snr4); title('Mag scale'); | ||
423 | end | ||
424 | if (Nrp > 0) & cmask(4), | ||
425 | subplot(176); plot(snr4r); title('Phs scale'); | ||
426 | end | ||
427 | subplot(177); plot(snr6); title('Im change'); | ||
428 | drawnow | ||
429 | |||
430 | end % if ~commented | ||
431 | |||
432 | end %END MAIN LOOP | ||
433 | |||
434 | im = prev_im; | ||
435 | |||
436 | snrP = [snr7 snr2 snr1 snr3 snr4 snr4r snr6]; | ||