%description:
% dilate a binary image using a thresholded distance function
%param:
% img_source = binary image
% radius = size of the dilation
%result:
% dilated binary image
function [result] = dilate(img_source, radius)
%distance map
dist = img_source.*0 + (1-img_source).*1000;
%distance mask, compute distances with less than 8% error according to an euclidian distance
%warning this distance mask produce distances 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