Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

56 lines (48 sloc) 2.783 kB
This example opens several image files (if selected) in ImageJ without to displaying them in the ImageJ-Canvas view.
The script transfers, clusters and summarizes the RGB pixel values of the images in R
Then it transfers the summarized results (pixel counts - areas) to the Bio7 Grid. The default images are scaled
and are painted in a grid cell, too!
The new Bio7 1.6 API method to transfer R, G, B values as a matrix is used for this workflow!
Note: This example demonstrates how to apply advanced RScripts to e.g. a folder of images and then
generate automatically a report of the results in the Bio7 spreadsheet (which can be saved and
restored as a file with the new Bio7 import/export format which saves images and formattings of
the sheet, too).
import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import org.eclipse.nebula.widgets.grid.Grid;
import ij.process.ImageProcessor;
import java.awt.image.BufferedImage;
if (RServe.isAliveDialog()) {
Bio7Grid.createSheet(0, 0, "Measurements");//Create a new sheet in the Table view!
for (int i = 0; i < 6; i++) {
Bio7Grid.createColumn(1,70, "State: " + (i + 1));//Create new columns!
Bio7Grid.createColumn(1,70, "Name");//Create new column!
Bio7Grid.createColumn(1,160, "Image");//Create new column!
String[] files = Bio7Dialog.openMultipleFiles();//Open files from multiple selections(with Shift+Mouse-Click or Ctrl+Mouse-Click).
RConnection c = RServe.getConnection();
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!
ImageProcessor ip = imp.getProcessor().resize(160, 120);//Resize the image data!
ImagePlus impResized=new ImagePlus("pic", ip);//Create a new image from the image data!
BufferedImage buff=impResized.getBufferedImage();//Create a BufferedImage from the resized image!
//Transfer the R, G, B image (ImagePlus required as argument!) to R in one shot with the new Bio7 1.6 API method!
ImageMethods.imageToR("currentImage", true, 3, imp);
c.eval("cl<-kmeans(currentImage,6)"); //Cluster the image data with the kmeans algorithm in R!
RServe.print("table(cl$cluster)");//Create a table in R from the results!
String[] values = (String[]) c.eval("as.character(table(cl$cluster))").asStrings();//Get the result values as a String array!
/*Put the values and the scaled image in the sheet!*/
for (int u = 0; u < values.length; u++) {
Bio7Grid.setValue(i, u, values[u]);
Bio7Grid.setValue(i, 6, imp.getTitle());
Jump to Line
Something went wrong with that request. Please try again.