% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%	MPOV - uloha 6 - defektoskopie
%
%	- program otevre kameru a v nekonecne smycce while sbira snimky dokud
%     neni stisknuta klavesa Esc
%   - upravte program tak, aby na sejmutem snimku nalezl lahev, zobrazil
%     vektory b1 a b2 a zaroven osu lahve
%	
%	verze: 9-2015 / midas.uamt.feec.vutbr.cz
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function ex06_uloha()

%% uklid
clear all;
close all;
clc;

%% kamera
imaqreset;
pause(2);
kam = videoinput('winvideo',1);
kam.ReturnedColorSpace = 'grayscale';
triggerconfig(kam,'manual');
param = getselectedsource(kam);
param.ExposureMode = 'manual';
param.Exposure = -10;
param.Gain = 260;

%% parametry
global AppRunning;
AppRunning = 1;

%% rozhrani
ScreenSize = get(0,'ScreenSize');
figure('Position',[10 10 ScreenSize(3)-20 ScreenSize(4)-100],'Color',[0.3 0.3 0.3],'KeyPressFcn',@MyKeyPressed);
hold on;

%% smycka zpracovani obrazu
start(kam);
pause(1);
while (AppRunning == 1)
    %% porizeni snimku
    img = getsnapshot(kam);
    img = flipud(img(1:2:end,1:2:end));
    [SY, SX] = size(img);
    SY = uint16(SY);
    SX = uint16(SX);
    
       
    % ZACNETE PROGRAMOVAT ZDE >>>
    % - detekce okraju lahve + vypocet osy lahve
   
    
    %% vykresleni vysledku
    imshow(img), title(['Osa lahve [',num2str(SX),'x',num2str(SY),'], Esc = Exit'],'Color',[1 1 0],'FontSize',16);
    
    % ZACNETE PROGRAMOVAT ZDE >>>    
    % - vykresleni b1, b2 a osy lahve
    
    %% priprava dalsi iterace
    pause(0.1);
end;
stop(kam);
close all;


function MyKeyPressed(src, event) %#ok<INUSL>
%% stisk klavesy
global AppRunning;
switch event.Key
    case 'escape'
        AppRunning = 0;
end;
