Invoke Watershed algorithm in Java



Hi to everyone,

I would like to know how to invoke the watershed algorithm for image segmentation in java program. Please share us any samples or documentation.

Looking forward for any suggestion.


Hi @BalaMIB,

This should be possible via the toWatershed(i) method in the EDM() constructor.


Hi @biovoxxel,

Thanks for the information. Can you share us more information on input format for the function “toWaterShed(i)” and please clarify me, is it we require to generate a marker image or it will generate inside the toWatershed function. If possible, please provide me any samples or documentation to understand further,


Hello Bala -

As Jan mentioned, you can use the (non-static) EDM.toWatershed()
method. (To answer your question to Jan, this method takes a
black-and-white ImageProcessor (that is expected to be a
ByteProcessor) as its argument, and modifies it in place.)

Here is a complete java plugin that illustrates this:

import ij.*;
import ij.plugin.PlugIn;
import ij.plugin.filter.EDM;

public class My_Plugin implements PlugIn {
  public void run(String arg) {
    ImagePlus imp = IJ.openImage ("");
    EDM edm = new EDM();
    edm.toWatershed (imp.getProcessor());;

(I am working in the context of the Fiji distribution of ImageJ 1.52h.)

You can find this by going to the search bar in the main Fiji / ImageJ
window and typing “watershed”. This will show you:

Menu path
Process > Binary > Watershed

If you click on the Source button, it will open a browser page for
you for the github source for

Poking around in the source, you will find toWatershed().

This algorithm works on binary images – that’s why I specifically chose
particles.gif from the sample images.

There are also gray-scale watershed algorithms packaged as ImageJ
plugins. But they don’t come with Fiji / ImageJ out of the box, so you
have to install them.

One such is:

Classic Watershed (IJPB-plugins)

Its source is here:

I haven’t tried any of that code, but it has a static method,
Watershed.computeWatershed() that looks like it might to
what you want.

	public static ImagePlus computeWatershed( 
			ImagePlus input,
			ImagePlus mask,
			int connectivity,
			double hMin,
			double hMax )

Thanks, mm