%COMP 256 Assignment 2
%Adrian Ilie

%function disparity(im1, im2, hs, vs, dispr)
%computes disparity map between im1 and im2
%using a window size of hsXvs
%and zmncc for a disparity range dispr

%load and pad for vertical alignment
im1=imread('corner28.jpg.w.bmp');
im2=imread('corner29.jpg.w.bmp');
[h,w,d]=size(im1);
im2=imgpad(im2,h,w,18,0);
imwrite(im2,'corner29pad2.bmp','bmp');

%compute disparity images
m=disparityold(im1,im2,5,5,80);
[h,w,d]=size(m);
%left image is max on 3rd dimension
d1=max(m,[],3);
%right image is max on the oblique axis
d2=zeros(h,w);
for i=1:h
    for j=d+1:w
        for k=1:d
            if (m(i,j-d,k)>d2(i,j))
                d2(i,j)=m(i,j-d,k);
            end
        end
    end
end
savepfmraw('d1.pfm',d1);
savepfmraw('d2.pfm',d2);

%enforce coherence
[n1,n2]=nofalsematches(d1,d2,10);
savepfmraw('n1.pfm',n1);
savepfmraw('n2.pfm',n2);

%compute left from right and right from left
[p1,p2]=lfrrfl(im1,im2,n1,n2);
imwrite(p1,'p1.jpg','jpg');
imwrite(p2,'p2.jpg','jpg');
