Friday, September 2, 2011

My first ImageJ macro/script

Once you are familiar with the various tools and menus of ImageJ, it's time to move to the dark side of the force and to start using the programming features of IJ via its macro/script. That's really useful specially if you have repetitive tasks and/or if you need precise data exploration.
Don't worry, learning a programming language is not so complicated and ...May the Force be with you...

1- Exemple: Drawing one horizontal line

Open the Recorder (Plugins > Macros > Record...), then create a 256x256 8-bit image entitled lines with a white background.  Choose the line tool in the toolbar (Tool #5), and draw  a horizontal line (Shift + left-button mouse) (I assume that your foreground color is black, otherwise, reset your foreground/background colors in the color dialog window).
In the Record window, the following three commands will be printed:

newImage("lines","8-bit White",256,256,1);

The four arguments of the function makeLine correspond to the XY-coordinates of the start  and  end points of the line, respectively. In my case, the horizontal line is drawn at Y=17 (of course, it can be different for you).

Click on the 'Create' button, a window entitled 'Macros.ijm' is created. This is your first script.Great!
Now to execute this script, go to Macros > Run Macro... (or use the shortcut Ctrl+R) and automatically your script is executed and a new window 'lines' is created with a horizontal line.
Don't forget to save your script in File  > Save.
Note: I suggest you to save this file with the extension ".ijm". It'll be easier to find all of your scripts.

2- Improvement

If you look at the ImageJ documentation in the Built-in Macro Functions section, there is another way to draw lines ... by using the drawLine function.
Try to modify your code and run again your script.

3- Go further

All you need to learn about the IJ programming scripting language is grouped in the resources section of ImageJ.
I'll post several tricks and tips about IJ scripting language, check this Table of Contents to follow their publication.

No comments:

Post a Comment