function [b] = getborder(I)

b = 0;

for y = 1:size(I,1)
    for x = 1:size(I,2)
        if I(y,x) > 0
            b = [x y];
            break;
        else
            c = [x y];
        end
    end
    
    if b ~= 0
        break;
    end
end

while 1
    [c, bnew] = getB(I, b(end,:), c);
    
    if bnew == b(1, :)
        break;
    else
        b = [b; bnew];
    end
end
end

function [c, b] = getB(I, b, c)
    d = c-b;
    
    mat = [-1 0; -1 -1; 0 -1; 1 -1; 1 0; 1 1; 0 1; -1 1];
    
    for row = 1:size(mat, 1)
        if mat(row, :) == d
            break;
        end
    end
    
    row = row + 1;
    if row > 8
        row = 1;
    end
    
    for j = 1:8
        pnt = b + mat(row, :);
        
        row = row + 1;
        if row > 8
            row = 1;
        end
        
        if I(pnt(2), pnt(1)) > 0
            b = [pnt(1) pnt(2)];
            return;
        else
            c = [pnt(1) pnt(2)];
        end
        
    end
    
    
%     if d == [-1 0]
%         
%     elseif d == [-1 -1]
%     elseif d == [0 -1]
%     elseif d == [1 -1]
%     elseif d == [1 0]
%     elseif d == [1 1]
%     elseif d == [0 1]
%     elseif d == [-1 1]
%     end
end

