\HeaderA{watershed}{Watershed transformation and watershed based object detection}{watershed}
\aliasA{watershed,Image-method}{watershed}{watershed,Image.Rdash.method}
\keyword{manip}{watershed}
\begin{Description}\relax
Watershed transformation and watershed based object detection.
\end{Description}
\begin{Usage}
\begin{verbatim}

  ## S4 method for signature 'Image':
  watershed(x, tolerance=1, ext=1, ...)

\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] An object of \code{\LinkA{Image}{Image.Rdash.class}} in the
\code{\LinkA{Grayscale}{Grayscale}} mode. 
\item[\code{tolerance}] The minimum height of the object in the units of image
intensity between its highest point (seed) and the point where it
contacts another object (checked for every contact pixel). If the
height is smaller than the tolerance, the object will be combined with
one of its neighbors, which is the highest. It is assumed that the
function is run on a distance map, therefore the default value
is 1. If running the function on an original grayscale image with
intensity range \code{[0,1]} one should modify this value, down to 0.1
or other which is image specific. 
\item[\code{ext}] Extension of the neighborhood for the detection
of neighboring objects. Higher value smoothes out small objects. 
\item[\code{...}] Reserved. 
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The algorithm identifies and separates objects that stand out of the
background (zero), in other words to use the water fill, the source image
is flipped upside down and the resulting valleys (values with higher
intensities) are filled in first until another object or background is met.
The deepest valleys (pixels with highest intensity) become indexed first.
\end{Details}
\begin{Value}
An object of \code{\LinkA{Image}{Image.Rdash.class}} in the \code{\LinkA{Grayscale}{Grayscale}} with
separate objects indexed be positive integers starting from 1. To preview
the results visually, use \code{\LinkA{display}{display}( \LinkA{display}{display}(result) )}
or use it in combination with \code{\LinkA{paintObjects}{paintObjects}}.
\end{Value}
\begin{Author}\relax
Oleg Sklyar, \email{osklyar@ebi.ac.uk}, 2007
\end{Author}
\begin{SeeAlso}\relax
\code{
  \LinkA{Image}{Image.Rdash.class}, \LinkA{Image}{Image.Rdash.class}, \LinkA{Image}{Image.Rdash.class}, \LinkA{Image}{Image.Rdash.class},
  \bsl{}code\{matchObjects\}
}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
  
  ## load images
  f <- paste( system.file(package="EBImage"), "images/Gene1_G.tif", sep="/" )
  ii = readImage(f)
  ## normalize images
  ii = normalize(ii, separate=TRUE)
  ## segment
  mask = thresh(ii, 25, 25, 0.02)
  ## refine segmentation with morphology filters
  mk3 = morphKern(3)
  mk5 = morphKern(5)
  mask = dilate(erode(closing(mask, mk5), mk3), mk5)
  ## index objects with 'watershed'
  io = watershed( distmap(mask), 1.5, 1)
  if (interactive()) display(io)

\end{ExampleCode}
\end{Examples}

