summaryrefslogtreecommitdiffstats
path: root/SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m
diff options
context:
space:
mode:
authorleochanj <jbakita@cs.unc.edu>2020-10-21 01:52:54 -0400
committerleochanj <jbakita@cs.unc.edu>2020-10-21 01:52:54 -0400
commit25d94aa8aabb8ac3e8bbea0bc439ea6148444cc8 (patch)
treeba80e76d25d9ca9486092e2f6b6d76f0e3352bf7 /SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m
parente2b50015cebdfba68699abd6e8575e38230f5a78 (diff)
debug libextra and remove matlab
Diffstat (limited to 'SD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m')
-rwxr-xr-xSD-VBS/benchmarks/texture_synthesis/src/matlab/textureSynthesis.m436
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 @@
1function [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
43Warn = 0; % Set to 1 if you want to see warning messages
44
45%% Check required args are passed:
46if (nargin < 2)
47 error('Function called with too few input arguments');
48end
49
50if ( ~exist('Niter') | isempty(Niter) )
51 Niter = 50;
52end
53
54if (exist('cmask') & ~isempty(cmask) )
55 cmask = (cmask > 0.5); % indices of ones in mask
56else
57 cmask = ones(4,1);
58end
59
60%% Extract parameters
61statg0 = params.pixelStats;
62mean0 = statg0(1); var0 = statg0(2);
63skew0 = statg0(3); kurt0 = statg0(4);
64mn0 = statg0(5); mx0 = statg0(6);
65statsLPim = params.pixelLPStats;
66skew0p = statsLPim(:,1);
67kurt0p = statsLPim(:,2);
68vHPR0 = params.varianceHPR;
69acr0 = params.autoCorrReal;
70ace0 = params.autoCorrMag;
71magMeans0 = params.magMeans;
72C0 = params.cousinMagCorr;
73Cx0 = params.parentMagCorr;
74Crx0 = params.parentRealCorr;
75
76%% Extract {Nsc, Nor, Na} from params
77tmp = size(params.autoCorrMag);
78Na = tmp(1); Nsc = tmp(3);
79Nor = tmp(length(tmp))*(length(tmp)==4) + (length(tmp)<4);
80la = (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.
85if ( 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);
91else
92 im = im0;
93end
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);
99nth = log2(min(Ny,Nx)/Na);
100if nth<Nsc+1 & Warn,
101 fprintf(1,'Warning: Na will be cut off for levels above #%d !\n',floor(nth));
102end
103
104if 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
118end
119
120imf = max(1,gcf-1); snrf = imf+1;
121figure(imf); clf
122subplot(1,2,1); grayRange = showIm(im,'auto',1); title('Starting image');
123drawnow
124
125prev_im=im;
126
127imf = max(1,gcf-1);
128figure(imf);
129clf;showIm(im,'auto',1); title(sprintf('iteration 0'));
130
131nq = 0;
132Nq = floor(log2(Niter));
133imS = zeros(Ny,Nx,Nq);
134
135%% MAIN LOOP
136for niter = 1:Niter
137
138%p = niter/Niter;
139p = 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);
166if 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);
177end % cmask(2)
178if 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
183end % cmask(2)
184
185 %% Subtract mean of magnitude
186if 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
193end % 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
204if (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
224end % if (cmask(3) | cmask(4))
225
226if 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
263end % 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);
320if 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
327end % cmask(2)
328 im = real(im);
329
330if 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
336end % cmask(1)
337
338 end %END Coarse-to-fine loop
339
340 %% Adjust variance in HP, if higher than desired
341if (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
349end % 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));
359if cmask(1),
360 im = im*sqrt(((1-p)*vars + p*var0)/vars);
361end % cmaks(1)
362 im = im+mean0;
363if 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)
369else
370 snr7(niter,2*(Nsc+1)+3) = snr(skew0,skew0-skew2(im));
371 snr7(niter,2*(Nsc+1)+4) = snr(kurt0,kurt0-kurt2(im));
372end % 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
401commented = 1; % set it to 0 for displaying convergence of parameters in SNR (dB)
402if ~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
412figure(snrf);
413if cmask(1)
414 subplot(171); plot(snr7); title('Mrgl stats');
415end
416if cmask(2),
417 subplot(172); plot(snr2); title('Raw auto');
418end
419if 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');
423end
424if (Nrp > 0) & cmask(4),
425 subplot(176); plot(snr4r); title('Phs scale');
426end
427 subplot(177); plot(snr6); title('Im change');
428 drawnow
429
430end % if ~commented
431
432end %END MAIN LOOP
433
434im = prev_im;
435
436snrP = [snr7 snr2 snr1 snr3 snr4 snr4r snr6];