% ExpandCluster

function result = expandcluster(point)
global clusterId label im2 eps minpts hr vr cr

result = 0;
% Find all the pixels that are not only similar in color space, but also
% close to each other in spatial space
seed = regionQuery(point);
seedsize = size(seed, 1);

% To see if this point is a core point
if (seedsize < minpts)
    label(point(1), point(2)) = 0;
    result = 0;
    return
else
    result = 1;
    % Create a new cluster
    label(point(1), point(2)) = clusterId;
    % Change the label of each pixel in seed
    changeAllID(seed, label(point(1), point(2)));
    % Remove the point from the seed set
    seed = seed(2:seedsize, :); 
    while (size(seed, 1)~=0)
        currentP = seed(1,:);
        % Region growing
        newseed = regionQuery(currentP);
        newseedsize = size(newseed, 1);
        
        if (newseedsize >= minpts)
            for i = 1:newseedsize
                resultP_x = newseed(i,1);
                resultP_y = newseed(i,2);
                if (label(resultP_x, resultP_y) == -1)
                   seed = [seed; [resultP_x, resultP_y]];
                end
                label(resultP_x, resultP_y) = label(currentP(1), currentP(2));
            end
        end
        seedsize = size(seed, 1);
        seed = seed(2:seedsize, :);
    end    
end
