function expandBox = roiExpand(tempImage,tempBox, addPixels)
%1. This function adds additional space for the bounding box generated from
%   the regionprop function. 
%2. Note the image start from (0, 0) on left upper corner.
%3. The cropBox was extracted from the downscaled image and then projected
%   back to the original image. There is a possibility that the position is
%   actually out of bounds on the original image.
%4. Input variables are original image to determine the maximum position, not
%   beyond the image.
%
% Created By: Kaichang CHENG, Jul 10,2019
%**************************************************************************
leftMin = 1;
lowMin = 1;
[upMax, rightMax] = size(tempImage);

if tempBox(1) > rightMax
    expandBox(1) = rightMax - 1;
else
    if tempBox(1) - (addPixels/2) > leftMin
        expandBox(1) = tempBox(1) - (addPixels/2);
    else
        expandBox(1) = leftMin;
    end
end

if tempBox(1) + tempBox(3) + (addPixels) < rightMax 
    expandBox(3) = tempBox(3) + (addPixels);
else
    expandBox(3) = rightMax - expandBox(1);
end

if tempBox(2)> upMax
        expandBox(2) = upMax - 1;
else
    if tempBox(2) - addPixels/2 > lowMin
        expandBox(2) = tempBox(2) - addPixels/2;
    else
        expandBox(2) = lowMin;
    end
end

if tempBox(2) + tempBox(4)+ addPixels < upMax
    expandBox(4) = tempBox(4) + addPixels;
else
    expandBox(4) = upMax - expandBox(2) ;
end   
end