<<

vvv

“Scripting” in

Santiago, 28/11/2016. [Summer School: Processing and Analysis of Fluorescence Microscopy Images]

Mauricio Cerda ICBM, Fac. Medicina Universidad de Chile [email protected] www.scian.cl/~mcerda vvv Outline

1. Context: image manipulation and processing 2. ImageJ as interaction tool 3. Macro building 4. Plug-in building 5. ImageJ vs FIJI?

2 vvv Context

• Software for digital imaging has been targeted at either manipulation or processing images. • Manipulation o artists, designers o Ex.: Photoshop, Corel, (paint) o Nice user interface and many available functions o Hard to extend • Processing o , engineers o Ex. Matlab, IDL, ImageMagick o Comprehensive and well-document software libraries o Poor user interface vvv Context: ImageJ key features

• ImageJ is a combination of image manipulation and image processing. • Key features: o Offers many interactive functions for image manipulation o A “simple” macro language to combine existing functions o Extended “easily” by writing software components or plug-ins in a real programming language (). vvv Context: history

• ImageJ was developed by Wayne Rasband at NIH (retired). • Substitute of NIH-Image, and Apple only.

Wayne Rasband (R) at 1st Imagej Conference in 2006. vvv Context: interactive tool

• Open images in JPG, GIF, PNG, DICOM, and common microscopy formats (olympus, zeiss, leica). • All process are applied to the current active window • Undo mechanism • ROI manipulation vvv Context: imagej macros

• Written in a simple ImageJ macro language (but support for , , python, ruby, , groovy). • Access to many ImageJ functionalities • Allows you to: o Automate tasks o Document what you did o Add toolbars/keyboard shortcuts vvv Context: imagej plugins

• Small java modules. • Access to many ImageJ functionalities • Two types (IJ1): o PlugIn, no required image to be open o PlugInFilter, current image is passed to the plugin. vvv Outline

1. Context: image manipulation and processing 2. ImageJ as interaction tool 3. Macro building 4. Plug-in building 5. ImageJ vs FIJI?

9 vvv

ImageJ as interative tool vvv Interactive ImageJ: aims

Interactive ImageJ aims • Open images in JPG, GIF, PNG, DICOM, and common microscopy formats (olympus, zeiss, leica). • Apply image processing algorithms to current window • Undo mechanism • ROI manipulation

Section outline • Basic pipeline • Advances pipeline (hands-on). vvv Interactive ImageJ: basic

Select experiment and discuss sampling rate with Biologist: • Open .lif file (leica), and select an experiment (FIJI only). • Change contrast to improve visualization. • Measure travelled distance between two frames. • Select 1 time use the 3D Viewer.

DEMO 1 vvv Interactive ImageJ: advanced

The problem: to estimate the number of infected cells with Trypanosoma cruzi. vvv Interactive ImageJ: advanced

The solution: i) segment cells, ii) segment parasites, iii) assign parasites to the closet cell, iv) define a “geometric cell”. vvv Interactive ImageJ: advanced

HANDS-ON 1 (Geometry Intro) vvv Interactive ImageJ : overview

• Limitations: o We saw that certain operations are hard to do by just clicking o Repetitive pipelines are time-consuming o We need to twist our algorithm to ImageJ interactive options.

• Remember that ImageJ has multiple ways to do the same operations, even this short practical session. • Some plugins are almost standard in domains like: Calcium dynamics, Neurons structure, Tracking. vvv Interactive ImageJ: references

o http://rsb.info.nih.gov/ij/docs/guide o http://cmci.embl.de/mainpages/courses o Computational Methods for Analysis of Dynamic Events In Cell Migration. Castañeda V, Cerda M, et al. Current Molecular Medicine, 14(2), 291-307. o Trypanosoma cruzi Infectivity Assessment in 'in vitro' Culture Systems by Automated Cell Counting. Limpi A, Castillo C, Cerda M, et al Acta Tropica, 143 : 47 – 50. vvv

ImageJ MACROS vvv Macros: aim

Macros aim • Document what you did, for scientific papers • Automate taks to decrease workloads, less clicking • Add toolbars/keyboard shorcuts, for sharing

Section outline • Hello World macro example • Image processing recording example (to document) • Macro Language • A batch procedure example (to automate) • A toolbar example (to share) • An exercice (hands-on). vvv Macros: hello world!

• Create a new macro (FIJI). vvv Macros: hello world!

• Write a macro (FIJI). vvv Macros: recording

