How to run the GrayscaleAttributeFilteringPlugin of MorphoLibJ in a python macro

macro
python
morpholibj

#1

Hi!
I am trying to write in Python the following command:


run("Gray Scale Attribute Filtering", "operation=Closing attribute=[Box Diagonal] minimum=50 connectivity=4")

It’s a while that I am trying to figure it out :-/ I wouldn’t like to use the IJ.run() command but rather call the class directly - to not have to show the image.

I saw this question (very clear answer!)
https://forum.image.sc/t/how-to-enable-morpholibj-plugin-to-work-in-python-macro/4158)
but I cannot translate it to the GrayscaleAttributeFilteringPlugin class.

Please help - I am completely stuck
many thanks in advance!

sylvia


#2

Okay, I found it; Maybe it can be of use for somebody, so I’ll answer to myself:

import os
from ij import IJ, ImagePlus
from inra.ijpb.morphology.attrfilt import BoxDiagonalOpeningQueue
from inra.ijpb.algo import DefaultAlgoListener

# IJ.run(singleSlice, "Gray Scale Attribute Filtering", "operation=Closing attribute=[Box Diagonal] minimum=50 connectivity=4")	

IJ.run("Close All")
path ="<yourPath>"
imp = IJ.openImage(os.path.join(path, '<yourFilname>'))
#imp.show()

image2 = imp.getProcessor().duplicate()
image2 = image2.duplicate()
image2.invert()

algo = BoxDiagonalOpeningQueue()
algo.setConnectivity(4)
DefaultAlgoListener.monitor(algo)
result = algo.process(image2, 50)
result.invert()

outImage = ImagePlus("result", result)
#outImage.show()

the lesson i learned from that: “do not consult the java doc, but go to the source code instead” :wink:


#3

Hi Sylvia,

glad to read that you could find the solution! And yes, the doc and manual still do not cover all aspects of the library…

We usually tried to provide different ways for accessing functionnalities in the library:

  • using the Plugin, either from the GUI or by calling the “run(…)” command
  • using a static method from a utility class. For example: inra.ijpb.morphology.AttributeFiltering.areaOpening(…)
  • by creating an instance of a low-level algorithm, as you finally discovered. This final solution has the advantage that most of the time, it is possible to add monitoring of the execution via the AlgoListener interface.

We’ll try to update the manual accordingly in a future version!

Best,
David


#4

Hey David,
Thanks for the explanation! I understand it better now :wink:
Great library!!
cheers, sy