function resArray = ledDetect(img)
%Usage:
%   resArray = ledDetect(img)
%The aim of this function is to detect potential LED locations in the
%difference image given as the input. This is done by using an intensity 
%threshold to pick out the position. A grouping function is also used to 
%group similar "difference" regions together and to remove too big to be 
%the LEDs. The returned array contains a list of
%the possible led positions.
% Inputs
% img: Image in which the led is to be detected
%
% Outputs
% resArray: An array containing the possible led positions, each position is
%           on a new row. Rows have the format:
%      [Xposition Yposition position_intensity pixel_count proximity_count]
%           Note that the proximity count is only intialised to zero in
%           this function
%

[height width] = size(img); %Get the size of the reference image

%Starting threshold for intensity, anything below this point is not picked up
thresh = 30;

%Intialise the results array
resArray = [];

%Intialise xLoc and yLoc to -1 incase they no points exceed the threshold
xLoc = -1;
yLoc = -1;

%Go though all the pixels, moving horizontally first (left to right) and
%then verticaly (top to bottom)
for i = 1:height,
    for j = 1:width,

        
        %Test if the pixel intensity is larger than the threshold and lower
        %than the threshhold ceiling
        if (img(i,j) > 80)
            thresh = 80;
        elseif (img(i,j) > thresh) & (thresh <= 80),
            %Increase threshold to match current intensity
            thresh = double(img(i,j));
        end

        %Test out the data in blocks of four pixels to detect area's with
        %three or more high valued (i.e. nearly white) pixels.

        if (i>1) & (j>1)

            pxlCount = 0;

            if( img(i,j) >= thresh)
                pxlCount = pxlCount +1;
            end
            if (img(i-1,j) >= (thresh - 5))
                pxlCount = pxlCount +1;
            end

            if(img(i,j-1) >= (thresh -5))
                pxlCount = pxlCount +1;
            end

            if(img(i-1, j - 1) >= (thresh - 5))
                pxlCount = pxlCount +1;
            end

            %Try to detect a spot larger than 2 pixels, since such a spot
            %constitutes a possible led postion

            if pxlCount >= 3
                %Possible led position, so add it to the results
                xLoc = j;
                yLoc = i;
                resArray = [resArray; xLoc yLoc thresh pxlCount 0];
            end
        end
    end
end

%Number of detection points is limited to 6, so if there are more than 6
%points, they need to be cut down. Firstly similar points are combined,
%then the top six (by threshold then pixel count) remaining points are
%returned.
%Group similar points together
resArray = groupFilter(resArray);

%Get the number of detection points
[numRes temp] = size(resArray);

%If there are more than 6 points detected, only use the top 6
if (numRes > 6)
    %Sort results based on decreasing threshold followed by pixel count
    resArray = sortrows(resArray,[-3 -4]);
    %Only keep the top 6 results
    resArray = resArray(1:6,:);
end
