function [handPosX, handPosY, pitch] = gloveDetect(movie)
% Returns the glove position and angle
% Inputs:
% movie: A movie object containing 8 frames
% Outputs:
% handPosX, HandposY: the hand position coordinates x and y respectivly
% pitch: The pitch of the hand


%Get the reference image and convert it to grayscale
%medilt 2 is a moving average function. Blurs the image which allows the
%"jitter" to be more easily removed.
refImg = medfilt2(rgb2gray(frame2im(movie(1))),[5 5]);

%Get the image from the movie object
img1 = medfilt2(rgb2gray(frame2im(movie(2))), [5 5]);

%Get the image from the movie object
img2 = medfilt2(rgb2gray(frame2im(movie(3))), [5 5]);

%Get the image from the movie object
img3 = medfilt2(rgb2gray(frame2im(movie(4))), [5 5]);

%Get the difference images between the reference frame and the led image
[id1 id2 id3] = imgDiff(refImg,img1,img2,img3);


%Detect the led positions from teh top row
res(1).pos = ledDetect(id1);
%Detect the led position
res(2).pos = ledDetect(id2);
%Detect the led position
res(3).pos = ledDetect(id3);


%Get the reference image and convert it to grayscale
%medilt 2 is a moving average function. Blurs the image which allows the
%"jitter" to be more easily removed.
refImg = medfilt2(rgb2gray(frame2im(movie(5))),[5 5]);

%Get the image from the movie object
img4 = medfilt2(rgb2gray(frame2im(movie(6))), [5 5]);

%Get the image from the movie object
img5 = medfilt2(rgb2gray(frame2im(movie(7))), [5 5]);

%Get the image from the movie object
img6 = medfilt2(rgb2gray(frame2im(movie(8))), [5 5]);

%Get the difference images between the reference frame and the led image
[id4 id5 id6] = imgDiff(refImg,img4,img5,img6);


%Detect the led position
res(4).pos = ledDetect(id4);

%Detect the led position
res(5).pos = ledDetect(id5);

%Detect the led position
res(6).pos = ledDetect(id6);

%Apply the proximity filter to all the potential LED positions to remove
%anamolies
led_points = proximityFilter(res);

[height width] = size(refImg);

%Extract the points on the top
points = [];
if( ~isempty(led_points(1).pos)),
    points = [led_points(1).pos(1) led_points(1).pos(2)];
end
if( ~isempty(led_points(2).pos)),

    points = [points; led_points(2).pos(1) led_points(2).pos(2)];
end
if( ~isempty(led_points(3).pos)),
    points = [points; led_points(3).pos(1) led_points(3).pos(2)];
end


%Get the number of LED positions that were detected
[n_points tmp] = size(points);

%Intialize the variables to -1 so that modifications can be picked out
xAvgTop =-1;
yAvgTop = -1;
xDifTop = -1;
yDifTop = -1;
angleTop = 360;

if( n_points > 1)

    %Enough points obtained so calculate glove postion and angle using the
    %first three points which correspond to the top row of leds (back of
    %the hand)
    [s tmp] = size(points);
    xAvgTop = sum(points(1:1:s))/s;
    yAvgTop = sum(points(s+1:1:2*s))/s;

    xDifTop = points(s) - points(1);
    yDifTop = (points(2*s) - points(s+1)) * -1;

    %Calculate the angle
    angleTop = atan(yDifTop/xDifTop * width/height) * 180 / pi;
    if yDifTop >= 0

        if xDifTop < 0

            angleTop = angleTop + 180;
        end
    else
        if xDifTop < 0
            angleTop = angleTop - 180;
        end
    end
    angleTop
end


%Extract the points on the bottom
points = [];
if( ~isempty(led_points(4).pos)),
    points = [led_points(4).pos(1) led_points(4).pos(2)];
end
if( ~isempty(led_points(5).pos)),
    points = [points; led_points(5).pos(1) led_points(5).pos(2)];
end
if( ~isempty(led_points(6).pos)),
    points = [points; led_points(6).pos(1) led_points(6).pos(2)];
end
[n_points tmp] = size(points);

%Intialize the variables to -1 so that modifications can be picked out

xAvgBottom = -1;
yAvgBottom = -1;
xDifBottom = -1;
yDifBottom = -1;
angleBottom = 360;
if( n_points <= 1)
    %Single point or failed to detect the LED, so can't deduce angle and
    %position is likely to be undependable
else
    %Enough points obtained so calculate glove postion and angle using the
    %last three points which corrspond to the bottom row of leds (palm side)
    [s tmp] = size(points);
    xAvgBottom = sum(points(1:1:s))/s;
    yAvgBottom = sum(points(s+1:1:2*s))/s;

    xDifBottom = points(s) - points(1);
    yDifBottom = (points(2*s) - points(s+1)) * -1;

    %Calculate the angle
    angleBottom = atan(yDifBottom/xDifBottom * width/height);
    
    if yDifBottom >= 0

        if xDifBottom < 0
            angleBottom = angleBottom + 180;
        end
    else
        if xDifBottom < 0
            angleBottom = angleBottom - 180;
        end
    end
    angleBottom
end


figure;
imshow(refImg);

%Combine the two sets of values
if(( xAvgBottom > 0) & ( xAvgTop > 0))
    handPosX = round((xAvgBottom + xAvgTop )/2);
elseif ( xAvgBottom > 0)
    handPosX = xAvgBottom;
elseif ( xAvgTop > 0)
    handPosX = xAvgTop;
else
    handPosX = -1;
end

if(( yAvgBottom > 0) & ( yAvgTop > 0))
    handPosY = round((yAvgBottom + yAvgTop )/2);
elseif ( yAvgBottom > 0)
    handPosY = yAvgBottom;
elseif ( yAvgTop > 0)
    handPosY = yAvgTop;
else
    handPosY = -1;
end

if( (angleTop ~= 360) & (angleBottom ~= 360)),
    pitch = round( (angleTop + angleBottom)/2);
elseif (angleTop ~= 360),
    pitch  = angleTop;
elseif (angleBottom ~= 360),
    pitch = angleBottom;
else
    pitch = 360;
end

%add display lines to image
if (handPosX ~= -1) & (handPosY ~= -1) & (pitch ~= 360)
    l = line([(handPosX - 30 * cos(pitch * pi/180));(handPosX+ 30*cos(pitch * pi/180))],[(handPosY + 40 * sin(pitch * pi/180)) ;(handPosY - 30 * sin(pitch * pi/180))]);
    set(l,'color',[1 0 0],'lineWidth',[2]); %red and increase the width
    if pitch > 0
        l2 = line([handPosX;(handPosX + 30*cos((pitch - 90) * pi/180))],[handPosY;(handPosY + 30 * -sin((pitch - 90) * pi/180))]);
    else
       l2 = line([handPosX;(handPosX + 30*cos((360 + pitch -90) * pi/180))],[handPosY;(handPosY + 30 * -sin((360 + pitch - 90) * pi/180))]);    
    end
    
    set(l2,'color',[0 1 0],'lineWidth',[2]);%green and increase the width
end



fprintf(1,'The hand is at position: %i,%i\n',handPosX,handPosY);
fprintf(1,'The hand is at an angle of: %i degrees\n',pitch);


