function [bigImages,bigMasks] = createBigImageAndMaskArrays(imageDir,maskDir)
% Create an array of bigimage objects and corresponding bigimage masks from
% the input folders, imageFolder and maskFolder respectively for the
% specified image indices in imgIdx. The imgIdx corresponds to the index of
% the image file in the folder.
%
% The Camelyon16 normal training file "normal_144.tif" cannot be read by a
% |bigimage| object, so do not create a bigimage from this file.

% Copyright 2019 The MathWorks, Inc.

imds = imageDatastore(imageDir,'FileExtensions',{'.tif'});

bigArrayIdx = 1;
for imgIdx = 1:length(imds.Files)
    imageFullFileName = imds.Files{imgIdx};
    [~,name] = fileparts(imageFullFileName);
    
    if strcmp(name,"normal_144")
        continue
    end
    
    maskImageDirName = strcat(name,'_mask'); 
    
    bigImages(bigArrayIdx) = bigimage(imageFullFileName);
    ref = setSpatialReferencingForCamelyon16(imageFullFileName);
    numSpatialRef = numel(ref);
    bigImages(bigArrayIdx).SpatialReferencing(1:numSpatialRef) = ref;    
    bigMasks(bigArrayIdx) = bigimage(fullfile(maskDir,maskImageDirName));   
    
    bigArrayIdx = bigArrayIdx+1;
end