% ObjectSearch.m
% Tom McGrath, 2004
% CIS 601, Temple University
%
%
% This program attempts to find objects of a given template in a 
% collection of images.
%



% Using correlation
% find the flower object
files = dir('flowers');
inputimage = imread('sourceflower.jpg');

% Extract a 2-dimensional matrix from the jpeg file
inputimage = inputimage(:,:);
inputimage = inputimage(:,1:32);

% show the image template
imshow( inputimage );
pause;

for i = 3 : size(files);
    targetimage = imread( ['flowers\',files(i).name]);
    
    % Extract a 2-dimensional matrix from the jpeg file
    targetimage = targetimage(:,:);
    targetimage = targetimage(:,1:1600);
    
    % show the image to be processed
    imshow( targetimage );
    pause;
    
    
   % filt = fspecial('sobel');
    %timage = im2double(tempimage);
    %img = imfilter( timage, filt, 'replicate');
    %imshow(img);
    %pause;
    
    % compute the correlation image
    g = dftcorr( targetimage, inputimage );
    % obtain grayscale image of correlation result
    gs = gscale(g);
    % show the grayscale image
    imshow(gs);
    pause;
    % compute the point of greatest correlation
    [I, J] = find( g == max(g(:)))
    % show the threshold image of the correlation
    imshow( gs > 254 );
    pause;
    
end
    
% end flower example



% find the coin example
clear all;
files = dir('coins');

inputimage = imread('sourcecoin.jpg');

% Extract a 2-dimensional matrix from the jpeg file
inputimage = inputimage(:,:);
inputimage = inputimage(:,1:52);

% show the image template
imshow( inputimage );
pause;

for i = 3 : size(files);
    targetimage = imread( ['coins\',files(i).name]);
    
    % Extract a 2-dimensional matrix from the jpeg file
    targetimage = targetimage(:,:);
    targetimage = targetimage(:,1:1600);
    
    % show the image to be processed
    imshow( targetimage );
    pause;
    
    % compute the correlation image
    g = dftcorr( targetimage, inputimage );
    % obtain grayscale image of correlation result
    gs = gscale(g);
    % show the grayscale image
    imshow(gs);
    pause;
    % compute the point of greatest correlation
    [I, J] = find( g == max(g(:)))
    % show the threshold image of the correlation
    imshow( gs > 254 );
    pause;
    
end

% end coin example




% find the stapler example
clear all;
files = dir('stapler');

inputimage = imread('sourcestapler.jpg');

% Extract a 2-dimensional matrix from the jpeg file
inputimage = inputimage(:,:);
inputimage = inputimage(:,1:320);

% show the image template
imshow( inputimage );
pause;

for i = 3 : size(files);
    targetimage = imread( ['stapler\',files(i).name]);
    
    % Extract a 2-dimensional matrix from the jpeg file
    targetimage = targetimage(:,:);
    targetimage = targetimage(:,1:1600);
    
    % show the image to be processed
    imshow( targetimage );
    pause;
    
    % compute the correlation image
    g = dftcorr( targetimage, inputimage );
    % obtain grayscale image of correlation result
    gs = gscale(g);
    % show the grayscale image
    imshow(gs);
    pause;
    % compute the point of greatest correlation
    [I, J] = find( g == max(g(:)))
    % show the threshold image of the correlation
    imshow( gs > 254 );
    pause;
    
end

% end stapler example

% end program
clear all;
%clc;

