im1=imread('C:\Users\boschm1\src\3rdparty\SFMedu2\images\B21.jpg');
im2=imread('C:\Users\boschm1\src\3rdparty\SFMedu2\images\B22.jpg');

im1=imresize(imfilter(im1,fspecial('gaussian',7,1.),'same','replicate'),0.5,'bicubic');
im2=imresize(imfilter(im2,fspecial('gaussian',7,1.),'same','replicate'),0.5,'bicubic');

im1=im2double(im1);
im2=im2double(im2);

%figure;imshow(im1);figure;imshow(im2);

cellsize=3;
gridspacing=1;

addpath(fullfile(pwd,'mexDenseSIFT'));
addpath(fullfile(pwd,'mexDiscreteFlow'));

sift1 = mexDenseSIFT(im1,cellsize,gridspacing);
sift2 = mexDenseSIFT(im2,cellsize,gridspacing);

SIFTflowpara.alpha=2*255;
SIFTflowpara.d=40*255;
SIFTflowpara.gamma=0.005*255;
SIFTflowpara.nlevels=8;
SIFTflowpara.wsize=1;
SIFTflowpara.topwsize=5;
SIFTflowpara.nTopIterations = 60;
SIFTflowpara.nIterations= 30;


tic;[vx,vy,energylist]=SIFTflowc2f(sift1,sift2,SIFTflowpara);toc

warpI2=warpImage(im2,vx,vy);
figure;imshow(im1);figure;imshow(warpI2)

% display flow
clear flow;
flow(:,:,1)=vx-mean(vx(:));
flow(:,:,2)=vy-mean(vy(:));
figure;imshow(flowToColor(flow));axis on

M=round(size(im1,2)/10);
N=round(size(im1,1)/10);
[x,y]=meshgrid(1:M,1:N);

% depth=sqrt(flow(:,:,1).^2+flow(:,:,2).^2);
% depth=255*depth./max(depth(:));
% depthres=imresize(depth,[N M],'cubic');
% figure;imshow(uint8(depth))
% figure;mesh(x,y,depthres)

addpath(genpath('./flow_code_v2/'))
% %  addpath('../flow_code_v2/utils/')
uvo=estimate_flow_interface(im1,im2,'classic+nl-fast');
%uvo = denoise_color_weighted_medfilt2(flow, rgb2gray(im1));
figure;imshow(flowToColor(uvo));axis on
uvo=estimate_flow_interface(im2,im1,'classic+nl-fast');
figure;imshow(flowToColor(-uvo));axis on
% %  depth=sqrt(uvo(:,:,1).^2+uvo(:,:,2).^2);
% %  depth=255*depth./max(depth(:));
% %  figure;imshow(uint8(depth))
% %  figure;mesh(x,y,depth)
return;

% this is the code doing the brute force matching
% tic;[flow2,energylist2]=mexDiscreteFlow(Sift1,Sift2,[alpha,alpha*20,60,30]);toc
% figure;imshow(flowToColor(flow2));
