Get "Direction (°)" from Directionality using an IJ1 Macro

imagej
macro
results-table
directionality

#1

Hello,

I’m trying to write a macro that utilizes the Analyze > Directionality plugin. After running the plugin, a (what appears to be a) javascript window pops up and displays generated statistics from the analysis. I want to extract the value of the “Direction (°)” and assign that value to a variable in my macro.

I have tried using “getResult” but the table doesn’t seem to be a normal FIJI Result table (aside from its irregular name). So the solution here didn’t seem relevant to me.

Does anyone know how to do this?

Below is the code that I am working with. The unsuccessful things that I have tried are commented out.

run("Z Project...", "projection=[Average Intensity]");
run("8-bit");
run("Directionality", "method=[Local gradient orientation] nbins=90 histogram=-90");
//IJ.renameResults("Directionality analysis for AVG_0096 (using Local gradient orientation)","Results");
//getResult("Direction (°)",1)

#2

The getResult() and IJ.renameResults() macro functions do not work with the table produced by the Directionality plugin because it is a JTable. There may be a way in a script or plugin to get a reference to this JTable (it’s not listed in the Window menu) and to access its cells.


#3

As @Wayne said, the displayed table is not an ImageJ ResultsTable. You can resort to scripting as explained in the plugin documentation:

For example, you can use the following Groovy script from the script editor:

#@ ImagePlus imp
#@ ResultsTable rt

import fiji.analyze.directionality.Directionality_

d = new Directionality_()

d.setImagePlus(imp)
d.computeHistograms()
d.fitHistograms()

result = d.getFitAnalysis()

mainDirection = Math.toDegrees(result[0][0])

rt.incrementCounter()
rt.addValue("Main Direction", mainDirection)
rt.show("Results")

If you save this script in a subfolder of Fiji’s scripts directory, e.g. as ./Fiji.app/scripts/Analyze/Get_Main_Direction.groovy, and restart Fiji, you’ll then be able to run it from the menu and record its usage with the macro recorder, which will record something like:

run("Get Main Direction");

Subsequently you can get the value from the standard results table in the way you tried.


#4

Wow thank you! Great to learn about this technique!


#5

I came across this thread trying to batch the Directionality analysis. For anyone interested here is an implementation for batch processing and returning all 4 Directionality parameters in a results table:

https://bitbucket.org/davemason/gvy_batchdirectionality/src/master/

Thanks to @imagejan for the code to get me started!