function maskImageAdjusted = adjustBigImageSize(maskImage,inputImage,resolutionLevel)
% Crop the mask image so that both mask and original image are aligned
% correctly at different resolution levels

% Copyright 2019 The MathWorks, Inc.

if resolutionLevel <= 4
    error('Cannot load the Set resolutionLevel to a value 5 and above');
else
    im = getFullLevel(maskImage,1);
end

numRowsToRemove = maskImage.LevelSizes(1,1) - inputImage.LevelSizes(1,1)/2^(resolutionLevel-1);
numColsToRemove = maskImage.LevelSizes(1,2) - inputImage.LevelSizes(1,2)/2^(resolutionLevel-1);

if numRowsToRemove < 0 || numColsToRemove < 0
    error('Expected to find rows or columns to remove');
end

imT = im(1:end-numRowsToRemove,1:end-numColsToRemove-1);
maskImageAdjusted = bigimage(imT);
maskImageAdjusted.SpatialReferencing(1).XWorldLimits = inputImage.SpatialReferencing(1).XWorldLimits;
maskImageAdjusted.SpatialReferencing(1).YWorldLimits = inputImage.SpatialReferencing(1).YWorldLimits;
