Category Archives: R

Cluster Multiple Images with ImageJ and R

30.08.2012

With Bio7 1.6 it is possible to send multiple images from ImageJ to R without the need to open them in the Graphical User Interface of ImageJ for speed improvements. With a simple script written in Java, Groovy or BeanShell a new Bio7 API command can be used (see below) to transfer images and e.g. to cluster image data with the available packages in R.

ImageMethods.imageToR(“currentImage”, true, 3, imp);  //The first argument is the transfer name of the image, the second argument creates a matrix if true (for datatype double and RGB transfer), the third argument determines the datatype for the transfer (byte, integer, double or RGB), the last argument expects an ImageJ image (can be opened with the ImageJ API) or if null transfers the current opened image in the Bio7 ImageJ panel (API).

The R commands itself can be embedded easily with the available Bio7 API or called from an external R script with the API. Here is a short video which uses the API to cluster selected images with BeanShell and creates a report of the cluster areas in the spreadsheet of Bio7:

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

The script for the example can be found here!

Apropos: One reason that Beanshell and Groovy are used as default scripting languages in Bio7 is that they also accept (more or less) the Java syntax by default. Beanshell has the greatest Java compatibility. Some minor Java differences to Groovy are described here. Therefore it is very easy for a Java developer (or a C++ developer) at the beginning to use these scripting languages and if interested profit from the scripting syntax sugar these languages offer. In addition Java files can easily converted to scripting files and vice versa (e.g. for speed improvements).

Bio7 1.6 for Windows and Linux released!

01.08.2012

Finally i released a new version of Bio7 with many improvements and new features. Updated tutorials are available, too. The new Bio7 1.6 release can be downloaded here. Please also download the examples *.zip file from the sourceforge website which contains new examples for Bio7 1.6 (e.g. an example to cluster an image folder with ImageJ and R).

New Features:

 Bio7 General

- Bio7 1.6 is now based on Eclipse 3.8.0.

- The option “Always run in background”  is now selected by default.

- Integrated new Bio7 grid file format to import or export formatted spreadsheet data (for images and fonts, etc.).

- Updated the integrated Java Libraries.

- Added the application API from “Processing“  (a few programming examples are available in the examples for Bio7 1.6).

- The main “Processing” panel can be embedded in the “Custom” view of Bio7 for integration (see screenshot below)).

- Updated WorldWind to the latest version.

- Integrated more syntax coloring options for the editors of Bio7.

- Now fonts for selected syntax types can be adjusted in the Bio7 editors, too.

- Added drag and drop text support for the different Bio7 editors.

- Double-Click in brackets selects the code in the brackets of the editors.

- Added some default Update sites for Bio7 (e.g. EGit).

- Updated links in the Bio7 browser.

- Updated the Bio7 API description!

- Refactored several plugins.

- Updated and improved many Bio7 API methods.

- Created new Bio7 API methods for the Bio7 grid.

- Added update support for the Bio7 plugins with the integrated p2 Update Manager (see screenshot below).

- Fixed some bugs.

- Reorganized the file wizard of Bio7 (see screenshot below).

Java Compiler

- Added Java 7 support for the Java compiler and editor.

- Bio7 now uses the standard Java compiler to compile Java source (with the support of the Janino API).

- Improved the error messages for a Java compilation (if errors occur).

- Compilation errors are now marked within the Java editor (selectable – see screenshot and video tutorial below).

- In the Outline view selected members of a Java class are now shown (created from the “Abstact Syntax Tree” (AST)).

Video Tutorial: http://bio7.org/flash/createjava.htm

- Now a complete Java classes can be compiled with the Java compiler dynamically (context menu Navigator view). If a main method is available it will be invoked automatically after compilation (see screenshot below).

- If a class extends the “Model” class it will be compiled like the default Java classbody (ecomain method can be invoked!).

- New option to switch between classbody AST parsing and class AST parsing (for the display of the Outline view) .

- New Wizards are available for Java files and ImageJ files, etc.

- New rearranged Wizard Pages are available for an improved overview.

R Specific

- Update to R 2.15.1.

- Update to Rserve 0.6.8.

- Added text drag and drop support for the R editor.

- Added more source formatting options for the editor.

- Added a display for the loaded packages (use the “Update” method in the R-Shell view to see the current loaded packages).

- Added a simple Sweave support for Bio7 (installation of e.g. MikeTex necessary!  The actions for Sweave are available in the context menu of the  Navigator view if a Sweave or Latex file is selected!)

