function varargout = final(varargin)

global blueSquareSigns;
blueSquareSigns = loadimage(dir('blueSquareSigns\*.png'));

global blueCirkleSigns;
blueCirkleSigns = loadimage(dir('blueCirkleSigns\*.png'));

global squareScore;
squareScore = zeros(1, length(blueSquareSigns));

global circleScore;
circleScore = zeros(1, length(blueCirkleSigns));


% FINAL MATLAB code for final.fig
%      FINAL, by itself, creates a new FINAL or raises the existing
%      singleton*.
%
%      H = FINAL returns the handle to a new FINAL or the handle to
%      the existing singleton*.
%
%      FINAL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FINAL.M with the given input arguments.
%
%      FINAL('Property','Value',...) creates a new FINAL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before final_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to final_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help final

% Last Modified by GUIDE v2.5 02-May-2017 22:15:45

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @final_OpeningFcn, ...
    'gui_OutputFcn',  @final_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before final is made visible.
function final_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to final (see VARARGIN)

% Choose default command line output for final
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes final wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = final_OutputFcn(~, ~, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbuttonOpen.
function pushbuttonOpen_Callback(hObject, ~, handles)

[filename, pathname] = uigetfile({'*.avi', 'All Files'});

not_loaded = true;
while not_loaded
    try
        handles.readerobj = VideoReader([pathname filename]);
        not_loaded = false;
    catch
        error('Oops..');
    end
end

handles.numFrames = get(handles.readerobj, 'numberOfFrames');

global actualFrame;
actualFrame = 1;

img = handles.readerobj.read(actualFrame);
imshow(img, 'Parent', handles.axes1);
guidata(hObject, handles);



% hObject    handle to pushbuttonOpen (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbuttonPlay.
function pushbuttonPlay_Callback(~, ~, handles)

global play;
global actualFrame;
global squareScore;
global circleScore;
global blueSquareSigns;
global blueCirkleSigns;
currentFrame = 0;
previousFrame = 0;

play = true;
axes(handles.axes1);
fps=30;

for i=actualFrame : handles.numFrames
    if play
        
        actualFrame = i;
        image = handles.readerobj.read(i);
        imshow(image);
        
        binaryImage=vidihsv(image);
        [blob, shapeID] = getimages(binaryImage, image);
        x = usePixelCount(shapeID, blob);       
        y = useSift(shapeID, blob);
        
        if x == false
            prepreviousFrame = previousFrame;
            previousFrame = currentFrame;
            currentFrame = i;
            if currentFrame-1 == previousFrame && previousFrame-1 == prepreviousFrame
            squareScore = zeros(1, length(blueSquareSigns));
            circleScore = zeros(1, length(blueCirkleSigns));
            imshow(x, 'Parent', handles.axes10);
            end
        end
        
        [squareScore, circleScore] = strongestSignFinal(x, handles, squareScore, circleScore, blob);
        [squareScore, circleScore] = strongestSignFinal(y, handles, squareScore, circleScore, blob);
        pause(1/fps);
    end
end

play = false;

% hObject    handle to pushbuttonPlay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbuttonPause.
function pushbuttonPause_Callback(~, ~, ~)

global play;
play = false;

% hObject    handle to pushbuttonPause (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbuttonExit.
function pushbuttonExit_Callback(~, ~, ~)

global play;
play = false;
close all
close();

% hObject    handle to pushbuttonExit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
