function [output] = skeleton(X,scaling,threshold)

[m,n] = size(X);

ero = X;
skel = zeros(m,n);
scalctr = 1;
% lastscal = 0;

while 1
    tempero = parero(X,1/scalctr * scaling);
%     if(lastscal ~= 0)
%         tempdiff = ero - parero(pardil(tempero,1/scalctr * scaling),1/lastscal * scaling);
%     else
        tempdiff = ero - pardil(tempero,1/scalctr * scaling);
%     end
    
    if(max(tempdiff(:)) <= threshold)
        break;
    end
    skel = skel + (tempdiff .* not(tempdiff < 0));
    ero = tempero;
    scalctr = 2 * scalctr;
%     lastscal = scalctr;
end
output = skel;% .* not(skel < 0);

end