


clear; 
load 'model';
sub = 1; Lrange = 6; % for faster speed, you can remove this line. 

% TESTING IMAGE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
totalScale = 10;  % total number of scales
Sx1 = zeros(1, totalScale); Sy1 = zeros(1, totalScale); 
for(i=1:totalScale)
    J{i} = imresize(double(rgb2gray(imread('womanfind2.jpg'))), .40+(i-1)*.03, 'nearest');  % scaled images
    [sx1, sy1] = size(J{i}); 
    Sx1(i) = sx1; Sy1(i) = sy1; % sizes of images
    tss1{i} = zeros(sx1, sy1);  % log-likelihood score map for testing images
    sym1{i} = zeros(sx1, sy1);  % symbolic sketch on testing image
end
mscale = zeros(1, totalScale);  % log-likelihood score over scales
ax = floor(sx/2); ay = floor(sy/2); % half size of bounding box


% VARIABLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(['start filtering']);
tic
allfiltered1 = applyfilterfftms(J, allfilter, Sx1(totalScale), Sy1(totalScale));  % filter training images
allmaxed = initialmax(J, allfilter);  % to be changed to local maximum
disp(['filtering time: ' num2str(toc) ' seconds']);

% MEX C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mex Cfind.c;   % compile C code
disp(['start finding']);
tic
Cfind(totalScale, norient, allfiltered1, allmaxed, h, ax, ay, Mi, Mx, My, Mm, allsymbol(1, :), Lrange, Orange, sub, Totalsketch, Upperbound, tss1, sym1, Sx1, Sy1, mscale, LIK);
disp(['mex-C finding time: ' num2str(toc) ' seconds']);

% RESULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
towrite = double(rgb2gray(imread('womanfind2.jpg'))); 
outeps(towrite, 'ex2_3_original'); 
towrite = uint8(255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
imwrite(towrite, ['ex2_3_original' '.gif'],'gif');
for(i=1:totalScale) 
  towrite = J{i}+sym1{i}*100;  
  outeps(towrite, ['ex2_3_scale' num2str(i)]); 
  towrite = uint8(255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
  imwrite(towrite, ['ex2_3_scale' num2str(i) '.gif'],'gif');
end






