function imGTnew = CS6640_GTImageCleanup(imGT,imOrig,colormapOrig,classList)
% CS6640_GTImageCleanup - cleans up a preliminary ground truth image by 
%    subjective interpretation
% On input:
%     imGT(MxN array): preliminary ground truth indexed image
%     imOrig(MxN array): original indexed image
%     colormapOrig(MxN array): original image's indexed colormap
%     classList(Kx1 string): list of class names in same order as imGT
%        indices
% On output:
%     imGTnew(MxN array): new ground truth indexed image
% Call:
%     im2 = CS6640_GTImageCLeanup(im1,imOrig,cmapOrig,clist)
% Author:
%     Michael Malmberg
%     UU
%     Fall 2020
%       
% How to use this program:
% This program looks at the original image side by side with the mask for
% each index. Then the mask is placed over the original image to see what
% true features were segmented. Both the negative and positive are shown
% for optimal feature identification. 
%
% Figure 3 with the positive masked original shows up, and the user clicks
% and drags the locations of a rectangle encompassing the pixels that the
% user desires to reclassify. The other figures can be used to help
% identify the feature to which a desired selection should be reclassified.
% The user then inputs to which class the selected pixels should be
% assigned. 
% 
% This process repeats until the user selects a region outside the bounds
% of Figure 3, at which point the program cycles to the next class. The
% program ends after all classes have been cyclced through.
% 
% Display class list
[nR,nC] = size(imGT);
numClasses = length(classList);
%regionRows = round(nR/2); regionCols = round(nC/2); % initialize to some arbitrary value within the image
for ii = 1:numClasses
    figure(1); subplot(1, 2, 1); imshow(imOrig,colormapOrig); title('Original')
    mask = imGT==(ii-1);
    subplot(1, 2, 2); imagesc(mask); title(['Mask for class ',num2str(ii)]);
    figure(2);
    imshow(imOrig.*uint8(~mask),colormapOrig); title(['Masked original negative - class ',num2str(ii),' - ',char(classList(ii))]);
    figure(3);
    imshow(imOrig.*uint8(mask),colormapOrig); title(['Masked original - class ',num2str(ii),' - ',char(classList(ii))]);
    
    pause(0.1);
    region = getrect();
    pause(0.1);
    regionRows = round(region(2):(region(2)+region(4)));
    regionCols = round(region(1):(region(1)+region(3)));
    condition = ~any(regionRows<=0) && ~any(regionRows>nR) && ~any(regionCols<=0) && ~any(regionCols>nC);
    while condition
        maskRegion = zeros(size(imGT)); maskRegion(regionRows,regionCols) = 1;
        maskRegion = logical(maskRegion);
        
        % Display list of classes, including the "change nothing" class
        disp('Choose from class list below to reclassify mask pixels within region:');
        disp(['(',num2str(-1),') = Keep as is (remake rectangle selection)']);
        for mm = 1:numClasses
            disp(strcat("(",num2str(mm-1),") = ",string(classList(mm))));
        end
        classChoice = input(['Class choice (0-',num2str(numClasses-1),'): ']);
        
        if classChoice >=0
            imGT(mask&maskRegion) = classChoice;
        end
        mask = imGT==(ii-1);
        figure(1); subplot(1, 2, 2); imagesc(mask); title(['Mask for class ',num2str(ii)]);
        figure(2); imshow(imOrig.*uint8(~mask),colormapOrig); title(['Masked original negative - class ',num2str(ii),' - ',char(classList(ii))]);
        figure(3); imshow(imOrig.*uint8(mask),colormapOrig); title(['Masked original - class ',num2str(ii),' - ',char(classList(ii))]);
        
        pause(0.1);
        region = getrect();
        pause(0.1);
        regionRows = round(region(2):(region(2)+region(4)));
        regionCols = round(region(1):(region(1)+region(3)));
        condition = ~any(regionRows<=0) && ~any(regionRows>nR) && ~any(regionCols<=0) && ~any(regionCols>nC);
    end
end
imGTnew = imGT;
close(1); close(2); close(3);