function [ res ] = verify( im1, im2, x, y, scale )

im1 = im2double(im1);
[rows, cols, channels] = size(im1);

im2 = im2double(im2);

% generate our downsampled target patch
patch = imresize(im2, scale, 'bilinear');
[prows, pcols, pchannels] = size(patch);

[patchgx,patchgy] = gradient(patch);
patchg = patchgx + patchgy;


x1 = round(x - (pcols/2));
y1 = round(y - (prows/2));
x2 = x1 + pcols - 1;
y2 = y1 + prows - 1;

underpatch = im1(y1:y2, x1:x2, :);
[im1gx,im1gy] = gradient(underpatch);
im1g = im1gx + im1gy;

% compute SSD
diff = underpatch - patch;
diff = diff .* diff;

gdiff = im1g - patchg;
gdiff = gdiff .* gdiff;

%{
% weight our SSDs by an inverted gaussian
mask = fspecial('gaussian', 64, 15);
mask = max(mask(:)) - mask;
mask = imresize(mask,[prows pcols], 'bilinear');

maskRGB(:,:,1) = mask;
maskRGB(:,:,2) = mask;
maskRGB(:,:,3) = mask;

diff = diff .* maskRGB;
gdiff = gdiff .* maskRGB;
%}

%{
diffr = sum(sum(diff(:,:,1)));
diffg = sum(sum(diff(:,:,2)));
diffb = sum(sum(diff(:,:,3)));

gdiffr = sum(sum(gdiff(:,:,1)));
gdiffg = sum(sum(gdiff(:,:,2)));
gdiffb = sum(sum(gdiff(:,:,3)));

res = (max([diffr diffg diffb]) + max([gdiffr gdiffg gdiffb])) / (prows * pcols);
%}

res = (sum(diff(:)) + sum(gdiff(:))) / (prows * pcols);


end






























