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

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)

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.

2 Likes

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 Likes

Wow thank you! Great to learn about this technique!

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!

Dear Jan,
This seems to be a good solution, but I am not getting the groovy code to work. I first copied the example code into a new Fiji sheet and changed it to record ‘dispersal’ because that is my interest from Directionality. I saved the code as a groovy file in scripts and created an ‘Analyze’ folder and saved it there. Then after restarting FIJI it still tells me "unrecognized command.
the macro I am writing:

run("Duplicate...", "title=temp");
run("Select None");
setTool("elliptical");
waitForUser("Selection","Select an elipse around the line you want to analyze");
run("8-bit");
run("Directionality", "method=[Local gradient orientation] nbins=90 histogram=-90");
run("Get_Dispersal.groovy");
Ldisp = getResult()
{close("temp");}

How to fix?
Best,
Freerk

The macro command to call the script needs to correspond to the menu entry, not the file name.

That means, if you saved your script e.g. under Fiji.app/scripts/My_Scripts/Get_Dispersal.groovy, it will show up in the menu at My Scripts > Get Dispersal, and the macro recorder will record the following command when you run it:

run("Get Dispersal");