How to enable MorphoLibJ Plugin to work in Python Macro

development
python
plugin
fiji
Tags: #<Tag:0x00007fb883364500> #<Tag:0x00007fb8833642d0> #<Tag:0x00007fb88336ba80> #<Tag:0x00007fb88336b6e8>

#1

Hi,

I have the following image:

And with this script

open("/Users/pdubois/Desktop/cont1_01_crop1.tif")
run("8-bit");
run("Set Scale...", "distance=0.44 known=1 pixel=1 unit=um");
run("Despeckle");
run("Morphological Filters", "operation=Closing element=Disk radius=5");
setAutoThreshold("Minimum dark");
setThreshold(157, 255);
run("Analyze Particles...", "display clear include summarize add");
saveAs("Jpeg", "/Users/pdubois/Desktop/cont1_01_crop1-Closing.jpg");
saveAs("Results", "/Users/pdubois /Desktop/Results.xls");

Which produces this:

I have no problem calculating the area that yield the result with size 44318.182.
But when I tried with Python:

    from ij import IJ
    # Open Image
    imp = IJ.openImage("/Users/pdubois/Desktop/cont1_01_crop1.tif");
    IJ.run(imp, "8-bit", "");
    IJ.run(imp, "Set Scale...", "distance=0.44 known=1 pixel=1 unit=um");
    IJ.run(imp, "Despeckle", "");
   #It seems that Morphological Filters doesn't work.
    IJ.run(imp, "Morphological Filters", "operation=Closing element=Disk radius=5");
    IJ.setAutoThreshold(imp, "Minimum dark");
    IJ.setThreshold(imp, 157, 255);
    IJ.run(imp, "Analyze Particles...", "display clear include summarize add")
    IJ.selectWindow("Results")
    IJ.saveAs("Results", "/Users/pdubois/Desktop/Results.xls");
    IJ.run("Close");
    IJ.selectWindow("ROI Manager")
    IJ.run("Close");
    IJ.saveAs(imp, "Jpeg", "/Users/pdubois/Desktop/cont1_01_crop1_segm.jpg");
    IJ.run("Close");

The result is wrong (29307.852), I believe this is caused by Morphological filters doesn’t get executed.
This produces this:

How can I do it properly in Python?

I really need to get the Python version running to integrate with my other larger program.


#2

Here you are:

from ij import IJ
from inra.ijpb.morphology import Strel
from inra.ijpb.morphology import Morphology
# Open Image
imp = IJ.openImage("/Users/pdubois/Desktop/cont1_01_crop1.tif");
IJ.run(imp, "8-bit", "");
IJ.run(imp, "Set Scale...", "distance=0.44 known=1 pixel=1 unit=um");
IJ.run(imp, "Despeckle", "");

# Create structuring element
strel = Strel.Shape.DISK.fromRadius( 5 );
# Apply filter
ip = Morphology.closing( imp.getProcessor(), strel );
# Set result to current image (imp)
imp.setProcessor( ip );
    
IJ.setAutoThreshold(imp, "Minimum dark");
IJ.setThreshold(imp, 157, 255);
IJ.run(imp, "Analyze Particles...", "display clear include summarize add")
IJ.selectWindow("Results")
IJ.saveAs("Results", "/Users/pdubois/Desktop/Results.xls");
IJ.run("Close");
IJ.selectWindow("ROI Manager")
IJ.run("Close");
IJ.saveAs(imp, "Jpeg", "/Users/pdubois/Desktop/cont1_01_crop1_segm.jpg");
IJ.run("Close");

#3

This is great. Where can I find documentation for the Python API?


#4

Have a look at the Jython scripting site. You can use any method from the ImageJ API and, of course, from any of the plugins you have installed.


#5

I got this error:

Traceback (most recent call last):
  File "mycode.py", line 9, in <module>
    from inra.ijpb.morphology import Morph
ImportError: cannot import name Morph

How can I resolve it?

The command line I use is this:

/Applications/Fiji.app/Contents/MacOS/ImageJ-macosx --ij2 -macro measure_area.py


#6

Hi,
the “Morph” import seems suspicious. Try replacing by “from inra.ijpb.morphology import Morphology”, maybe there was an unexpected truncation in the code sample.


#7

Indeed, I will fix the original post.