The 2-D watershed example on Mathworks.com can be summarized below:
bw = imread('someImage.tif');
D = -bwdist(~bw);
D(~bw) = -Inf;
L = watershed(D);
I am not quite understand why D(~bw) = -Inf;
is necessary here which forces pixels that don't belong to the objects to be at -Inf (originally zero).
I tried running the program without that statement, and the result is confusing. Why is it necessary to force those pixels to -Inf in order for watershed to get correct result?
As pointed out by Frederick, I used surf
to plot the surface here,
before running D(~bw) = -Inf;
, the result is:
After running it, the result is:
-Inf
enhance the contrast between the connected component and the background. Otherwise watershed is very likely to merge all the background with the single connected component to one region. Actually an -Inf
setting will completely eliminate the background from the region of interest. Check out the influence of -Inf
on the segmentation with your sample image. If you set D(~bw) = 0;
, you will get: