Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [u dt cur energy] = match_pdfs_2d_haker( pdf1, pdf2)
- % normalize pdfs
- cumsum1 = cumIntegrate(pdf1(:));
- cumsum2 = cumIntegrate(pdf2(:));
- pdf1 = pdf1/cumsum1(end);
- pdf2 = pdf2/cumsum2(end);
- [M N] = size(pdf1);
- % find an initial map, by doing 1D matchings in x and then in y.
- proj1 = zeros(1,N);
- proj2 = zeros(1,N);
- for i=1:N
- cumsum1 = cumIntegrate(pdf1(:,i));
- cumsum2 = cumIntegrate(pdf2(:,i));
- proj1(i) = cumsum1(end);
- proj2(i) = cumsum2(end);
- end;
- a = find_warp(proj2, proj1);
- ga = gradientAccurate(a);
- [X,Y] = meshgrid(1:N, 1:M);
- interpPDF2 = interp2(X,Y,pdf2, a, 1:M, 'spline',0.);
- b=zeros(M,N);
- for i=1:N
- b(:,i) = find_warp( interpPDF2(:, i).*ga(i), pdf1(:,i));
- end;
- %u^0 = (a,b)
- u=reshape([repmat(a', [M 1]) b], M, N,2);
- %%%%% debug : compute errors %%%%%%%%%%
- ta = interp1(1:N, proj2, a, 'spline',0.).*abs(ga);
- fprintf('error 1D:%f\n',norm(proj1-ta')/norm(proj1));
- [dbdx dbdy] = gradientAccurate(b);
- interpResult = interp2(X,Y,pdf2, u(:,:,1), u(:,:,2), 'spline',0.);
- img=interpResult.*repmat(ga, 1, N).*dbdy;
- fprintf('error 2D:%f\n',norm(img(:)-pdf1(:))/norm(pdf1(:)));
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %let's a constant number of iterations
- for t=1:3000
- %%%%%%%% compute grad^orthog Laplace^{-1} div(u^orthog) %%%%%%%%%
- %%%%%%%% where orthog stands for a 90 degrees rotations,%%%%%%%%%
- %%%%%%%% and Laplace^-1(g) solves Laplacian f = g %%%%%%%%%
- [dudx dudy] = gradientAccurate(u);
- divorthog = -dudy(:,:,1)+dudx(:,:,2); %div(u^orthog)
- f=reshape(poicalc(divorthog(:),1,1,M,N), M, N); %Laplace^{-1} div(u^orthog)
- [dfdx dfdy] = gradientAccurate(f); %grad Laplace^{-1} div(u^orthog)
- % 1./pdf1 * grad^orthog Laplace^{-1} div(u^orthog)
- update1 = repmat(1./pdf1, [1 1 2]).*reshape([-dfdy dfdx], size(u));
- %%%%%%%% upwind for Du %%%%%%%%%%%%%%%%%
- [dudx dudy] = gradientAccurate(u);
- dudxm = dudx;
- dudym = dudy;
- dudxp = dudx;
- dudyp = dudy;
- dudxm(:, 3:end-2, :) = (3*u(:, 3:end-2, :) - 4*u(:, 2:end-3, :) + u(:, 1:end-4, :))*0.5;
- dudxp(:, 3:end-2, :) = (-3*u(:, 3:end-2, :) + 4*u(:, 4:end-1, :) - u(:, 5:end, :))*0.5;
- dudym(3:end-2, :, :) = (3*u(3:end-2, :, :) - 4*u(2:end-3, :, :) + u(1:end-4, :, :))*0.5;
- dudyp(3:end-2, :, :) = (-3*u(3:end-2, :, :) + 4*u(4:end-1, :, :) - u(5:end, :, :))*0.5;
- %Du = abs(dudx(:,:,1).*dudy(:,:,2)-dudx(:,:,2).*dudy(:,:,1)); %centered
- Dupp = abs(dudxp(:,:,1).*dudyp(:,:,2)-dudxp(:,:,2).*dudyp(:,:,1));
- Dupm = abs(dudxp(:,:,1).*dudym(:,:,2)-dudxp(:,:,2).*dudym(:,:,1));
- Dump = abs(dudxm(:,:,1).*dudyp(:,:,2)-dudxm(:,:,2).*dudyp(:,:,1));
- Dumm = abs(dudxm(:,:,1).*dudym(:,:,2)-dudxm(:,:,2).*dudym(:,:,1));
- Du = (update1(:, :,1)>0).*(update1(:, :,2)>0).*Dupp ...
- + (update1(:, :,1)>0).*(update1(:, :,2)<0).*Dupm ...
- + (update1(:, :,1)<0).*(update1(:, :,2)>0).*Dump ...
- + (update1(:, :,1)<0).*(update1(:, :,2)<0).*Dumm;
- dt(t) = 0.9*min(1./abs(update1(:))); %dt according to stability conditions
- %%%%% debug image : should remain constant
- interpResult = interp2(X,Y,pdf2, u(:,:,1), u(:,:,2), 'spline', 0.);
- img=interpResult.*Du;
- figure(1);
- image(img*N*N*65); colormap('gray');
- uxmid = u(:,:,1)-repmat(1:N, M, 1); %u-identity
- uymid = u(:,:,2)-repmat(transpose(1:M), 1, N);
- e = (uxmid.^2+uymid.^2).*pdf1;
- energy(t) = sum(e(:));
- cur(t) = sum(sum(curl(u(:,:,1),u(:,:,2))));
- fprintf('iter:%u\tdt:%e\tcurl:%f\tenergy:%f\n', t, dt(t), cur(t), energy(t));
- %%%%%%%%%%%%%%
- % update : du/dt = 1./pdf1 * det(Jac(u)) * update1
- u = u+dt(t).*repmat(Du, [1 1 2]).*update1;
- end;
- function y = find_warp(pdf1, pdf2) %1D matching
- cdf1 = cumIntegrate(pdf1);
- cdf2 = cumIntegrate(pdf2);
- cdf1 = cdf1/cdf1(end);
- cdf2 = cdf2/cdf2(end);
- N = length(cdf1);
- y = zeros(N, 1);
- % cursor = 2;
- % for i=1:N
- % desiredF = cdf2(i);
- %
- % while (cursor<N && cdf1(cursor)<desiredF)
- % cursor = cursor+1;
- % end;
- %
- % alpha = min(1, max(0,(desiredF - cdf1(cursor-1))/(cdf1(cursor)-cdf1(cursor-1))));
- % y(i) = ((cursor-1)*(1-alpha) + alpha*cursor );
- % end;
- % the one below is shorter and might be more accurate
- [ucdf1, uxx] = unique(cdf1, 'first');
- y = transpose(interp1(ucdf1,uxx,cdf2,'spline',N));
- function y = cumIntegrate(f) % generic function to approximate the cumulative integral
- y= cumtrapz(f); % or y=cumsum, or y=intgrad1 with the library
- function varargout=gradientAccurate(f)
- if (length(f)==length(f(:)))
- [varargout{1}] = gradient2(f); %or my 5th order gradient2(f) code
- else
- [varargout{1} varargout{2}] = gradient2(f); %or my 5th order gradient2(f) code
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement