vidobj = videoinput('winvideo',2);

% Configure the object for manual trigger mode.
triggerconfig(vidobj, 'manual');

% Now that the device is configured for manual triggering, call START.
% This will cause the device to send data back to MATLAB, but will not log
% frames to memory at this point.
start(vidobj)

x = input('prompt');

Grille = getsnapshot(vidobj);



imagesc(Grille)

[xR,yR,xV,yV,xB,yB] = PositionsReperes(Grille);

Vecteur_Base_Horizontal=[(xB-xV)/7,(yB-yV)/7];
Vecteur_Base_Vertical=[(xR-xV)/6,(yR-yV)/6];


for i = 1 : 1
    Image1 = getsnapshot(vidobj);
    imagesc(Image1);
    
    Img1=rgb2gray(Image1);
    %Img1 = wiener2(I,[1 1]);
    %Image1Traite = seuil(I,127);
    imshow(Img1);
    
    x = input('prompt');
    %w = waitforbuttonpress;
    %pause(4)
    
    Image2 = getsnapshot(vidobj);
    imagesc(Image2);
    Img2=rgb2gray(Image2);
    %Img2 = wiener2(I,[1 1]);
    %Image2Traite = seuil(I,127);
    imshow(Img2);
    
    Difference = abs(Img1-Img2);
    imshow(Difference)
    Difference = seuil(Difference,80);
    imshow(Difference)
    
    [xC,yC] = PositionCoup(Difference);
    xBase=xC-xV;
    yBase=yC-yV;
    VecteurCoup=[xBase, yBase];
    ligne = round((dot(VecteurCoup,Vecteur_Base_Vertical)/(norm(Vecteur_Base_Vertical))^2)+0.5)
    colonne= round((dot(VecteurCoup,Vecteur_Base_Horizontal)/(norm(Vecteur_Base_Horizontal))^2)+0.5)
end

stop(vidobj)
delete(vidobj)

