I see the problem now. The marker image cannot be binary, otherwise all
markers are the same (255). You need to apply connected components on top
of that. Please find the correct code below.
Cheers!
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;
import inra.ijpb.watershed.Watershed;
import inra.ijpb.binary.BinaryImages;
public class Marker_Watershed implements PlugIn {
public void run(String arg) {
ImagePlus imp = IJ.getImage();
ImageProcessor ip = imp.getProcessor();
int width = imp.getWidth();
int height = imp.getHeight();
PointRoi pointRoi = (PointRoi) imp.getRoi();
if (pointRoi == null) {
IJ.showMessage("No Marker", "There are no markers drawn");
return;
}
ImageProcessor ipMarkers = new ByteProcessor(width, height);
FloatPolygon fPolygon = (FloatPolygon) pointRoi.getFloatPolygon();
int length = fPolygon.npoints;
float[] xPoints = fPolygon.xpoints;
float[] yPoints = fPolygon.ypoints;
for (int index = 0; index < length; index++) {
ipMarkers.set((int) Math.round(xPoints[index]), (int)
Math.round(yPoints[index]), 255);
}
ImagePlus impMarkers = new ImagePlus("Markers", ipMarkers);
impMarkers.show();
ImagePlus labeledMarkers = BinaryImages.componentsLabeling(
impMarkers, 4, 16 );
labeledMarkers.show();
ImagePlus impSeg = Watershed.computeWatershed(imp, labeledMarkers,
null, 4, true, 0, true);
IJ.run(impSeg, "3-3-2 RGB", "");
impSeg.show();
}
}
On Sat, Aug 8, 2026 at 8:54 AM Ignacio Arganda-Carreras <
[email protected]> wrote:
> I see the problem now. The marker image cannot be binary, otherwise all
> markers are the same (255). You need to apply connected components on top
> of that. Please find the correct code attached.
>
> Cheers!
>
> On Fri, Aug 7, 2026 at 10:20 PM Yuekan Jiao <[email protected]> wrote:
>
>> Hi Ignacio,
>>
>> I have attached the image that shows the boundaries of blobs, and the
>> blobs
>> are marked using ImageJ "Multi-point" tool. I also attached my test
>> plugin.
>>
>> Thank you!
>>
>> Yuekan
>>
>> On Fri, Aug 7, 2026 at 9:34 AM Ignacio Arganda-Carreras <
>> [email protected]> wrote:
>>
>> > Hello Yuekan,
>> >
>> > You're using this method:
>> >
>> >
>> >
>> https://ijpb.github.io/MorphoLibJ/javadoc/inra/ijpb/watershed/Watershed.html#computeWatershed(ij.ImagePlus,ij.ImagePlus,ij.ImagePlus,int,boolean,double,boolean)
>> >
>> > Therefore, the "imp" image should be the input image where the watershed
>> > method will take the pixel values from. This is usually a "gradient"
>> image
>> > where the borders of the objects to be segmented are highlighted. Is
>> this
>> > your case? You can maybe post here the images you used?
>> >
>> > Cheers!
>> >
>> >
>> > On Thu, Aug 6, 2026 at 8:06 PM Yuekan Jiao <[email protected]>
>> wrote:
>> >
>> > > Hi
>> > >
>> > > I am trying to use MorphoLibJ Marker-controlled Watershed /
>> Interactive
>> > > Marker-controlled Watershed in my Java code. Using installed
>> MorphoLibJ
>> > it
>> > > works well, for example, on the gradient (I use "Find Edges") of
>> Gaussian
>> > > filtered image blobs. With my own code, however, I get an empty image.
>> > The
>> > > command in my code is:
>> > >
>> > > ImagePlus impSeg = Watershed.computeWatershed(imp, impMarkers, null,
>> 4,
>> > > true, 0, true);
>> > >
>> > > imp - the Gaussian filtered image blobs with markers drawn using
>> > > "Multi-point" tool
>> > > impMarkers - the binary image of the markers
>> > >
>> > > I also tried some similar functions included in class Watershed, but
>> > could
>> > > not get it to work.
>> > >
>> > > Could anyone tell me what I am missing? Thank you.
>> > >
>> > > Yuekan
>> > >
>> > > --
>> > > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>> > >
>> >
>> >
>> > --
>> > Ignacio Arganda-Carreras, Ph.D.
>> > Ikerbasque Research Associate Professor
>> > Departamento de Ciencias de la Computacion e Inteligencia Artificial
>> > Facultad de Informatica, Universidad del Pais Vasco
>> > Paseo de Manuel Lardizabal, 1
>> > 20018 Donostia-San Sebastian
>> > Guipuzcoa, Spain
>> >
>> > Phone : +34 943 01 73 25
>> > Website: https://cvpd.github.io/author/ignacio-arganda-carreras/
>> >
>> > --
>> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>> >
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
>
> --
> Ignacio Arganda-Carreras, Ph.D.
> Ikerbasque Research Associate Professor
> Departamento de Ciencias de la Computacion e Inteligencia Artificial
> Facultad de Informatica, Universidad del Pais Vasco
> Paseo de Manuel Lardizabal, 1
> 20018 Donostia-San Sebastian
> Guipuzcoa, Spain
>
> Phone : +34 943 01 73 25
> Website: https://cvpd.github.io/author/ignacio-arganda-carreras/
>
--
Ignacio Arganda-Carreras, Ph.D.
Ikerbasque Research Associate Professor
Departamento de Ciencias de la Computacion e Inteligencia Artificial
Facultad de Informatica, Universidad del Pais Vasco
Paseo de Manuel Lardizabal, 1
20018 Donostia-San Sebastian
Guipuzcoa, Spain
Phone : +34 943 01 73 25
Website: https://cvpd.github.io/author/ignacio-arganda-carreras/
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html