%% Quanli Wang, Mike West
% quanli@stat.duke.edu
% MCF Size Transform Example for Concealed Objects detection
addpath dist

%% read image and extract subimages
IMG = imread('images\concealedobject.bmp');
IMG = IMG(:,:,1);
data{3} = IMG(950:950+440-1,4:504-1);
data{2} = IMG(477:477+440-1,4:504-1);
data{1} = IMG(4:4+440-1,4:504-1);

% total number of images to process
NumberOfImages = 3;

% Parameters used background detection
[m n] = size(data{1});
minsize_background = floor(m * n * 0.1);
maxsize_background = floor(m * n * 0.9); 
disk = 2;

% tuning parameter, not used
gap = 0;

%% MCF size transform
for i=1:NumberOfImages 
    i
    % show original image
    figure(1),imshow(data{i},[]), title('original input image');
    
    %MCF size transform
    [x1 f1] = ImmersionSimulation1(data{i}, @ISSO_SizeShapeFilter, @ISSO_agr,[minsize_background maxsize_background disk], []);
    [x2 f2] = ImmersionSimulation1(imcomplement(data{i}), @ISSO_SizeShapeFilter, @ISSO_agr,[minsize_background maxsize_background disk 1], []);
    
    % show resonstructed image from both channels
    showRGB(f1,f2,0,3,2);
    
    % background detection
    t1 = f1+gap>=f2; b = data{i};b(t1>0) = 0;
    figure(3),imshow(b,[]);title('image with bechground remove');
    
    %parameters used to object detection
    minsize_object = floor(length(find(b>0))*0.01);
    maxsize_object = floor(length(find(b>0))*0.1);
    [x3 f3] = ImmersionSimulation1(b, @ISSO_SizeShapeFilter, @ISSO_agr,[minsize_object maxsize_object disk], []);
    figure(4),imshow(imclearborder(f3),[]); title('detected concealed objects');
    
end