Invoke Watershed algorithm in Java

imagej

#1

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.


#2

Hi @BalaMIB,

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


#3

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,


#4

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 ("http://wsr.imagej.net/images/particles.gif");
    EDM edm = new EDM();
    edm.toWatershed (imp.getProcessor());
    imp.show();
  }
}

(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 EDM.java:

EDM.java

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:

Watershed.java

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