function [Overlay] = drawCorrespondences_ZNCC(image1, image2, fl_1, fl_2, n, min_resp, patch_size)
%Joshua Stough
%COMP 290-89, Pollefeys
%Assignment 2, Computing F
%This function takes to images as read from imread. It 
%produces three figure ouputs and returns the image 
%contained in one.
%-the two images, with feature points highlighted.
%-the first image, with feature points from the first
%image in green and the corresponding feature points
%from image two in blue-green, and a lines between
%such corresponding points.

if (nargin < 7) patch_size = 7; end
if (nargin < 6) min_resp = .7; end
if (nargin < 5) n = 500; end

if (nargin < 4) fl_2 = corner_detector(image2,n); end
if (nargin < 3) fl_1 = corner_detector(image1,n); end

drawFeatureList(image1,fl_1);
drawFeatureList(image2,fl_2);

corr = find_correspondences_ZNCC(fl_1, fl_2, image1, image2, min_resp, patch_size);

I = double(rgb2gray(image1));
I = I/max(max(I));

s = size(I);
Overlay = zeros(s(1),s(2),3);

Overlay(:,:,1) = I;
Overlay(:,:,2) = I;
Overlay(:,:,3) = I;

%for every point correspondence, color the points and the line between
%them.

for pix = corr
    %draw the image1 point in green.
    if (pix(1) > 0 & pix(2) > 0 & pix(1) <= s(1) & pix(2) <= s(2))
        Overlay(pix(1)-1,pix(2)-1,2) = 1;
        Overlay(pix(1)-1,pix(2),2) = 1;
        Overlay(pix(1)-1,pix(2)+1,2) = 1;
        Overlay(pix(1),pix(2)-1,2) = 1;
        %Overlay(pix(1),pix(2),2) = 1;
        Overlay(pix(1),pix(2)+1,2) = 1;
        Overlay(pix(1)+1,pix(2)-1,2) = 1;
        Overlay(pix(1)+1,pix(2),2) = 1;
        Overlay(pix(1)+1,pix(2)+1,2) = 1;
    end
    
    %draw the image2 point in blue_green.
    if (pix(3) > 0 & pix(4) > 0 & pix(3) <= s(1) & pix(4) <= s(2))
        Overlay(pix(3)-1,pix(4)-1,2) = .7;
        Overlay(pix(3)-1,pix(4),2) = .7;
        Overlay(pix(3)-1,pix(4)+1,2) = .7;
        Overlay(pix(3),pix(4)-1,2) = .7;
        %Overlay(pix(3),pix(4),2) = .7;
        Overlay(pix(3),pix(4)+1,2) = .7;
        Overlay(pix(3)+1,pix(4)-1,2) = .7;
        Overlay(pix(3)+1,pix(4),2) = .7;
        Overlay(pix(3)+1,pix(4)+1,2) = .7;
        
        Overlay(pix(3)-1,pix(4)-1,3) = 1;
        Overlay(pix(3)-1,pix(4),3) = 1;
        Overlay(pix(3)-1,pix(4)+1,3) = 1;
        Overlay(pix(3),pix(4)-1,3) = 1;
        %Overlay(pix(3),pix(4),3) = 1;
        Overlay(pix(3),pix(4)+1,3) = 1;
        Overlay(pix(3)+1,pix(4)-1,3) = 1;
        Overlay(pix(3)+1,pix(4),3) = 1;
        Overlay(pix(3)+1,pix(4)+1,3) = 1;
    end
    
    %draw a green line between the two points.
    numsteps = 2*sqrt(sum((pix(1:2) - pix(3:4)).^2));
    dir = (pix(3:4)- pix(1:2))/numsteps;
    
    cur = pix(1:2) - dir;
    for i = 1:numsteps
        cur = cur + dir;
        Overlay(round(cur(1)),round(cur(2)),2) = 1;
    end
    
end

figure, imshow(Overlay,[0 1],'truesize');
