function [ ] = func_groundTruthFromLabelPic( dataStorePicturePath, dataStoreLabelPath, outFile )
%
%   erstellt us einem Picture-Datastore und einem Label-Datastore
%   eine Groundtruth-Tabelle, wie sie z.B. in FasterRCNN.m
%   benoetigt wird
%
%   file von tas
%   adaptiert als func 2022/12/28 vh
%

labelDS = imageDatastore(dataStoreLabelPath, 'IncludeSubfolders', true);
pictureDS = imageDatastore(dataStorePicturePath, 'IncludeSubfolders', true);

labelCount = numel(labelDS.Files)
pictureCount = numel(pictureDS.Files)

if labelCount ~= pictureCount
    fprintf("!!!! Error: Die Anzahl der Bilder und Anzahl der LabelPicture sind ungleich -> Abbruch");
    return
end

fprintf("-----------------------------------------------------------\n");
fprintf("Picture-Verzeichnis: %s\n", dataStorePicturePath)
fprintf("Anzahl Images: %d\n", pictureCount)
fprintf("Label-Verzeichnis: %s\n", dataStoreLabelPath)
fprintf("Anzahl Images: %d\n", labelCount)
fprintf("BE PATIENT.... \n")
fprintf("-----------------------------------------------------------\n");

% table anlegen
sz = [pictureCount 3];
varTypes = ["cellstr","cell","logical"];
varNames = ["imageFilename","sign","valid"];
DataSet = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);

rng(0)
shuffledIndices = randperm(pictureCount);

% los gehts
for i = 1:pictureCount
    shuffeldIndex = shuffledIndices(i);
    [imPic imPic_INFO]= readimage(pictureDS, shuffeldIndex);
    [im_path imPic_name im_ext]=fileparts(imPic_INFO.Filename);
    
    [imLabel imLabel_INFO]= readimage(labelDS, shuffeldIndex);
    [im_path imLabel_name im_ext]=fileparts(imLabel_INFO.Filename);
    
    % fprintf("picture: %s label: %s\n", imPic_name, imLabel_name);
    box = [0,0,0,0]; %default if theres no labelimg

    v = true;

    if ~strcmp(imPic_name, imLabel_name)
        fprintf("!!!! Error: zum Picture gibt es kein entsprechendes LabelPicture -> Abbruch");
        imPic_name
        imLabel_name
    
    else 
    
        % LabelRegion aus Image ausschneiden
        bw = imLabel;
        s = regionprops(bw, 'BoundingBox');
        box = cat(1, s.BoundingBox); % structure to matrix
        box = round(box);
        
        % falls mehrere Marker vorhanden sind, ignorieren wir den Datensatz
        % das dürfte für das Training Region detection besser sein.
        if (height(box) > 1)
            v = false;
        end
        box = box(1,:);
        
        if (numel(box) ~= 4)
            fprintf("Boxkoordinaten nicht ok: %s %s\n", imPic_name, imLabel_name)
            box
            v = false
        end

    end
    a = num2cell(box, 2);

    

    %check for boxes which are somewhat wrong
    if v
        if (box(3) < 8.) ... 
            || (box(4) < 8.) ...
            || (abs(box(3) - box(4)) > 2) ...
            || (box(1) + box(3) > 1024) ...
            || (box(2) + box(4) > 768)
            fprintf("boxkoordinaten nicht i.o. %s  (%d  %d  %d  %d) \n", imLabel_name, box(1),box(2),box(3),box(4));
            v = false;
        end
    end
    
    DataSet(shuffeldIndex,:) = {imPic_INFO.Filename,a, v};
    
    % display one of the training images and box labels.
    if (shuffeldIndex == 4)
        annotatedImage = insertShape(imPic,'Rectangle',box);
        figure
        imshow(annotatedImage)
    end
    
end

%Die Daten sind teilweise nicht in Ordnung, am einfachsten ist natürlich
%die Einträge zu löschen, die nicht gut sind.
fprintf("Groundtruth hat %d eintraege vor der Bereinigung \n ", height(DataSet) )

toDelete =  DataSet.valid == false;
DataSet(toDelete,:) = [];

DataSet.valid=[];

fprintf("Groundtruth hat %d eintraege nach der Bereinigung \n", height(DataSet) )

save(outFile, 'DataSet' );