[morpholibJ] keep largest region withouth showing the result image

Dear all,
is there a way to use this plugin:

from morpholibj suite without showing the image and getting it in a variable?

it would be super useful inside a script, instead of getting it using WindowManager and then hide it.

thank you,
Emanuele

1 Like

Hi,

are you using a scripting language? Then it is easy, you need essentially this line (line49) + the packge imports:
resultPlus = LabelImages.keepLargestLabel(imagePlus);

The API of morpholibj lives here (direct link to LabelImages: https://javadoc.scijava.org/MorphoLibJ/inra/ijpb/label/LabelImages.html )

For macro I don’t know.

1 Like

Hey @emartini,

that’s a cool plugin in #morpholibj indeed. If you can’t call it in macro directly, you may want to explore calling it via CLIJ:


// Init GPU
run("CLIJ2 Macro Extensions", "cl_device=");

// Load image from disc 
open("C:/structure/data/blobs.tif");
image1 = getTitle();
Ext.CLIJ2_push(image1);

// Threshold Otsu
Ext.CLIJ2_thresholdOtsu(image1, image2);
Ext.CLIJ2_release(image1);

Ext.CLIJ2_pull(image2);

// Connected Components Labeling Box
Ext.CLIJ2_connectedComponentsLabelingBox(image2, image3);
Ext.CLIJ2_release(image2);

Ext.CLIJ2_pull(image3);
run("glasbey_on_dark");

// Keep Largest Region
Ext.CLIJx_morphoLibJKeepLargestRegion(image3, image4);
Ext.CLIJ2_release(image3);

Ext.CLIJ2_pull(image4);
Ext.CLIJ2_release(image4);

You can get the result in a variable (image4) by not calling the “pull” command.

In order to make it run, you need to install clij, clij2 and the clijx-assistant-extesions as described here:

Let me know if it’s useful! This is experimental code and I need user feedback to see if this should survive or not :wink:

Cheers,
Robert

2 Likes

Hi @noreenw,
yes I am using jython scripting language.

I had found that api page but there is still a step that I think I am missing, what is the input of that call?

resultPlus = LabelImages.keepLargestLabel(imagePlus)

how it is supposed to be the ImagePlus that I pass?

for example this snippet, doesn’t produce an image with only the largest region, but it doesn’t throw any error: it simply returns the input ImagePlus

from ij import IJ
from inra.ijpb.label import LabelImages

imp = IJ.createImage("Untitled", "8-bit black", 512, 512, 1);
imp.setRoi(65,26,198,212);
IJ.run(imp, "Set...", "value=255");
imp.setRoi(366,353,39,45);
IJ.run(imp, "Set...", "value=255");

largest_region = LabelImages.keepLargestLabel(imp)
largest_region.show()


Do I have to “labelise” someway the imageplus?

thank you for your help
Emanuele

dear @haesleinhuepf,
at the moment I am trying it in jython and with no-gpu approach.

But of course if I’ll have time I will give it a try with clij2

ciao,
Emanuele Martini

1 Like

dear @noreenw
found a solution: if you are working with binary images, you can use directly this *BinaryImages api:


with the keepLargestRegion(ij.ImagePlus imagePlus) method

this way:

from ij import IJ
from inra.ijpb.binary import BinaryImages

imp = IJ.createImage("Untitled", "8-bit black", 512, 512, 1);
imp.setRoi(65,26,198,212);
IJ.run(imp, "Set...", "value=255");
imp.setRoi(366,353,39,45);
IJ.run(imp, "Set...", "value=255");
imp.show();
largest_region = BinaryImages.keepLargestRegion(imp)
largest_region.show()

hth,
Emanuele Martini

3 Likes

Happy to hear that you found the solution already!
Yes indeed, for black-white images the BinaryImages class would be the way to go (or alternatively do a connected component labeling and then use LabelImages).

3 Likes

Indeed.

What you show is a binary image. Both of the rectangles have the “label” 255, so they’re in fact treated as the same (but disconnected) object by any algorithm that expects a “label image” (or “label map”).

You first need to create an image where each object has a distinct label, e.g. by doing a connected component analysis. In Analyze Particles, use the Count Mask option to get this. CLIJ has a function for that as well, of course.

2 Likes