watershed
implements one of the variants of watershed,
non-parametric marker-based segmentation algorithm, described in Meyer (1992).
Details
Before passing image
to the function, you have to roughly
outline the desired regions in the markers
image with positive (>0)
indices. So, every region is represented as one or more connected components
with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from
a binary mask using findContours
. The markers are "seeds" of
the future image regions. All the other pixels in markers
, whose
relation to the outlined regions is not known and should be defined by the
algorithm, should be set to 0's. In the function output, each pixel in
markers
is set to a value of the "seed" components or to -1 at
boundaries between the regions.
References
Meyer F. Color image segmentation. 1992 International Conference on Image Processing. 1992. Available: https://ieeexplore.ieee.org/abstract/document/785528/
Author
Simon Garnier, garnier@njit.edu
Examples
dots <- image(system.file("sample_img/dots.jpg", package = "Rvision"))
bw <- inRange(dots, 0, 250)
medianBlur(bw, target = "self")
#> NULL
sure_bg <- morph(bw, "dilate", k_shape = "ellipse", iterations = 3)
dt <- distanceTransform(bw, "L2")
sure_fg <- dt > 20
unknown <- sure_bg - sure_fg
markers <- connectedComponents(sure_fg, table = FALSE)$labels + 1
markers <- markers * (invert(unknown) / 255)
watershed(dots, markers)
#> NULL