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: