imagej / fiji workshop 2012 - unibas.ch · imagej / fiji workshop 2012 part ii – macro...

21
Niko Ehrenfeuchter Imaging Core Facility ImageJ / FiJi Workshop 2012 Part II – Macro Programming

Upload: lamquynh

Post on 28-Apr-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core Facility

ImageJ / FiJiWorkshop 2012

Part II – Macro Programming

Page 2: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop2

Outline

Part II – Macro Programming

– Motivation– Documentation & Resources– Editors– Syntax– Macro Recorder– User Interaction– Installing Macros

Page 3: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop3

Why Macros?

Macros can be used to

– automate repetitive tasks– document what you did– share common procedures– provide consistency in your analysis– add tools to the toolbar– add keyboard shortcuts

A Macro is a program that automates a series of ImageJ commands.

→ ImageJ has a dedicated programming language for this.

Page 4: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop4

Resources

ImageJ Macro Tutorialhttp://rsbweb.nih.gov/ij/developer/macro/macros.html

Reference Documentationhttp://imagej.nih.gov/ij/developer/macro/functions.html

FiJi Macro Tutorialhttp://fiji.sc/wiki/index.php/Introduction_into_Macro_Programming

Textbook “Macro Programming in ImageJ” by K.Miura (recommended!)http://cmci.embl.de/documents/ijcourses

ImageJ toolsetshttp://rsbweb.nih.gov/ij/macros/toolsets/

Page 5: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop5

The Macro Editor

Select File > New > Script in the FiJi main menu, this will open the FiJi script editor:

– Syntax highlighting– Templates– Output console

Page 6: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop6

Getting Started

The obligatory “Hello World” code:

macro "hello" {print("Hello World!");

}

Running this opens a new window “Log” showing the message:

→ print() is useful for debugging

Page 7: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop7

Variables & Strings

The most important concept: a named piece of memory!– Name and value– Value can be numeric or text– Can be used in arithmetic expressions or string combinations–

x = 2;y = 3;result = x * x + y + y;

name = "Bob";msg = "Those days are over, " + name;print(msg);

Page 8: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop8

Variables & Strings

Self-references in expressions:

amount = amount * 2;counter = counter + 1;

counter++;

Short-hand for increment:

Page 9: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop9

Parameter input by user

getNumber() can be used to request a numerical input by the user:

macro "input_print_out_calc" {a = getNumber("a?", 10);b = getNumber("b?", 5);c = a*b;print("\\Clear");print(c);

}

Note: if no number is entered, no assignment will take place.

a = getString("b?", 5);getString() for text:

Page 10: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop10

Running ImageJ commands

Crucial element for macros: run ImageJ commands– Almost all commands can be run from macros– A few Plugins are not macro-ready

Most convenient way to find the corresponding macro commands is to use the built-in Macro Recorder:

Page 11: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop11

Batch processing

To apply a macro or multiple macro commands to many files, you could add file-accessing and looping commands or use the Batch Process tool.

Process > Batch > Macro

Select folders for in- and output, paste your commands in the text window and hit “Process”.

Page 12: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop12

Looping

Loops are required to iterate a certain part of our macro.

macro "loop1" { msg = getString("message to loop?", "default"); for(i=0; i<5; i+=1) { print(i + ": " + msg); }}

Page 13: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop13

Stack-processing with loops

One very powerful application of loops is stack processing since many ImageJ commands work just on a single slice. To work on all slices the “nSlices” and “setSlice” commands are used:

macro "Measure Avg Intensity Stack" { frames=nSlices; run("Set Measurements...", " mean redirect=None decimal=4"); run("Clear Results"); for(i=0; i<frames; i++) { currentslice=i+1; setSlice(currentslice); run("Measure"); }}

Page 14: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop14

Conditional statements

If–else statements can be used to run a certain part only if one or more conditions are met:

num = getNumber("Number:", 3);if (num == 3) {

print(num + ": default value");} else {

print(num + ": value changed");}

Note the “==” in the condition (a comparison operator), this is different from a single “=” (assignment). Other comparison operators are:

< , <= , > , >= , !=

Page 15: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop15

User-defined functions

When macros become more sophisticated, it is very likely that some parts will appear multiple times. These parts should be placed in separate functions to avoid redundancy and improve readability:

function CheckStack() { if (nSlices==1) { exit("Image is not a stack"); }} function CheckThreshold() { getThreshold(lower, upper); if ((lower==-1) && (upper==-1)) { exit("Must be thresholded"); } }

macro “threshold check” { CheckStack(); CheckThreshold(); getThreshold(lower, upper);}

Page 16: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop16

Return values

If your function does some calculation, you need a way to hand back the result to the code that called the function:

function ReturnAdd(n, m) { p = n + m; return p;}

macro “function add” { a = 1; b = 2; result = ReturnAdd(a, b); print(result);}

Page 17: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop17

Advanced dialogues

If multiple values and settings need to be requested from the user, it is easier to use a “Dialog” for this task:

Dialog.create("input dialog");Dialog.addMessage("my description");Dialog.addNumber("value 1:", 0);Dialog.addNumber("value 2:", 0);Dialog.addCheckbox("really?", true);Dialog.show;

val1 = Dialog.getNumber();val2 = Dialog.getNumber();really = Dialog.getCheckbox();

Page 18: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop18

Saving to files

The macro language provides a way to automatically save data (images, movies, measurements, text) to files.

If the second argument to saveAs() is omitted, the user will be presented with a dialog to choose the location.

setThreshold(100, 255);run("Set Measurements...", "area mean centroid circularity slice limit redirect=None decimal=2");run("Analyze Particles...", "size=10-Infinity circularity =0.50-1.00 show=Outlines display exclude clear stack");

my_dir = "D:\\test_results\\"img_title = getTitle();dest_filename = img_title+"_measure.csv";fullpath = my_dir + dest_filename;

saveAs("Measurements", fullpath);

Page 19: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop19

Batch-processing many files

As a fully automated alternative to the previously shown batch processing, this functionality can be directly built into a macro.

dir = getDirectory("Choose a Directory ");list = getFileList(dir);

for(i = 0; i < list.length; i++) { print(list[i]);}

open(image_filename);img_ID = getImageID();img_title = getTitle();//do somethingsaveAs("tiff", new_filename);

Page 20: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop20

Installing Macros

Macros are recognized by ImageJ from the following filename suffixes:– *.IJM (preferred)– *.TXT (deprecated)

Code files with the txt-suffix need to have an underscore “_” in their name, otherwise ImageJ ignores them.

The macro code files need to be placed in a sub-directory inside the ImageJ base directory:

– macros– plugins (preferred)

Page 21: ImageJ / FiJi Workshop 2012 - unibas.ch · ImageJ / FiJi Workshop 2012 Part II – Macro Programming. ... → ImageJ has a dedicated programming language for ... The macro language

Niko EhrenfeuchterImaging Core FacilityImageJ / FiJi workshop21

Hands-On sessionMacro programming