%define the object as on slide 13, lecture 9
image = zeros(16,14);
image(1,10:end)=255;
image(2,9:end)=255;
image(3,7:end)=255;
image(4,7:end)=255;
image(5,6:end)=255;
image(6,5:end-1)=255;
image(7,3:end-3)=255;
image(8,2:end-5)=255;
image(9,2:end-5)=255;
image(10,2:end-3)=255;
image(11,3:end-3)=255;
image(12,4:end-4)=255;
image(13,4:end-4)=255;
image(14,4:end-4)=255;
image(15,5:end-5)=255;
figure
imshow(image);

[m n]=size(image);

% row scan
for j=1:m
 
% first scan
    for i = 1:n
   
    if  image(j,i)~= 0
       % object pixel
       if j == 1
             image(j,i) = 1;
      
       else
           
           image(j,i)=min(image(j,i),(image(j-1,i) + 1));
       end %  elseif
       
       end %second if
    
    end%first if

end %for j statment first scan

% second scan
for j = m :-1:1
    
    for i=1:n
 
    if image(j,i) ~= 0
    %object pixel
    
    if j == m
    
        image(j,i) = 1;
        
       
    
    else
        
        
       
        end
           image(j,i)=min(image(j,i),(image(j+1,i) + 1));
    
    end

    end

    end



val = image;

% 
% rows = m;
% columns = n;
% %step2
% for x=1:columns
% for y=1:rows
%      i = 1:rows;
%      g(y)=min (sqrt(image(i, x)'.^2+(y-i).^2));
%      image(y, x) = g(y);
%     end
%     %g
%     %  (ii)
%     %image(:, x) = g';
% end

rows = m;
columns = n;
%step2
for y=1:rows
for x=1:columns
     i = 1:columns;
     g(x)=min (sqrt(image(y, i).^2+(x-i).^2));
     image(y, x) = g(x);
    end
    %g
    %  (ii)
    %image(:, x) = g';
end


val = image;