R + ImageJ

- Added a new image transfer option to transfer RGB image values to R in one shot (as raw vectors or as a  R, G, B integer matrix – see screenshot below).

- Added a new transfer mode to transfer selected pixels (all pixels within an area selection). If the ROI Manager in ImageJ is active all ROI image data (selected pixels)  for all opened images (every roi is applied to all opened images regardless in which image the ROI was created!) is transferred to R (also recognizes now the transfer type adjustments!). For e.g. supervised classification purposes a signature is added to each selection matrix, too. If the ROI manager is not active the standard transfer method is invoked (see screenshots below – for an explanation please follow the video tutorial link below the images).

Video tutorial: http://bio7.org/flash/roiselections.htm

- Added a new API to transfer images to R with ImageJ in a windowless mode for the purpose of batch computing of images (see examples).

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

- Added a new API to transfer R image vector data as an ImageJ float image in place e.g. for gridded simulations in R (and in silent mode without printing evaluation messages).

ImageJ

- Updated the integrated ImageJ application to the latest ImageJ version 1.47a.

- ImageJ Plugins can now be compiled dynamically with the integrated dynamic Java compiler. The action is available in the context menu of the Navigator view if a Java file is selected. The action compiles the active Java file in  the editor. For this action it is not necessary anymore to put the class file in the plugins directory of ImageJ. Classfiles and *.jar archives can be created with available actions in the Navigator view, too.

3d view

- Added speed adjustments for the 3d view in z-direction.

- Added a “Reset View” button.

Bio7 examples

- Added a few examples for the new image API methods.

- Added a Swarm example (for the OpenGL 3d view).

- Added Java Processing examples.

Installation

System requirements:

