function detImg = trackFeatures(img,eyeDet,leyeDet,bDet,xStart,xEnd,yStart,yEnd,step_size, halfPoint)
%Function to extract the postion of the pupils, eye brows, and left corner of the eye
%Inputs:
%img 
%image that would be searched for the features
%
%eyeDet,leyeDet,bDet
%The neural network for the pupils, left eye corner and eyebrow respectivly
%
%xStart,xEnd,yStart,yEnd
%The coordiates of the face in the image
%
%step_size 
%Defines how far apart the grid points are. 
%For step size of 1 it takes several minutes on a large image, a value between 
%5 and 10 is suitable
%
%halfPoint
%The half way point of the face on the x axis
%
%Outputs:
%detImg
%resulting image with the dectection points

%intialise variables
testImg = rgb2gray(img);
[height width t] = size(testImg);
detImg = img;
LeyePos = [];
ReyePos = [];
LlePos = [];
RlePos = [];
RbrowPos = [];
LbrowPos = [];

%start timer
tic;

for i = xStart:step_size:xEnd, 
    for j = yStart:step_size:yEnd,
	%for each grid point apply the sampling function
        pimg = lpImgTrans(testImg,i,j,16,1);
        nd = transpose(double(pimg(1:256)));

	%simulate neural inputs with the sampled data
        [Y1,Pf,Af,E,perf] = sim(eyeDet,nd);
        [Y2,Pf,Af,E,perf] = sim(bDet,nd);
        [Y3,Pf,Af,E,perf] = sim(leyeDet,nd);
       
        %Test to see if detection has occured
        if Y1 > 0.5,
            detImg(j,i,:) = [255 0 0];
            if i < halfPoint,
                LeyePos = [LeyePos; i j];
            else
                ReyePos = [ReyePos; i j];
            end
        elseif Y2 > 0.9,
            detImg(j,i,:) = [0 255 0];
            if i > halfPoint,
                RbrowPos = [RbrowPos; i j];
            else
                LbrowPos = [LbrowPos; i j];
            end
        elseif Y3 > 0.9,
            detImg(j,i,:) = [0 0 255];
            if i > halfPoint,
                RlePos = [RlePos; i j];
            else
                LlePos = [LlePos; i j];
            end
        else
            %detImg(j,i,:) = [0 0 0];
        end

    end
end
toc;

%Set the postion to the median value, gives much better results than the average
LeyePos = round(median(LeyePos,1));
ReyePos = round(median(ReyePos,1));
LlePos = round(median(LlePos,1));
RlePos = round(median(RlePos,1));
RbrowPos = round(median(RbrowPos,1));
LbrowPos = round(median(LbrowPos,1));

%Display results
figure;
imshow(detImg);

if (~isempty(RbrowPos)) 
    l = line([RbrowPos(1) - 20;RbrowPos(1)+20],[RbrowPos(2);RbrowPos(2)]);
    set(l,'Color',[0 1 0]);
end

if ( ~isempty(LbrowPos))
    l = line([LbrowPos(1) - 20;LbrowPos(1)+20],[LbrowPos(2);LbrowPos(2)]);
    set(l,'Color',[0 1 0]);
  
end
if( ~isempty(LeyePos))
    l2 = line([(LeyePos(1)-8);(LeyePos(1)+8)],[LeyePos(2);LeyePos(2)]);
    set(l2,'Color',[1 0 0]);
    l2 = line([LeyePos(1);LeyePos(1)],[(LeyePos(2)-8);(LeyePos(2)+8)]);
    set(l2,'Color',[1 0 0]);
end

if (~isempty(ReyePos))
    l2 = line([(ReyePos(1)-8);(ReyePos(1)+8)],[ReyePos(2);ReyePos(2)]);
    set(l2,'Color',[1 0 0]);
    l2 = line([ReyePos(1);ReyePos(1)],[(ReyePos(2)-8);(ReyePos(2)+8)]);
    set(l2,'Color',[1 0 0]);
    


end
if ( ~isempty(LlePos) )
    
    l3 = line([(LlePos(1)-4);(LlePos(1)+4)],[LlePos(2);LlePos(2)]);
    set(l3,'Color',[0 0 1]);
    l3 = line([LlePos(1);LlePos(1)],[(LlePos(2)-8);(LlePos(2)+8)]);
    set(l3,'Color',[0 0 1]);
end
if (~isempty(RlePos) )
    l3 = line([(RlePos(1)-4);(RlePos(1)+4)],[RlePos(2);RlePos(2)]);
    set(l3,'Color',[0 0 1]);
    l3 = line([RlePos(1);RlePos(1)],[(RlePos(2)-8);(RlePos(2)+8)]);
    set(l3,'Color',[0 0 1]);
    

end




