
% REPRODUCING EXPERIMENT 1

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

epsilon = .1;  % allowed correlation between selected Gabors
Totalsketch = 60;  % allowed number of selected Gabors   
sub = 2;    % 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);  % 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;
[tsssort, ind] = sort(tss); 
figure; % display top K examples
col = 10; row = floor(K/col*2)+1; row = col;
subplot(row, col, 1, 'v6'); imshow(-(sym), []); 
for (i = 1 : K)
    subplot(row, col,  1+i*2, 'v6'); 
    imshow(I{ind(nimage-i+1)}, []);
    subplot(row, col,  2+i*2, 'v6');
    imshow(-(Asym{ind(nimage-i+1)}), []); 
end

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