It is recommended that your computer should have at least 1024 mb ram and a 1 ghz processor.
A 3d Graphics Card which is OpenGL enabled (only necessary for the “Spatial” view and the embedded “WorldWind” view.
To use the OpenOffice (LibreOffice) feature of Bio7 an installation of OpenOffice (LibreOffice) >= 2.0 is
required. The path to OpenOffice will be automatically fetched by Bio7 from the registry (Windows) or can be adjusted in the Bio7 preferences (Windows, Linux, MacOSX) to the default path.
For the MacOSX version a JRE >=1.6.0 has to be installed if not available on the OS by default (For Windows and Linux a JRE is bundled in the Bio7 installation package).

Installation Windows, MacOSX and Linux:

The installation of Bio7 is similar to the installation of the Eclipse environment. Simply decompress the downloaded *.zip file in a preferred location on your file system.
After decompressing with a standard zip-tool (like WinZip, Win Rar) the typical file-structure of an Eclipse based application will be created.
To start the application simply double click on the Bio7.exe (Windows) or Bio7 (Linux, MacOSX) file.

R Windows specific:

Because Windows Vista, Windows 7, etc. is very sensitive to user rights etc. the package installation was reorganized in Bio7 1.4 for installable add-on packages inside of Bio7. The default add-on package location in a Bio7 release is now:

“Bio7\plugins\Bundled_R_x.xx.xx\R\site-library”

At this location inside of Bio7 the packages will be installed by default (if not changed!).

Windows only (if needed)!:

To write the R package location path to the Windows registry please start Bio7 with the enabled
option “Install path at startup” to set the registry path to the bundled R location. By default this option is disabled not to harm any default user installation of R.
In Bio7 x.x please install the R package rgdal which is needed for some spatial import and export scripts.
Please note that the import scripts for spatial data can have problems with special text encoded filenames. If an rgdal import script produces an error the Rserver will probably die and you have to restart Rserve.

R Linux specific:

To use R from Bio7 please install R with a package manager.
Also the installation of the Rserve library is required. This can be easily done inside R by typing:

> install.packages(“Rserve”, dependencies=TRUE)

in the R console.

After the installation of R the path to the R (if not using the default path) application has to be
adjusted inside of Bio7 (“Preferences->Preferences Bio7″). In addition the path to the (add-on) packages
install location has to be adjusted, too (“Preferences->Preferences Bio7->RServe Preferences”).
Please also set the user rights for the folder. This is sometimes necessary if you would like to install packages with the Bio7 interface and you don’t have the user rights. (Start Rserve with a shell for an analysis if problems occur!  ->  R->Preferences->Rserve preferences->Start with shell)
Since Bio7 1.4 default paths are set which are usually correct for a Linux distribution.
If Rserve is running you can then easily install the package rgdal with the Bio7 interface which is needed
for the import/export scripts (you can also install it beforehand with R!).

Linux (only 32bit): Scanner with Sane:

If the Sane interface is not running by default please set a symbolic link (the shared library has to be found!):
ln -s /usr/lib/libsane.so.1 /usr/lib/libsane.so

R MacOSX specific

To use R from Bio7 please install the latest R. After the installation of R start R and install the Rserve package:

> install.packages(“Rserve”, dependencies=TRUE)

The default installation paths of Bio7 are pointing to a standard installation of R but can be adjusted in the
preferences of Bio7 (“Preferences->Preferences Bio7″). In addition the path to the (add-on) packages install location can be adjusted, too if necessary (“Preferences->Preferences Bio7->RServe Preferences”). If Rserve is running you can then easily install the package rgdal with the Bio7 interface which is needed for the import/export rgdal scripts (you can also install them beforehand with R!).

Examples

On the Bio7 SourceForge website you can download examples for the Bio7 application (recommended!). In addition the examples can
be accessed on github. To install the Bio7 examples please import the examples from the Examples.zip file.

“File->Import->Existing Projects into Workspace”.

Select the archive file “Examples.zip” and the two projects “Bio7 Examples” and “Ecological Modelling”. Press “Finish” to import them into Bio7 (they will be imported to the workspace location).

Increased Java memory

Windows and Linux

For an increased Java heap space open the Bio7.ini file in the install directory of Bio7. In the file you can change the default memory settings e.g. the initial heap size -Xms and the maximum heap space -Xmx.

MacOSX

For an increased Java heap space open the Bio7 package (context menu if you click on the icon) then go to Contents->MacOS and open the Bio7.ini file with a texteditor. In the file you can change the default memory settings e.g. the initial heap size -Xms and the maximum heap space -Xmx.

Landscape Metrics with R, SDMTools, ImageJ and Bio7

01.02.2012

Landscape metrics were developed to analyze spatial patterns of landscapes (e.g. composition and spatial arrangement). In R it is possible to calculate these metrics with the “SDMTools” package. Bio7 offers an easy to use interface to R and ImageJ and can use these tools to simplify a workflow to analyze image data (e.g. vegetation data) with landscape metrics. A typical workflow would include to cluster available image data into different classes and then analyze the data with the available metrics.

In the video below a simple artificial example is given of the workflow in Bio7 with the help of ImageJ, R and the “SDMTools” package:

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

Beside this possibility it is also possible to calculate a subset of landscape metrics with a Java API available in Bio7 for simulation purposes.

Apropos:

Landscape metrics algorithms are quite similar to some algorithms which are default available in ImageJ and these measurements can maybe complement an analysis of landscape structures, see:

http://rsbweb.nih.gov/ij/docs/menus/analyze.html
http://bio7.org/flash/clusterParticles.htm

Be aware that it is of importance to define the neighborhood rule (e.g. 4- neighbor rule or 8-neighbor rule) beside several other aspects which have to be considered. For an overview about landscape analysis and landscape ecology follow the link to a literature database at the end of this post.

Related to this article:

Creating fractal categorical maps and images with Bio7
http://www.uni-bielefeld.de/biologie/Oekosystembiologie/bio7app/flashtut/fractalrandom.htm

Supervised classification of images with Bio7 and R
http://www.uni-bielefeld.de/biologie/Oekosystembiologie/bio7app/flashtut/supervisedimages.htm

SDMTools package
http://cran.r-project.org/web/packages/SDMTools/index.html

Literature (see Landscape Analysis – if you know good papers or books let me know!)
https://www.zotero.org/groups/ecological_modelling/items

Image Data from ImageJ to R and Vice Versa

In recent years many R packages have been developed to enable image analysis in R. As an alternative the combination of R with a powerful image analysis software like ImageJ offers many advanced image analysis interfaces and algorithms not yet available in R. Bio7 integrates both applications in a Rich Client Plattform based on Eclipse interfaced and connected with the RServe application (which offers an API to connect Java and R). In addition Bio7 offers an easy to use interface for the bidirectional transfer of image data from ImageJ to R respectively R to ImageJ. The transfer of selected image data regions, selection point(s) coodinates (single and multiple) and transfer of particle measurements data to R is supported with special actions in Bio7, too. Here is a short overview:

Transfer of image data to and from R

It is quite simple to transfer images from ImageJ to R with the interface Bio7 offers. The image data itself can be transferred as (datatype) doubles, integers or bytes which are supported by default in R. The data is transferred as vector data in R (except double data which can also be transferred as a matrix). In addition the image size (as variables) is transferred, too (this variables for size are automatically selected if an image is transferred from R to ImageJ).
Vice versa the default data types can be used to create ImageJ supported image types which are:
Colour images (R,G,B), Float 32-bit images, Short 16-bit images, Greyscale 8-bit images.

The options to select different datatypes and image types enables an efficient and appropriate transfer of image data to the R workspace or to ImageJ from the R workspace.
R,G,B images, for example, can efficiently be transferred as byte data after a split of the image R,G,B data in its intensity (greyscale) components (in ImageJ the command can be found in the main menu: Image->Colour->Split Channels).

Note: If several opened images should be transferred in one shot to the R workspace the “Cluster Pic” or “PCA” action can be used with the option “Only Transfer” to transfer all selected image layers (see video below).

Video:
http://bio7.org/flash/imageDataTransfer.htm

http://www.uni-bielefeld.de/biologie/Oekosystembiologie/bio7app/flashtut/imagetnew.htm (older!).

Transfer of selected image data (pixels)

With the Bio7 interface it is also possible to transfer values from selected pixels. Several selection types are available in ImageJ and will be transferred as vector data to R. With this action it is e.g. possible to do regional statistics of image data in R. If several images are opened in the tabbed interface the selected region for all opened layers can be transferred, too in one shot e.g. to analyze changes in time or space (see video below).

Video:

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

Transfer of selection coordinates

In addition the transfer of selection coordinates of ImageJ is supported in two different ways: single selections as vectors and multiple selections (recorded in the “ROI-Manager” of ImageJ – ROI=Region Of Interest) as 2d-vectors. This features can be used e.g. to analyze shape data, point-pattern data, etc.
Please be aware that in Java the coordinate display is different from the coordinate display in R (the 0,0 coordinates for x and y in Java are in the upper-left!).

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

Transfer of Particle Analysis data

In ImageJ measurements of Particles are supported in an easy way. Particles could be for example cells, animals, plants etc. which are recorded in an image. The particles can be analyzed automatically with the Analyze->Analyze Particles action in the ImageJ menu. Several geometrical measurements are supported e.g. Area, Diameter, Centroids, Bounding rectangle, Ferret’s diameter etc.
The results of such measurements can directly be transferred to the R workspace with a special action available in the Bio7 GUI interface (in the detached Image-Methods view). The measurements can be analyzed easily with different packages in R.
With the Spatstat package for example the centroid values of the particle analysis can be used to calculate nearest neighbour distances, point-pattern statistics etc. The geometrical measurements can be used for example to split the data in different classes (see video below).

Video:

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

Dynamic transfer of R data to ImageJ

Dynamic R data (or plot data) can be transferred (and recorded) to ImageJ with the available R API and the Java language (or BeanShell and Groovy) with Bio7. Created R data from called R scripts or created within Java e.g. can be tranferred directly into an ImageJ image (as a kind of display) or ImageJ stack (see video below). Even the recording of timed R plots in ImageJ can be realized (as a stack).

Video:
http://bio7.org/flash/imagejDisplayRdata.htm

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

At all the combination of R and ImageJ offers powerful possibilities to combine image analysis and statistical analysis with an easy to use Graphical User Interface offered by Bio7. Bio7 is an OpenSource Software and is available for all popular platforms (Windows 32, 64-bit, Linux 32, 64-bit and MacOSX 64-bit) at:

http://bio7.org

Shared Ecological Modelling References

05.06.2011

Today i started to create a list of books and articles about ecological modelling. In this list you will not only find general books about modelling but also books about spatial analysis, image analysis and other (in my opinion) important techniques useful in the context of ecological modelling. For the collection i use “Zotero” which is an excellent open source reference management software running in the firefox browser. In addition with the Zotero network i can share my own collections of books, articles, links etc.

I created a group “ecological modelling” in which you can find a first personal collection of textbooks and articles which i will extend from time to time and is hopefully useful for people interested in this topic.
In one subgroup you will find references of R-literature

The group can be found here:

http://www.zotero.org/groups/ecological_modelling

If you join the group you can synchronize your local Zotero and a new generated literature list will be added to your “Group Libraries” which can simply be exported in different styles (e.g. to  the clipboard).
I you have some suggestions for books or articles (or links) for a more profund list don’t hesitate to contact me.