% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <https://www.gnu.org/licenses/>

% Author: Ilya Belevich, University of Helsinki (ilya.belevich @ helsinki.fi)
% part of Microscopy Image Browser, http:\\mib.helsinki.fi 
% Date: 08.05.2024

function out = matReadInstanceLabels(filename, getImageOptions)
% function out = matReadInstanceLabels(filename, getImageOptions)
% read preprocessed instance segmentation labels and return them as a cell
% array with 3 element.
%
% Parameters:
% filename: full path to mat-file with preprocessed instance labels
% containing 3 variables - instanceBoxes, instanceNames, instanceMasks,
% instanceBoxes, matrix  [N×4 double] containing bounding box coordinates of objects, where N is a number of objects on the image
% instanceName, array [N×1 categorical] containing names of objects,
% currently the same name should be used for all objects, can be any string
% converted to categorical.
% instanceMasks, matrix [720×1280×N logical] binary masks where each slice
% represents individual object that should match the corresponding entry in
% instanceBoxes and instanceNames
%
% Return values:
% out:  cell array where the 1st element is instanceBoxes, 2nd -
% instanceName, 3rd - instanceMasks


% read 3 variables that were generated by preprocessing the instance labels
% model: instanceBoxes, instanceNames, instanceMasks
data = load(filename);

% generate path to an image that corresponds to the instance mask
[projectDir, parentDir] = fileparts(fileparts(filename));

if parentDir(1) == 'T'  % image coming for training
    imageFilename = fullfile(projectDir, 'TrainImages', data.imageFilename);
else                    % image coming for validation
    imageFilename = fullfile(projectDir, 'ValidationImages', data.imageFilename);
end
out{1} = mibDeepStoreLoadImages(imageFilename, getImageOptions);
if ndims(out{1}) > 3
    errordlg(sprintf(['!!! Error !!!\n\n' ...
                    'The instance segmentation requires 2D images:\n' ...
                    'width x height x colors,\n' ...
                    'where colors can be 1 or 3 channel images']),...
            'Preprocessing instances', 'modal');
    return;
end
% convert Grayscale to RGB
if size(out{1}, 3) == 1
    out{1} = repmat(out{1}, [1, 1, 3]);
end

out{2} = data.instanceBoxes;       % Nx4 double bounding boxes
%out{3} = repmat(categorical("Object"), [numObjects 1]);       % Nx1 categorical object labels
out{3} = data.instanceNames;
out{4} = data.instanceMasks;        % HxWxN logical mask arrays

