imagej / pyimagej

Use ImageJ from Python
https://pyimagej.readthedocs.io/
Other
473 stars 82 forks source link

Send ImageJ results table to Python #279

Closed clarenzheng closed 1 year ago

clarenzheng commented 1 year ago

Hello,

I'm trying to use PyImageJ to integrate into my Python code a macro that opens a results table in a headless ImageJ mode. I would like to use this table for further applications in my Python code--either saving this table to a file or to assigning it to a variable in Python would be adequate for my purposes.

However, the problem is that when I run the macro using ij.py.run_macro(), only the final line of the table gets saved (this only happens when I run the macro in the headless mode--I've tried running it in my desktop ImageJ application and the table saves fine). Thus, both saving the entire table as well as attempting to append each line of the table to a file failed.

When I run the macro, it does also print the lines to my terminal (see below for an example), but is there a way to write this to a Python variable (e.g. append to a list)?

Thank you!

Macro code I am using:

open(openPath);
run("Measure Stack...");
saveAs("Results", savePath);

Excerpt of terminal output:

[java.lang.Enum.toString] 1     55377.923       107.890 69      148     5974705.992     56565261        523[java.lang.Enum.toString]
1       55377.923       107.894 74      159     5974929.177     56567374        524
[java.lang.Enum.toString] 1     55377.923       107.894 74      159     5974929.177     56567374        524[java.lang.Enum.toString]
1       55377.923       107.912 69      153     5975963.035     56577162        525
[java.lang.Enum.toString] 1     55377.923       107.912 69      153     5975963.035     56577162        525[java.lang.Enum.toString]
1       55377.923       107.908 70      164     5975719.253     56574854        526
[java.lang.Enum.toString] 1     55377.923       107.908 70      164     5975719.253     56574854        526[java.lang.Enum.toString]
clarenzheng commented 1 year ago

Resolved.

ctrueden commented 1 year ago

@clarenzheng Would you care to elaborate on how you solved it? It might help others with similar issues.

clarenzheng commented 1 year ago

I worked around the issue by measuring the image slice by slice, rather than the whole thing at a time. I used a for loop in my ImageJ macro that runs setSlice(), run("Measure"), and saves the result, iterating over each slice.