• Hello world was nice but boring. We can record a simple procedure. • A segmentation procedure for neurons slides from confocal imaging. A semi-automatic segmentation was applied to the neurons stacks. The automatic part was implemented in FIJI [Schindelin et al 2012] sequentially using: a normalization step to reduce variability among samples (using the full histogram), applying an anisotropic diffusion filter (parameters 0.1 and 0.99) to smooth neurons processes without blurring edges, highlighting elongated areas associated with processes with the frangi filter (default parameters), and finally with a manual threshold to binarize the stack. Ampuero, Cerda et al 2016 [in preparation].

DEMO 2 (result in prefiltrado.ijm) vvv Macros: to document

The segmentation procedure for neurons slides from confocal imaging results is:

(Anisotropic Diffusion and Frangi are plugins in the process sub-menu) vvv Macros: language

• Not only to record operations, we can also automatize complex procedures. • We need to understand first the macro language to use: variables, comments, conditions, loops and functions. vvv Macros: variables

• Placeholder for a changing object. • It has a name an a value. • To execute the same but for different images/parameters. • [Implicit data type]

• Numeric variable assignation: factor = 1024;

assignation end of instruction

• String variables: message = "Hello Montevideo!" ;

for strings vvv Macros: variables

• Arithmetic expressions:

i = 0; filenameindex = 2*i;

• Strings operations:

i = 0; filenameindex = 2*i; filename = "myimage_" + filenameindex + ".tif";

vvv Macros: comments

• 2 type of comments:

//gaussian blur sigma parameter sigma = 3.0; //print( sigma ); parameters = "radius=" + sigma ; /* Call to an ImageJ level function Gaussian Blur operation Sigma is the parameter to execute a gaussian blur. */ run("Gaussian Blur...", parameters); vvv Macros: conditions

• Execute a code block only when a conditions is met

if ( is("binary") ){ write("The current image is binary" ); }else { write("The current image is NOT binary" );

}

• Multiple conditions with && (AND) and || (OR)

if ( is("binary") || is("grayscale") ){ write("The current image is binary or grayscale"); }else{ write("The current image is NOT binary or grayscale"); } vvv Macros: loops

• A Loop repeats instructions several times. • For keyword followed by 3 statements and 1 code block.

Counter initialization Loop condition Counter increment

for ( i = 0; i < 10; i++){ filenameindex = 2*i; Code block filename = "myimage_" + filenameindex + ".tif"; print( filename ); }

• What does this code? vvv Macros: functions

• For a recurring task, you can define your own functions.

function closeImageByTitle( title ){ selectWindow( title); close(); }

• Note that title is another variable created when the function is called.

closeImageByTitle( "My pretty new image"); vvv Macros: installing

• To install keyboar shortcuts or tool icons, you need to wrap your code code in macro blocks.

macro "My first macro" { write("Hello montevideo"); } vvv Macros: installing

• Tools icons are created by using the special string “Action Tool” in the name of the macro.

// A click on the empty rectangle will have the same // effect as {{bc | File | Save As | Jpeg...}}

macro "Save As JPEG Action Tool - C000R11ee" { saveAs("Jpeg"); }

• Keyboard shortcuts use brackets.

macro "Save As JPEG... [j]" { quality = call("ij.plugin.JpegWriter.getQuality "); quality = getNumber("JPEG quality (0-100):", quality); run("Input/Output...", "="+quality); saveAs("Jpeg"); } vvv Macros: batch processing

• A typical problem is to apply a set of operations to a all files in a folder.

DEMO 3 vvv macros: hands-on

HANDS-ON 2

1.- Build a macro to segment nuclei in the trypanosoma cruzi problem.

2.- [Optional] Starting from 2 segmentations (tif files), nuclei and parasites, build a macro to directly estimate infection rate. vvv Macros: overview

• Limitations: o If you need to process large images/stacks it may be slow. Up to 40x slower than a plug-in! o Macro is not efficient in real-time interactive input. o Macro is tightly coupled to graphic windows. Hard to process without showing results. vvv Macros: overview

• A nature protocol article macro!

vvv Macros: references

o http://rsb.info.nih.gov/ij/developer o http://fiji.sc/Introduction_into_Macro_Programming o http://cmci.embl.de/mainpages/courses o , An Algorithmic Introduction using Java. Burger W and Burge, MJ. Springer, 1st edition, 2010. o Computational Methods for Analysis of Dynamic Events In Cell Migration. Castañeda V, Cerda M, et al. Current Molecular Medicine, 14(2), 291-307.