How to run the GrayscaleAttributeFilteringPlugin of MorphoLibJ in a python macro



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 command but rather call the class directly - to not have to show the image.

I saw this question (very clear answer!)
but I cannot translate it to the GrayscaleAttributeFilteringPlugin class.

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



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

#, "Gray Scale Attribute Filtering", "operation=Closing attribute=[Box Diagonal] minimum=50 connectivity=4")"Close All")
path ="<yourPath>"
imp = IJ.openImage(os.path.join(path, '<yourFilname>'))

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

algo = BoxDiagonalOpeningQueue()
result = algo.process(image2, 50)

outImage = ImagePlus("result", result)

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


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!



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