ccc

a = rgb2gray(imread('bone2.png'));
th = multithresh(a);
a(a <= th) = 0;
a(a > th) = 1;

% a = zeros(80, 80);
% a(20:60, 10:60) = 1;


r = a;



delpnts = 1;

while delpnts > 0
  
  b = getborder(a);
  
%   disp('step 1');
  
  del = [];
  for m = 1:length(b)
    x = b(m, 1);
    y = b(m, 2);

    % step 1
    p2 = a(y-1,x); p3 = a(y-1, x+1); p4 = a(y, x+1); p5 = a(y+1, x+1);
    p6 = a(y+1, x); p7 = a(y+1, x-1); p8 = a(y, x-1); p9 = a(y-1, x-1);

    N = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;

    T = 0;

    p = [p2 p3 p4 p5 p6 p7 p8 p9 p2];
    for n = 1:length(p)-1
      if (p(n) == 0) && (p(n+1) == 1)
        T = T + 1;
      end
    end

    prod1 = p2 * p4 * p6;
    prod2 = p4 * p6 * p8;


    if (2 <= N) && (N <= 6) && (T == 1) && (prod1 == 0) && (prod2 == 0)
      del = [del; y x];
    end

  end

  delpnts = 0;

  for m = 1:size(del,1)
    y = del(m, 1);
    x = del(m, 2);
    a(y, x) = 0;
    delpnts = delpnts + 1;
  end

  del = [];

  for m = 1:length(b)
     x = b(m, 1);
    y = b(m, 2);

    % step 1
    p2 = a(y-1,x); p3 = a(y-1, x+1); p4 = a(y, x+1); p5 = a(y+1, x+1);
    p6 = a(y+1, x); p7 = a(y+1, x-1); p8 = a(y, x-1); p9 = a(y-1, x-1);

    N = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;

    T = 0;

    p = [p2 p3 p4 p5 p6 p7 p8 p9 p2];
    for n = 1:length(p)-1
      if (p(n) == 0) && (p(n+1) == 1)
        T = T + 1;
      end
    end

    prod3 = p2 * p4 * p8;
    prod4 = p2 * p6 * p8;


    if (2 <= N) && (N <= 6) && (T == 1) && (prod3 == 0) && (prod4 == 0)
      del = [del; y x];
    end
  end

  delpnts = 0;

  for m = 1:size(del,1)
    y = del(m, 1);
    x = del(m, 2);
    a(y, x) = 0;
    delpnts = delpnts + 1;
  end

%   figure(1);
%   imshow(a,[])
%   pause(0.1);

end

[y, x] = find(a > 0);

figure(1);
imshow(r,[]);
hold on;
plot(x, y, 'r.');
hold off



