function [] = demoSkeletonization(fileName);

% clear all;
close all;

img = imread(fileName);
fig1 = figure;
imshow(img, [0,60]);

se = strel('disk', 2);
imgClean = imclose(imopen(img,se),se);
imshow(imgClean, [0,60]);
bw0 = ~(im2bw(imgClean,.12));
imshow(bw0);

LO1 = [ 0  0  0;  0  1  0;  1  1  1]
LO2 = [ 0  0  0;  1  1  0;  0  1  0]
LO3 = [ 1  0  0;  1  1  0;  1  0  0]
LO4 = [ 0  1  0;  1  1  0;  0  0  0]
LO5 = [ 1  1  1;  0  1  0;  0  0  0]
LO6 = [ 0  1  0;  0  1  1;  0  0  0]
LO7 = [ 0  0  1;  0  1  1;  0  0  1]
LO8 = [ 0  0  0;  0  1  1;  0  1  0]


LB1 = [ 1  1  1;  0  0  0;  0  0  0]
LB2 = [ 0  1  1;  0  0  1;  0  0  0]
LB3 = [ 0  0  1;  0  0  1;  0  0  1]
LB4 = [ 0  0  0;  0  0  1;  0  1  1]
LB5 = [ 0  0  0;  0  0  0;  1  1  1]
LB6 = [ 0  0  0;  1  0  0;  1  1  0]
LB7 = [ 1  0  0;  1  0  0;  1  0  0]
LB8 = [ 1  1  0;  1  0  0;  0  0  0]

L1 = LO1;
L1(find(LB1>0)) = -1
L2 = LO2;
L2(find(LB2>0)) = -1
L3 = LO3;
L3(find(LB3>0)) = -1
L4 = LO4;
L4(find(LB4>0)) = -1
L5 = LO5;
L5(find(LB5>0)) = -1
L6 = LO6;
L6(find(LB6>0)) = -1
L7 = LO7;
L7(find(LB7>0)) = -1
L8 = LO8;
L8(find(LB8>0)) = -1


seO1 = strel('arbitrary', LO1);
seO2 = strel('arbitrary', LO2);
seO3 = strel('arbitrary', LO3);
seO4 = strel('arbitrary', LO4);
seO5 = strel('arbitrary', LO5);
seO6 = strel('arbitrary', LO6);
seO7 = strel('arbitrary', LO7);
seO8 = strel('arbitrary', LO8);

seB1 = strel('arbitrary', LB1);
seB2 = strel('arbitrary', LB2);
seB3 = strel('arbitrary', LB3);
seB4 = strel('arbitrary', LB4);
seB5 = strel('arbitrary', LB5);
seB6 = strel('arbitrary', LB6);
seB7 = strel('arbitrary', LB7);
seB8 = strel('arbitrary', LB8);



flag = 1;
i = 1;
figure;
while flag == 1
    fprintf('iteration: %d', i);
    flag = 0;
    i = i+1;
    tt1 = bw0;
    tt2 = bwhitmiss(tt1,L1);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L2);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L3);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L4);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L5);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L6);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L7);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L8);
    tt1 = tt1-tt2;
    bw1 = tt1;
    
    
   [count1, tt] = size(find(bw1>0));
   [count0, tt] = size(find(bw0>0));
      if count1 ~= count0
        flag = 1;
        bw0 = bw1;
        imshow(bw0);
        input('');
    end
end

% pruning
  

L1 = [ 0  1  0; -1  1 -1; -1 -1 -1]
L2 = [-1  0  0; -1  1 -1; -1 -1 -1]
L3 = [-1 -1  0; -1  1  1; -1 -1  0]
L4 = [-1 -1 -1; -1  1  0; -1 -1  0]
L5 = [-1 -1 -1; -1  1 -1;  0  1  0]
L6 = [-1 -1 -1; -1  1 -1;  0  0 -1]
L7 = [ 0 -1 -1;  1  1 -1;  0 -1 -1]
L8 = [ 0 -1 -1;  0  1 -1; -1 -1 -1]
L1 = [-1 -1 -1; -1  1 -1; -1 -1 -1]

flag = 1;
i = 1;
while flag == 1
    fprintf('prune iteration: %d', i);
    flag = 0;
    i = i+1;
    tt1 = bw0;
    tt2 = bwhitmiss(tt1,L1);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L2);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L3);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L4);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L5);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L6);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L7);
    tt1 = tt1-tt2;
    tt2 = bwhitmiss(tt1,L8);
    tt1 = tt1-tt2;
    bw1 = tt1;
    
    
   [count1, tt] = size(find(bw1>0));
   [count0, tt] = size(find(bw0>0));
      if count1 ~= count0
        flag = 1;
        bw0 = bw1;
        imshow(bw0);
        h = input(' (more?)', 's');
        if h == 'n'
            flag = 0;
        end
    end
end
