function  resArray = groupFilter(points)
%Usage:
%resArray = groupFilter(points)
%This function aims to combine similar points together so that the largest
%range of points can be used later on.
%Inputs
%points: A list of possible led locations in the following format
%      [Xposition Yposition position_intensity pixel_count proximity_count]
%Outputs
% resArray: A list that's in the same format as the points list
% above which has had detection points within 4 pixels (locality)combined 
% into one.




distThresh  = 20^2; %Threshold for considering two points similar, squared so 
                  %that there's no need to square root when comparing

%Set intialise the output array to be the same as the input
resArray = points;

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

%If there's more than one point then combination may be applicable
if numRes > 1
    i = 1;
    %Loop through the the results to combining them as necessary
    while (i <= numRes),
        %Get the curret result to which the other results will be compared
        row = resArray(i,:);

        %Set xLoc1,yLoc1,thresh1 & pxlCount1, not needed but imporves readability
        xLoc1 = row(1);
        yLoc1 = row(2);
        thresh1 =row(3);
        pxlCount1 = row(4); 
        j = i+1;
        %Loop through the remaining results testing for locality
        while(j <= numRes),
            %Set xLoc2,yLoc2,thresh2 & pxlCount2, not needed but imporves
            %readability
            row = resArray(j,:);
            xLoc2 = row(1); 
            yLoc2 = row(2);
            thresh2 = row(3);
            pxlCount2 = row(4); 

            %Get the square of the differences
            distSqrd = (xLoc2-xLoc1)^2 + (yLoc2-yLoc1)^2;
            if(distSqrd < distThresh)
                %points are near each other so combine and add at the back
                %of resArray
                resArray = [resArray; round((xLoc1 + xLoc2)/2) round((yLoc1 + yLoc2)/2) max(thresh1,thresh2) (pxlCount2 + pxlCount1) 0];
                %points combined so they need to be removed from resArray
                %The current row will still combine with the rest of the
                %results
                resArray(i,:) = [];
                resArray(j,:) = [];
                %Since the current row was removed, the next row is at row
                %j
            else
                %No rows removed, so j needs to be incremented so that the
                %other rows are compared.
                j = j+1;
            end
            %Refresh the number of remaining results
            [numRes temp] = size(resArray);
        end
        %Increment i to test out the next set of results 
        i = i+1;
    end
end

[numRes temp] = size(resArray);
i=1;
while (i <= numRes),
    row = resArray(i,:);

    %Test to see the number of detected pixels, if there are too many
    %then the object is too big to be a led
    xLoc = row(1);
    yLoc = row(2);
    thresh =row(3);
    pxlCount = row(4); 
    if(pxlCount > 20000) %too big to be a led
        resArray(i,:) = [];
    end
    [numRes temp] = size(resArray);
    i = i+1;
end 

