imagej / pyimagej

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

How to get the printed result as python variables #303

Closed 54601 closed 6 months ago

54601 commented 6 months ago

image I want to get the list for the result of count particles. The document ended before hinting on how to acquire the results for further analysis.

54601 commented 6 months ago

I figured it out! If anyone's interested, it's: ij.py.from_java(ij.convert().convert(ij.ResultsTable.getResultsTable(), sj.jimport("org.scijava.table.Table")))

54601 commented 6 months ago

Is there any method to utilize roiManager() commends in python?

mol666 commented 6 months ago

Try this

rois = ij.RoiManager.getRoiManager()
for roi_index in range(rois.getCount()):
        rois.select(jimp, roi_index)
54601 commented 6 months ago

Try this

rois = ij.RoiManager.getRoiManager()
for roi_index in range(rois.getCount()):
        rois.select(jimp, roi_index)

Thank you for this! It's most helpful indeed. However, I still have a problem: adding 'add' to the Analyze particles command will crash the script: run("Analyze Particles...", "size=2000-Infinity circularity=0.8-1.00 clear add composite slice"); Is there other way to add the results to RoiManger?

elevans commented 6 months ago

Hi @54601, sorry for the late response to this issue. Our team has been busy working on the SciJava ops project.

ij.py.from_java(ij.convert().convert(ij.ResultsTable.getResultsTable(), sj.jimport("org.scijava.table.Table")))

As you discovered, the way to get the ResultsTable to something usable in Python as pandas dataframe, you need to convert the ResultsTable to a SciJava table. This is painful and I added a converter to make this easier here: 3bd99b858efd6758737eb8a1e6247b8f3e700988. I'm working to release a new version of pyimagej soon.

Thank you for this! It's most helpful indeed. However, I still have a problem: adding 'add' to the Analyze particles command will crash the script: run("Analyze Particles...", "size=2000-Infinity circularity=0.8-1.00 clear add composite slice"); Is there other way to add the results to RoiManger?

That's weird! Is there an error output produced? Are you in headless mode?

54601 commented 6 months ago

As you discovered, the way to get the ResultsTable to something usable in Python as pandas dataframe, you need to convert the ResultsTable to a SciJava table. This is painful and I added a converter to make this easier here: 3bd99b8. I'm working to release a new version of pyimagej soon.

Thank you for your contribution to this project! This package has helped me tremendously.

Thank you for this! It's most helpful indeed. However, I still have a problem: adding 'add' to the Analyze particles command will crash the script: run("Analyze Particles...", "size=2000-Infinity circularity=0.8-1.00 clear add composite slice"); Is there other way to add the results to RoiManger?

That's weird! Is there an error output produced? Are you in headless mode?

That's a false alarm, sorry for that. I tried again this day and the problem couldn't be reproduced. Maybe it was a mistake on my side previously.

However, I really want to know how to get the boundaries for Rois, after I select them with rois.select(jimp, roi_index).

elevans commented 6 months ago

Thank you for your contribution to this project! This package has helped me tremendously.

Awesome! I'm glad that you've found pyimagej helpful!

However, I really want to know how to get the boundaries for Rois, after I select them with rois.select(jimp, roi_index).

Once you have a ROI from the RoiManager or the ImagePlus itself you can print it's bounds with roi.getBounds().toString(). Here's an example after loading the RoiManager with a single free hand ROI:

# get the RoiManager
rm = ij.RoiManager.getRoiManager()

# get the first ROI
roi = rm.getRoi(0)

# print it's bounds
print(roi.getBounds().toString())

Which for my random blob I drew returned: java.awt.Rectangle[x=58,y=38,width=141,height=126]

54601 commented 6 months ago

Thank you for your contribution to this project! This package has helped me tremendously.

Awesome! I'm glad that you've found pyimagej helpful!

However, I really want to know how to get the boundaries for Rois, after I select them with rois.select(jimp, roi_index).

Once you have a ROI from the RoiManager or the ImagePlus itself you can print it's bounds with roi.getBounds().toString(). Here's an example after loading the RoiManager with a single free hand ROI:

# get the RoiManager
rm = ij.RoiManager.getRoiManager()

# get the first ROI
roi = rm.getRoi(0)

# print it's bounds
print(roi.getBounds().toString())

Which for my random blob I drew returned: java.awt.Rectangle[x=58,y=38,width=141,height=126]

Huge thanks! That totally works!

elevans commented 6 months ago

Great! I'm glad that was what you were looking for!