%img_source = binary image function [result] = distance(img_source) %distance map dist = img_source.*0 + (1-img_source).*1000; %distance mask to obtain less than 8% error according to the real euclidian distance %(warning, the resulting ditance is multiplied by 3) mask(1,:) = [4, 3, 4]; mask(2,:) = [3, 0, 3]; mask(3,:) = [4, 3, 4]; %loop over the image to compute chanfrein distance [h,w] = size(img_source); %first pass dist_temp = dist; for i=2:h-1 for j=2:w-1 dmin = dist(i,j); for n=-1:1 dcurr = dist_temp(i-1,j+n)+mask(1,2+n); if dcurr