Anyone could explain the function of bwdist for me?

5 views (last 30 days)
Hello, everyone. I learnt a example from help ,which was aim to use watershed function to segment the image.Here is the code
if true
% center1=-10;
center2=-center1;
dist=sqrt(3*(2*center1)^2);
radius=dist/2 * 1.4;
lims=[floor(center1-1.2*radius) ceil(center2+1.2*radius)];
[x,y,z]=meshgrid(lims(1):lims(2));
bw1=sqrt((x-center1).^2+(y-center1).^2+(z-center1).^2)<=radius;
bw2=sqrt((x-center1).^2+(y-center2).^2+(z-center2).^2)<=radius;
bw=bw1|bw2;
figure;
%extract isosurface from volume data 从容积数据里面提取等值曲面
isosurface(x,y,z,bw,0.5);
% 0.5 means isovalue; bw is the volume data;
axis equal;
title('original');
set(gcf,'color','w');
%set:set graphics object properties;
%gcf means current figure handle
% this sentence means set current graphics color to white
xlabel x, ylabel y,zlabel z
xlim(lims), ylim(lims), zlim(lims) %图像的边界
view(3)%set the default 3D view
%计算三维变换;
D=bwdist(~bw);%bwdist distance transform of binary image.
figure;
isosurface(x,y,z,D,radius/2);
axis equal;
title('距离等值变换');
set(gcf,'color','w');
xlabel x,ylabel y,zlabel z;
xlim(lims),ylim(lims),zlim(lims);
view(3);
camlight;%在camera坐标系里面 create or move light
lighting gouraud;% this method is used for curved surface
D=-D;
D(~bw)=-Inf; %force pixels do not belong to the image be -Inf
compute the watershed transform
L=watershed(D); %分水岭的算法,D为待分割的图像
figure;
isosurface(x,y,z,L==2,0.5);
isosurface(x,y,z,L==3,0.5);
axis equal;
title('分割对象');
set(gcf,'color','w');
xlabel x,ylabel y,zlabel z;
xlim(lims),ylim(lims),zlim(lims);
view(3),camlight,lighting gouraud
end
when I run this demo it was right as I expected but there is something that troubles me.
if true
% D=bwdist(~bw)
end
why we need to compute the distance transform of complement? why not force the pixels which do not belong to this to be Inf.

Accepted Answer

Image Analyst
Image Analyst on 30 Sep 2017
Because it's the distance to the nearest non-zero pixel, not the closest zero pixel. So if you want to find the distances within a white blob to the black background, you must invert the image to make the background white.

More Answers (0)

Categories

Find more on 光照、透明度和着色 in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!