ImageJ selection to R headless

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ImageJ selection to R headless

WagnerSC
Dear Bio7 community,

This is my first post to this forum, please excuse inaccuracies.

I'm interested in the Selection to R in the Image-Methods (Bio7-Toolbar). Is it possible to run it in a script in ImageJ? I could not find the script called through activating the button.

What I need to do is to export thousands of ImageJ ROIs as XY pairs for computing the minimal distance between these ROIs. I planned to use R package spatstat for this.

Thank you very much for your help and considerations
Wagner
Reply | Threaded
Open this post in threaded view
|

Re: ImageJ selection to R headless

Bio7
Hello Wagner,

in Bio7 you have access to the ImageJ API and the R API (by means of Rserve).

But if you only need to transfer the coordinates of ROI's you can simply load all ROI's and
add them to the ROI Manager. If you invoke the "Selection" action all available ROI coordinates
are transferred to R as a 2D-List which can then be postprocessed in R.

Example:

http://www.uni-bielefeld.de/biologie/Oekosystembiologie/bio7app/flashtut/shape_analysis.htm

In additon you can write a script (BeanShell, Jython, Groovy) or create a Java file to load Images
from a folder, make a particle analysis and then transfer the centroid-coordinates to R.

Example:

http://bio7.org/flash/batchimagejr.htm

Script:

https://github.com/Bio7/Bio7/blob/master/Bio7_1.7_Examples/Bio7_Examples/Since_1.6/ClusterMultiple%20Files.bsh

Example transfer coordinates Particle Analysis (API available to automate):

http://bio7.org/flash/newparticlemeasure.htm


Maybe the first suggestion would be painless, if you have the ROI's already available.

If you need somethong like this: Load Roi->transfer coordinates to R->Load ROI->tranfer coordinates to R....

A: You can use the ImageJ APi to load all ROI's from the files and then transfer them in one shot (see suggestion above).

B. If necessary you need a patch to trigger the "Selection" or "Points" action from a script.

However i would need a more detailed description if the suggestions don't fit youre needs.


Reply | Threaded
Open this post in threaded view
|

Re: ImageJ selection to R headless

WagnerSC
Hello,

thank you for the fast reply. I do have the ROI files ready and the first suggestion suits my needs.

I realized that R does not acquire the name of the transferred ROI. I will need a list to keep track of the ROIs transferred, but this is a minor concern. I will use suggestion A and load all ROIs into ImageJ, than transfer all together to R and then rename them in R according to the list in ImageJs ROI manager.

Best regards,
Wagner
Reply | Threaded
Open this post in threaded view
|

Re: ImageJ selection to R headless

Bio7
In addition here is a small script which loads a single ROI from selected *.tiff files.
Simply create a *.bsh script and copy the source below.

You can easily customize this script to bring the data with R in the correct form or even
perform the R analysis in this script. Apropos a script can also call an R-script if you want to avoid to much quoting, see:
 
https://github.com/Bio7/Bio7/tree/master/Bio7_1.7_Examples/Bio7_Examples/R_Beanshell_Snippets/Execute_Source

Script:

/*
Loads a single ImageJ ROI's (selection) from  multiple selected *.tiff files and transfer the ccordinates to R!
Author: Marcel Austenfeld
*/
import ij.IJ;
import ij.ImagePlus;
import ij.process.ImageProcessor;
import ij.gui.Roi;


if (RServe.isAliveDialog()) {
        String[] files = Bio7Dialog.openMultipleFiles();//Open files from multiple selections(with Shift+Mouse-Click or Ctrl+Mouse-Click).
         if (files != null) {
                RConnection c = RServe.getConnection();
       
                Roi roi = null;
                for (int i = 0; i < files.length; i++) {
                        System.out.println(Bio7Dialog.getCurrentPath() + "/" + files[i]);
                        ImagePlus imp = IJ.openImage(Bio7Dialog.getCurrentPath()+ "/"+ files[i]);//Open  image data with the ImageJ API!

                        roi = imp.getRoi();
                        int[] roix = null;
                        int[] roiy = null;
                        if (roi != null) {
                                Polygon po = roi.getPolygon();
                                roix = po.xpoints;
                                roiy = po.ypoints;


                                try {
                                        c.assign("selectionx_" + i, roix);

                                        c.assign("selectiony_" + i, roiy);
                               
                                } catch (REngineException e) {
                                // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }

                        } else {
                        System.out.println("A ROI is not available!");
                        }

                }
        }
}