%{
script file to lab4, Cellular Wave Computers
In the sample images (inside the folder 'gyak4_images') there are three 
different kind of objects:
- small, black square;
- middle-gray rectangle (rectangle: the ratio between the side-lengths is
at least 1.5);
- not completely filled objects.

The objective is to process the sample images and idenfify the objects'
classes with CNN-template sequences. (One image can contain different kind of
objects at the same time.)

>> Fekete kicsi téglalap
THRES > mCC.I = -0.9
EROSION x10
RECALL > input: thres_out, init_state: erosion_out
LOGDIF > input: recall_out, init_state: thres_out
SHADOWD > countrow, countcol
SHADOWL

>> Szürke téglalap
THRES I=-0.5 >> ritka
THRES I=+0.5 >> sûrû
LOGDIF > init_state: sûrû, input: ritka
SHADOWL
SHADOWD
%}

close all;
clear all;

cnn_setenv;

mCNN.TemGroup='temlib_plus';
ImgSize=256;
ImgNum=5;
% this will be your estimation vector, later this should be modified
class=[000; 000; 000; 000; 000];
% for the coding of the objects' classes, please see the LoadGT function
GT=LoadGT();

% we will process the different images sequentially, iterating on the input
% images, one after the other:
for idx = 1:ImgNum
    %img=lbmp2cnn(strcat('gyak4_images/img', num2str(idx), '.bmp'));
    load(strcat('gyak4_images/img', num2str(idx), '.mat'));
    %loadCNN(strcat('gyak4_images/img', num2str(i), '.bmp'))
    input=img;
    
    
    
    
    % you can place here your template-sequence extracting small squares  -
    % GT-coding: 1
    
    
    
    
    % you can place here your template-sequence extracting middle-gray
    % rectangles - GT-coding: 10
    
    
    
    
    % you can place here your template-sequence extracting not completely
    % filled objects - GT-coding: 100
    % one solution is provided, to help you to create the two other
    % detectors:
    %   necessary steps to find objects with holes:
    %     1) threshold: grayscale --> bw
    %     2) holefiller: fills the holes on the bw image, inside objects
    %     3) logdif: if the 'result_img = holefilled-thresholded' contains
    %     any black pixels, it means there was at least one object with
    %     hole on the initial image
    
    %THRESHOLD
    mCNN.INPUT1 = 1 .* zeros(size(input));
    mCNN.STATE = 1 .* input;
    mCNN.Boundary = 2;
    mCNN.TimeStep = 1;
    mCNN.IterNum = 10;
    loadtem('THRES');
    mCNN.I=1;
    runtem;
    thresholded = 1 .* mCNN.OUTPUT;
    %HOLEFILLER
    mCNN.INPUT1 = 1 .* thresholded;
    mCNN.STATE = ones(ImgSize);  %black image  
    mCNN.Boundary = -1;
    mCNN.TimeStep = 1;
    mCNN.IterNum = floor(sqrt(sum(size(input).^2)));
    loadtem('HOLE');
    runtem;
    holefilled = 1 .* mCNN.OUTPUT;
    %LOGDIF: holefilled - thresholded
    mCNN.INPUT1 = 1 .* thresholded;
    mCNN.STATE = 1 .* holefilled;
    mCNN.Boundary = 2;
    mCNN.TimeStep = 1;
    mCNN.IterNum = 10;
    loadtem('LOGDIF');
    runtem;
    if (CountBlackImg(mCNN.OUTPUT)>10)
        class(idx)=class(idx)+100;
    end
    
    
    
    
    
    % at the end of every template sequence, you can make your decision on
    % the basis of the your result image with the following functions:
    % CountBlackImg(result_img); - this will count the total number of
    %                              black pixels on the image
    % CountBlackRow(result_img); - this will count the black pixels in the
    %                              bottom row of the image
    % CountBlackCol(result_img); - this will count the black pixels in the
    %                              most-left column of the image
    
    % if you have identified one class, do not forget to update the
    % appropriate digit of the appropriate coordinate of the class array
    
    fprintf('Image number: %d, estimated classes: %03.0f, ground truth classes: %03.0f\n', ...
        idx, class(idx), GT(idx));
end



