image.watershed
result = image.watershed(surface, markers=)
Input: surface, markers · Output: result
What it is
surface is a Mat (row-major, arbitrary real values) — the topographic surface to flood, typically a NEGATED image.distance_transform(mask) (so each blob's own center, the point farthest from any background, is the surface's deepest point and floods first) or a gradient magnitude. markers= is a required named Mat, the same shape as surface: 0 = unlabeled/to-be-flooded, any other value a seed already assigned to that label — required rather than derived from local extrema when omitted, because auto-deriving seeds is a separate, parameter-sensitive algorithm and a silent wrong guess would look like a wrong watershed instead of what it is. Marker-controlled watershed segmentation (priority-flood/Meyer's algorithm): floods strictly by ascending surface value, 4-connected, growing each marker's labeled region outward; where two different regions' flood-fronts meet, that pixel becomes a watershed line and is written 0 (the same value markers uses for "unlabeled", not -1) rather than joining either region — this is what stops two touching blobs from merging into one label.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = image.watershed(surface, markers=) | surface Matsurface is a Mat (row-major, arbitrary real values) — the topographic surface to flood, typically a NEGATED image.distance_transform(mask) (so each blob's own center, the point farthest from any background, is the surface's deepest point and floods first) or a gradient magnitude.markers Matmarkers= is a required named Mat, the same shape as surface: 0 = unlabeled/to-be-flooded, any other value a seed already assigned to that label — required rather than derived from local extrema when omitted, because auto-deriving seeds is a separate, parameter-sensitive algorithm and a silent wrong guess would look like a wrong watershed instead of what it is. | result Mat | Returns a new Mat of labels, same shape as surface. Added in v0.4.0. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
import image
# Two overlapping circles -- one connected blob to bwlabel.
w = 21
h = 13
mask_m = zeros(h, w)
for y in 0 to h - 1
for x in 0 to w - 1
in_a = (x - 6) ^ 2 + (y - 6) ^ 2 <= 25
in_b = (x - 14) ^ 2 + (y - 6) ^ 2 <= 25
if in_a or in_b
mask_m[y, x] = 255
end if
end for
end for
mask = image_from_matrix(mask_m)
n_blobs = bwlabel(mask).count
print(n_blobs) # 1 -- the two circles are one connected blob
dt = image.distance_transform(mask)
surface = -dt # each circle's own center floods first
markers = zeros(h, w)
markers[6, 6] = 1 # circle A's own center
markers[6, 14] = 2 # circle B's own center
labels = image.watershed(surface, markers=markers)
print(labels[6, 2]) # 1 -- deep inside circle A only
print(labels[6, 18]) # 2 -- deep inside circle B only
1 1 2
from images