
% REPRODUCING EXPERIMENT 1

clear; 
load 'nlf'; 
% CONSTANTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

epsilon = .1;  % allowed correlation between selected Gabors
Totalsketch = 50;  % allowed number of selected Gabors   
sub = 1;    % subsampling rate for Gabor
Lrange = 3; % allowed shifting in location (times sub pixels)
Orange = 1; % 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
SHUTUP = 0.; % SHUTUP is to neglect the positions with low average responses          
fold = .5; 


% TRAINING EXAMPLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Folder = 'bicycles';  % 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
Sx = zeros(1, nimage) + sx; 
Sy = zeros(1, nimage) + sy; 

% FILTERING IMAGES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(['start filtering']);
tic
C = corr(allfilter, epsilon);    % generate the inhibition maps
allfiltered = applyfilterfftsame(I, allfilter, localHalfx, localHalfy, thresholdFactor);  % filter training images
disp(['filtering time: ' num2str(toc) ' seconds']);

% the following three lines are the only things that need to be added
mex Ctransform.c; 
Ctransform(nimage, norient, allfiltered, Sx, Sy, Upperbound);


% 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);  % lambda
Mm1 = zeros(1, Totalsketch);  % logZ
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, Mm1, allsymbol(1, :), sym, Asym, Lrange, Orange, sub, Totalsketch, Upperbound, tss, gain, SHUTUP, binnum, M, lam, e, lz);
disp(['mex-C learning time: ' num2str(toc) ' seconds']);

% RESULTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
K = nimage; bx =0; by = 0; 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, 'LNbikeLearn'); 


save 'model' norient h sx sy Mi Mx My Mm Mm1 allfilter allsymbol Lrange Orange sub Totalsketch Upperbound M lam e lz localHalfx localHalfy thresholdFactor;

