width=512; height=512; % sdf1=imread('SDF_03.bmp'); sdf1=rgb2gray(sdf1); fori=1:height forj=1:width if sdf1(i,j)>0 sdf1(i,j)=255; end end end % sdf2=imread('SDF_04.bmp'); sdf2=rgb2gray(sdf2); fori=1:height forj=1:width if sdf2(i,j)>0 sdf2(i,j)=255; end end end % qx=zeros(width*height+5,1); qy=zeros(width*height+5,1); qr=zeros(width*height+5,1); sdfres=zeros(width,height); % left=1; right=0; vis=zeros(height,width); fori=1:height forj=1:width if sdf1(i,j)==255 sdfres(i,j)=0; vis(i,j)=1; right=right+1; qx(right,1)=i; qy(right,1)=j; qr(right,1)=0; end end end
while left<=right x=qx(left,1); y=qy(left,1); r=qr(left,1)+1; % dx=-1; dy=1; gx=x+dx; gy=y+dy; if gx>=1&&gx<=height&&gy>=1&&gy<=width&&vis(gx,gy)==0 vis(gx,gy)=1; sdfres(gx,gy)=r; right=right+1; qx(right,1)=gx; qy(right,1)=gy; qr(right,1)=r; end % dx=1; dy=1; gx=x+dx; gy=y+dy; if gx>=1&&gx<=height&&gy>=1&&gy<=width&&vis(gx,gy)==0 vis(gx,gy)=1; sdfres(gx,gy)=r; right=right+1; qx(right,1)=gx; qy(right,1)=gy; qr(right,1)=r; end % dx=1; dy=-1; gx=x+dx; gy=y+dy; if gx>=1&&gx<=height&&gy>=1&&gy<=width&&vis(gx,gy)==0 vis(gx,gy)=1; sdfres(gx,gy)=r; right=right+1; qx(right,1)=gx; qy(right,1)=gy; qr(right,1)=r; end % dx=-1; dy=-1; gx=x+dx; gy=y+dy; if gx>=1&&gx<=height&&gy>=1&&gy<=width&&vis(gx,gy)==0 vis(gx,gy)=1; sdfres(gx,gy)=r; right=right+1; qx(right,1)=gx; qy(right,1)=gy; qr(right,1)=r; end
left=left+1; end fori=1:height forj=1:width if sdf2(i,j)==0 sdfres(i,j)=0; end end end sdfres=(sdfres-min(sdfres(:)))*(255.0-0)/(max(sdfres(:))-min(sdfres(:)))+0; fori=1:height forj=1:width if sdf2(i,j)==0 sdfres(i,j)=0; else sdfres(i,j)=255-sdfres(i,j); end end end sdfres=uint8(sdfres); imwrite(sdfres,'SDFout2.bmp'); b=imread('SDFout2.bmp'); imshow(b);