%{
im1 = im2double(imread('test1.bmp'));
im2 = im2double(imread('test2.bmp'));
im3 = im2double(imread('test3.bmp'));
im4 = im2double(imread('test4.bmp'));
im5 = im2double(imread('test5.bmp'));

figure(1);
imshow(im1);

figure(2);
imshow(im2);

figure(3);
imshow(im3);

figure(4);
imshow(im4);

figure(5);
imshow(im5);
%}

num_imgs = 128;
img_offset = 0;

if (~(exist('skipsim')) || skipsim == 0)
    
skipsim = 1;
    
clear images;

fprintf('loading %i images...\n', num_imgs);
tic
for n = 1 : num_imgs
    %filename = sprintf('test%i.bmp', n);
    filename = sprintf('data2/image%03u.jpg', n + img_offset);
    images(n) = im2frame(im2double(imread(filename)));
end
fprintf('images loaded.\n');
toc

distmatrix = zeros(num_imgs);

fprintf('computing image histogram similarity matrix...\n');
tic
for n = 1 : num_imgs
    fprintf('.');
    for m = 1 : num_imgs
        if (n >= m)
            % set dist to very high
            distmatrix(n,m) = 0;
        else
            distmatrix(n,m) = imgdist(images(n).cdata, images(m).cdata);
        end    
    end
    if (mod(n,50) == 0)
        fprintf('\n');
    end
end
fprintf('\ndone.\n');
toc

distmatrix = distmatrix / max(distmatrix(:));
end % end skipsim



% *************************************************************
% compile a set of 'good' potential image matches
clear imgs;
i = 0;
num_potential_matches = 3000;

fprintf('computing best potential histogram matches...\n');
tic
for n = 1 : num_imgs
    fprintf('.');
    for m = 1 : num_imgs
        if (n >= m)
            % do nothing
            
        else
            i = i + 1;
            imgs(i,:) = [distmatrix(n,m) n m];
        end    
    end
    if (mod(n,50) == 0)
        fprintf('\n');
    end
end

imgs = sortrows(imgs);
potential_matches = imgs(1:num_potential_matches, :);

fprintf('\n%u best matches selected.\n', num_potential_matches);
toc


% *************************************************************
% now compute actual best patch distance for candidate matches

if (~(exist('skipmat')) || skipmat == 0)
    
skipmat = 1;

clear matches;

sym = 1; % are matches symmetric?  i.e. do we check both orders

fprintf('computing actual match sub-patch responses...\n');
tic

if (sym == 1)
    for i = 1 : num_potential_matches
    	cur = potential_matches(i,:);
        dist = cur(1);
        im1i = cur(2);
        im2i = cur(3);    
        % add the potential match, in the reverse order.
        potential_matches(num_potential_matches + i,:) = [dist im2i im1i];
    end
end

total = size(potential_matches,1);

for i = 1 : total
    fprintf('.');
	[resp x y scale] = calcpatch(im2double(images(potential_matches(i,2)).cdata), im2double(images(potential_matches(i,3)).cdata));
    matches(i,:) = [resp x y scale potential_matches(i,2) potential_matches(i,3)];
    if (mod(i,50) == 0 || i == num_potential_matches)
        fprintf('\n');
        fprintf('%u of %u complete.\n', i, total);
        toc
    end
end
fprintf('done.\n');
toc

matches = sortrows(matches, -1);

end % exist('skipmat') ...

clear hist;
hist = zeros(num_imgs,1);

clear ssds;
%ssds = zeros(num_imgs,1);

tic
% spit out best 5 matches?
for i = 1 : total
    %[resp x y scale im1i im2i] = matches(i,:);
    tmp = matches(i,:);
    x = tmp(2);
    y = tmp(3);
    scale = tmp(4);
    im1i = tmp(5);
    im2i = tmp(6);
    if (i <= 0)
        figure;
        res = zoom2(images(im1i).cdata, images(im2i).cdata, x, y, scale, 0, 10);
        imshow(res);
    end
    m(i) = mean(images(im2i).cdata(:));
    %fprintf('%u - %f\n', i, m(i));
    hist(im1i) = hist(im1i) + 1;
	hist(im2i) = hist(im2i) + 1;
    ssd = verify(images(im1i).cdata, images(im2i).cdata, x, y, scale);
    ssds(i,:) = [tmp(1)/ssd x y scale im1i im2i];
    %ssds(i,:) = [ssd x y scale im1i im2i];
    fprintf('.');
    if (mod(i,50) == 0 || i == total)
        fprintf('\n');
    end
end

fprintf('\n');

ssds = sortrows(ssds,-1);
toc








