
% REPRODUCING EXPERIMENT 1

clear; 

% CONSTANTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
scale = .7;  % scale of Gabors, length = 17 pixels
norient = 15;  % number of orientations
epsilon = .1;  % allowed correlation between selected Gabors
Totalsketch = 30;  % allowed number of selected Gabors   
Upperbound = 16.; % saturation value
sub = 1;    % subsampling rate for Gabor
Lrange = 6; % allowed shifting in location (times sub pixels)
Orange = 2; % allowed shifting in orientation
%%% Note: Lrange*sub is the pixels allowed to shift along normal
%%%       Lrange*sub should be the same in learning and testing

LIK = 1.; SHUTUP = 0.;  % use log-likelihood score. If LIK=0, then use active correlation
% SHUTUP is to neglect the positions with low average responses          
% LIK = 0; SHUTUP = 1.;   
fold = .7; 


% TRAINING EXAMPLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Folder = 'sailboat';  % image input folder
Iname = dir([Folder '\*.jpg']);
nimage = size(Iname, 1);
Isize = zeros(nimage, 2);
for (i = 1 : nimage)
    I{i} = imresize(double(rgb2gray(imread([Folder '\' Iname(i).name]))), fold, 'nearest');
    %Ishow{i} = imread([Folder '\' Iname(i).name]); 
    Isize(i, :) = size(I{i});
end
sx = min(Isize(:, 1)); sy = min(Isize(:, 2)); 
for (i = 1 : nimage)
    I{i} = I{i}(1:sx, 1:sy); 
end   % make the sizes of the training images the same


% FILTERING IMAGES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(['start filtering']);
tic
[allfilter, allsymbol] = makefilter(scale, norient);  % generate Gabor filters 
h = (size(allfilter{1}, 1)-1)/2;  % half size of Gabor
C = corr(allfilter, epsilon);    % generate the inhibition maps
allfiltered = applyfilterfftsame(I, allfilter);  % filter training images
disp(['filtering time: ' num2str(toc) ' seconds']);

% VARIABLES FOR MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Mi = zeros(1, Totalsketch);  % orientation of selected Gabor
Mx = zeros(1, Totalsketch);  % position of selected Gabor
My = zeros(1, Totalsketch); 
Mm = zeros(1, Totalsketch);  % average response of selected Gabor
sym = zeros(size(I{1}));  % symbolic plot of selected Gabor
Asym = cell(1, nimage);   % symbolic plot for each image with active Gabor
for (i = 1 : nimage)
    Asym{i} = zeros(size(I{1}));  
end
tss = zeros(nimage, 1);  % total scores for testing images
gain = zeros(Totalsketch, 1);  % coding gain for selected Gabor


binnum = 50; % number of bins 
% MEX-C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
mex Clearn.c;   % compile C code
disp(['start learning']);
tic
Clearn(nimage, norient, allfiltered, C, h, sx, sy, Mi, Mx, My, Mm, allsymbol(1, :), sym, Asym, Lrange, Orange, sub, Totalsketch, Upperbound, tss, gain, LIK, SHUTUP, binnum);
disp(['mex-C learning time: ' num2str(toc) ' seconds']);

% RESULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K = nimage; bx =5; by = 5; cc = 3;  
[tsssort, ind] = sort(tss); 
col = 6; row = floor(K/col)+1;
Iout = zeros(row*sx+(row-1)*(2*bx+cc), 2*col*sy+(col-1)*(2*by+cc))+255; 
towrite =  -sym; 
towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
Iout(1:sx, 1:sy) = towrite;
i = -1;  
for (r = 1 : row)
    for (c = 1 : col)
        i = i+1; 
        if ((i>=1)&&(i<=K))
            towrite = I{ind(nimage-i+1)}; 
            towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
            Iout((r-1)*sx+1+(r-1)*(2*bx+cc): r*sx+(r-1)*(2*bx+cc), (c-1)*2*sy+1+(c-1)*(2*by+cc): (c-1)*2*sy+sy+(c-1)*(2*by+cc)) = towrite; 
            towrite =  -(Asym{ind(nimage-i+1)}); 
            towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
            Iout((r-1)*sx+1+(r-1)*(2*bx+cc) : r*sx+(r-1)*(2*bx+cc), (c-1)*2*sy+sy+(c-1)*(2*by+cc)+1 : (c-1)*2*sy+sy+(c-1)*(2*by+cc)+sy) =towrite;  
        end
    end
end
for (r = 1 : row-1)
  Iout(r*sx+(r-1)*(2*bx+cc)+bx+1:r*sx+(r-1)*(2*bx+cc)+bx+cc, :) = 0; 
end
for (c = 1 : col-1)
  Iout(:, c*(sy*2)+(c-1)*(2*by+cc)+by+1:c*(sy*2)+(c-1)*(2*by+cc)+by+cc) = 0; 
end
outepsgif(Iout, 'ex1_7');

save 'model' norient h sx sy Mi Mx My Mm allfilter allsymbol Lrange Orange sub Totalsketch Upperbound LIK;

