watershed | R Documentation |
watershed
implements one of the variants of watershed,
non-parametric marker-based segmentation algorithm, described in Meyer (1992).
watershed(image, markers)
image |
An 8-bit (8U), BGR |
markers |
A signed 32-bit (32S) single-channel (GRAY) |
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.
This function does not return anything. It modifies markers
in
place.
Simon Garnier, garnier@njit.edu
Meyer F. Color image segmentation. 1992 International Conference on Image Processing. 1992. Available: https://ieeexplore.ieee.org/abstract/document/785528/
findContours
dots <- image(system.file("sample_img/dots.jpg", package = "Rvision"))
bw <- inRange(dots, 0, 250)
medianBlur(bw, target = "self")
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.