%clear all; 
scale = 1.4;
norient = 15; ns = 3; 
expan = 20.; K = 3; 
nsketch = 50;
Lrange = 4; Orange = 1;
epsilon = .1; Upperbound = 6.; 
SHUTUP = 0.; 


% TRAINING EXAMPLES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Folder = 'd';  % image input folder
Iname = dir([Folder '\*.jpg']);
nimage = size(Iname, 1);
Isize = zeros(nimage, 2);
fold = .5; 
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


% getting the Gabor filters for analysis 

[allfilterr, allfilteri, allsymbol] = makefilter1(scale, norient, ns, expan, K); 
[Crr, Cri, Cir, Cii] = corr1(allfilterr, allfilteri);  
[allfilteredr, allfilteredi, allfiltered] = applyfilter1(I, allfilterr, allfilteri);
half = zeros(1, norient*ns+K); 
for (i=1:norient*ns+K)
    half(i) = (size(allfilterr{1, i}, 1)-1)/2;
end


syma = zeros(sx, sy); 

sym = cell(1, ns+1); 
for (i = 1 : ns+1)
    sym{i} = zeros(sx, sy); 
end

Asym = cell(1, nimage); 
for (i = 1 : nimage)
    Asym{i} = zeros(sx, sy); 
end

Asyma = cell(1, nimage); 
for (i = 1 : nimage)
    Asyma{i} = zeros(sx, sy); 
end

Asym0 = cell(1, nimage*(ns+1));
for (i = 1 : nimage*(ns+1))
    Asym0{i} = zeros(sx, sy);  
end


mex Cmp1.c; 
Cmp1(nimage, norient*ns+K, norient, K, allfilteredr, allfilteredi, allfiltered, Crr, Cri, Cir, Cii, half, sx, sy, allfilterr(1, :), allfilteri(1, :), allsymbol(1, :), syma, sym, Asym, Asym0, Asyma, Lrange, Orange, nsketch, epsilon, Upperbound, SHUTUP);

figure; 
col = 6; row = 1 + floor((nimage+1.)/2); 
for (i = 1:ns+1)
   subplot(row, col, i); imshow(-sym{i}, []);
end
subplot(row, col, ns+2); imshow(-syma, []); 
c = 6; 
for (i = 1 : nimage)
    c = c + 1;  subplot(row, col,  c); 
    imshow(I{i}, []);
    c = c + 1;  subplot(row, col,  c); 
    imshow(-Asyma{i}, []); 
    c = c + 1;  subplot(row, col,  c); 
    imshow(Asym{i}, []);  
end


figure; 
for (i = 1:ns+1)
   towrite =  -sym{i}; 
   towrite = (255 * (towrite-min(towrite(:)))/(max(towrite(:))-min(towrite(:))));
   outepsgif(towrite, ['ex10_1sym' num2str(i)]); 
end   
K = nimage; bx =10; by = 10; cc = 3;  
col = 5; row = floor(K/col)+1;
Iout = zeros(row*sx+(row-1)*(2*bx+cc), 2*col*sy+(col-1)*(2*by+cc))+255; 
towrite =  -syma; 
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{i}; 
            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{i}; 
            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, 'ex10_1'); 
