This is an old revision of the document!
Table of Contents
Jython <- (ImageJ, R)
Analysis using several tools is a powerful way to deal with complex Image data. We often do this, and here is one small example of taking advantages of ImageJ and R at the same time.
ImageJ classes could be accessed by Jython, a python based java interface. R has rJava (JRI, Java - R Interface). For this reason, one way of using ImageJ functions and R functions in an integrated environment is to use these resources from Jython.
Do everything from Java is one way, but python-scripting environment is light enough to test many things. After these testings are done, then a serious Java code could be written.
Here is a trial in win32. All commands will be done from command line. It should basically be similar in other environment.
Setting Up Environment.
You should have done:
- rJava package should be installed in R.
- jython.jar should be somewhere locally.
1. set CLASSPATH
Following three jars should be in your classpath.
- JRI.jar
- REngine.jar
- JRIEngine.jar
- ij.jar
… so in command line
set CLASSPATH=%CLASSPATH%;C:\Program Files\R\R-2.12.0\library\rJava\jri\JRI.jar;C:\Program Files\R\R-2.12.0\library\rJava\jri\REngine.jar;C:\Program Files\R\R-2.12.0\library\rJava\jri\JRIEngine.jar;C:\ImageJ2\ij.jar
2. set PATH
R native libraries should be linked. Then the path should be added with bin, jri, R.dll and JVM.dll:
set PATH=%PATH%;C:\Program Files\R\R-2.12.0\bin;C:\Program Files\R\R-2.12.0\library\rJava\jri;C:\Program Files\R\R-2.12.0\bin\i386;C:\Program Files\Java\jre6\bin\client
Example Scripting
C:\>java org.python.util.jython Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54) [Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_22 Type "help", "copyright", "credits" or "license" for more information. #importing image using ImageJ >>> import ij.IJ >>> import org.rosuda.JRI.REXP >>> import org.rosuda.JRI.Rengine as Rengine >>> import ij.IJ as IJ >>> imp = IJ.openImage("http://rsb.info.nih.gov/ij/images/blobs.gif") >>> imp.show() >>> flA = imp.getProcessor().getFloatArray() >>> intA = imp.getProcessor().getIntArray() >>> intA3=intA[3] >>> histA = imp.getProcessor().getHistogram() # going into R, first instantiate >>> engine = Rengine(['--no-save'], False, None) >>> engine.assign("histR", histA) >>> engine.eval("pixint <- {0:255}") >>> engine.eval("histform <- data.frame(pixint, histR)") >>> engine.eval("plot(histform$pixint, histform$histR)") #.. will plot the graph but one cannot close it... >>> engine.eval("graphics.off()") #will close all the plots.