% dip09_1.m                     gr_sep16    nov18
%    Basic binary morphological filtering
clear all; % close all;
magn = 700; % 100=orig.size

f = double(imread('.\img\lena.tif'))/256;
f = f(244:283,240:299);
[r,c] = size(f);
figure(1); imshow(f, 'InitialMagnification', magn);
thr = 70/256;  fb = f<thr; % fb = "logical" matrix
figure(2); imshow(fb,'InitialMagnification', magn);

return

%%  erode, dilate, open, close
se = strel('disk',1); % try 1,2

fb_er = imerode(fb,se);
figure(3); subplot(221); imshow(fb_er,'InitialMagnification', magn);

fb_di = imdilate(fb,se);
subplot(222); imshow(fb_di,'InitialMagnification', magn);

fb_op = imdilate(fb_er,se);  %   = open(fb)
subplot(223); imshow(fb_op,'InitialMagnification', magn);

fb_cl = imerode(fb_di,se);  %   = close(fb)
subplot(224); imshow(fb_cl,'InitialMagnification', magn);

%% repeated erosions

dum = fb;
for k = 1:8,
    fb_er = imerode(dum,se);
    figure(33); imshow(fb_er,'InitialMagnification', magn);
    dum = fb_er;
    pause(1);
end

%%  boundary
se4 = strel('square',3);   % 4-connected boundary
se8 = strel('disk',1);    % 8-connected boundary

fb_bndr4 = fb - imerode(fb,se4);
fb_bndr8 = fb - imerode(fb,se8);
% fb_bndr = xor(fb,imerode(fb,se)); % to get a "logical" output
figure(5); subplot(211); imshow(fb_bndr4,'InitialMagnification', magn);
subplot(212); imshow(fb_bndr8,'InitialMagnification', magn);

%%  hole filling
se = strel('disk',1);

fb_hf0 = zeros(size(fb));  fb_hf0(15,20) = 1; % seed of the hole 
figure(6); imshow(fb_hf0,'InitialMagnification', magn); pause()
while 1==1,
    fb_hf1 = imdilate(fb_hf0,se) .* (1 - fb_bndr4);   % in boundary
    imshow(fb_hf1,'InitialMagnification', magn); pause(.2)
    if (fb_hf1 - fb_hf0) == 0, break, end
    fb_hf0 = fb_hf1;
end
pause();
fb_hf = fb_hf1 + fb_bndr4;
imshow(fb_hf,'InitialMagnification', magn);

%%  extraction of connected components
se = strel('square',3);

fb_cc0 = zeros(size(fb));  fb_cc0(15,20) = 1;
figure(7); imshow(fb_cc0,'InitialMagnification', magn); pause(1)
while 1==1,
    fb_cc1 = imdilate(fb_cc0,se) .* fb; 
    imshow(fb_cc1,'InitialMagnification', magn); pause(.2)
    if (fb_cc1 - fb_cc0) == 0, break, end
    fb_cc0 = fb_cc1;
end
imshow(fb_cc1,'InitialMagnification', magn);

%% convex hull, thinning, skeletons...

fb_c_hull = bwconvhull(fb);   % since R2011a
figure(8); imshow(fb_c_hull,'InitialMagnification', magn);

% help bwmorph

fb_thin = bwmorph(fb,'thin',Inf);  % try N = 1 ... Inf
figure(9); imshow(fb_thin,'InitialMagnification', magn);

fb_sk = bwmorph(fb,'skel',Inf);
figure(10); imshow(fb_sk,'InitialMagnification', magn);

fb_thick = bwmorph(fb,'thick',Inf);  % try N = 1 ... Inf
figure(11); imshow(fb_thick,'InitialMagnification', magn);

%% geodesic dilation
marker = zeros(size(fb));  marker(15,20) = 1;
mask = fb;
fb_g_di = and(imdilate(marker,se),mask);
figure(12); imshow(fb_g_di,'InitialMagnification', magn);

  % iterate --> reconstruction by dilation
while 1==1,
    fb_g_recdil = and(imdilate(fb_g_di,se),mask);
    figure(12); imshow(fb_g_recdil,'InitialMagnification', magn);
    pause(.1);
    if (fb_g_recdil - fb_g_di) == 0, break, end
    fb_g_di = fb_g_recdil;
end

% imreconstruct(marker,mask);

