%% Quanli Wang, Mike West
%quanli@stat.duke.edu
% MCF example 3: budding yeast segmentation

%% 
% load and show the input image
addpath dist

a = imread('images\050413-02-p-119.tif');
figure(1),imshow(a)
[m n ] = size(a);

%apply MCF Size Transform to enhance the input image
[X2 A2] = ImmersionSimulation1(a, @ISSO_SizeShapeFilter, @ISSO_agr,[1 floor(m*n/4)], []);
figure(2),imshow(A2,[])

%global thresholding
thres = 20;
bw = imhmax(A2,thres)>0;
figure(3), imshow(bw,[])

%% binary segmentation
% binary segmentaion using MCF Size Transform with default minsize = 10
minsize = 10;
r = BinarySeg(bw,minsize);
%remove objects that are small than 50 pixels
r = bwareaopen(r,50);
figure(4),imshow(r); title('Final segmentation result') 

%% random colored objects
[L num] = bwlabel(r); 
rgb = label2rgb(L,'jet','k', 'shuffle');
figure(8),imshow(rgb)
