Tuesday, August 23, 2011

ImageJ script: the loop

One of the main purposes of a ImageJ macro/script is to perform repetitive tasks. In programming languages (and in IJ script), this can be done by using a specific iterative mechanism: the loop.


Imagine that we want to draw several horizontal lines in an image (Fig.1).
In a previous post [1], I described how to draw one horizontal line in a IJ script, the simplest (and naive) way to draw many lines is to repeat the function as much as you need:
drawLine(0,0,256,0);
drawLine(0,4,256,4);
drawLine(0,8,256,8);
drawLine(0,12,256,12);
drawLine(0,16,256,16);
.....
Note: We assume that: (i) we draw in an image of 256x256 with white background, (ii) the lines are colored in black, and (iii) are separated by a space of 3 pixels.

There is a better way to repeat tasks in a programming language: the loop. In ImageJ, there is three kind of loops...


Fig.1: Images with horizontal lines.

1- The loop 'while'

while (<condition>)
{
  do_something
}

While the <condition> returns TRUE, the lines of code between the two braces {...} (called a block) are executed iteratively. The loop stops when the condition returns FALSE.

For example, to print the first ten numbers, that gives:

1 // 1- Initialize variable
2 number=1;
3 while (number <=10)
4 {
  // 2- Do something
  print(number);
  // 3- Increment number
  number = number + 1;
9 }

There is three steps in this loop:
  1. Initialization of the variable - the 'start': number = 0
  2. The control condition : number <=10
  3. The increment: number = number + 1 the number variable is incremented by 1 at each cycle.

1-1 The start

TODO

1-2- The control condition
The comparison operators are:
Fig.2: Comparison operators


If the condition is correct, it returns the value TRUE otherwise FALSE.

1-3 The increment
In ImageJ (and other programming languages using the C syntax), there is shortcuts to increment a variable, for example:
number = number + 1 <=> number++
number = number + 3 <=> number += 3
number = number - 1 <=> number--
number = number - 3 <=> number -= 3

Note: Don't put <space> character between the two signs

2- The loop 'do ... while'

do
{
  do_something
} while (<condition>)


This loop works like the 'while' loop but the condition is done AFTER the execution of lines code.

3- The loop 'for'

The loop 'for' has a more compact syntax because, the start, condition and increment are put together in the same line just after the 'for' keyword. The main advantage of this loop is the fact that you can't forget the <increment> or <start>

for (<start> ; <condition> ; <increment>)
{
  do_something
}

4- Nested loop

A nested loop is an inner loop executed inside an outer loop.

for (y=0;y<8;y++)
{
  for (x=0;x<8;x++)
  {
      print(x,y);
  }
}

In the first pass of the outer loop, y = 0. Then, the inner loop (x-loop) works until completion from 0 to 7. When it has finished, the outer loop increments y (y=1) and the inner loop is triggered again for a new cycle. That leads to the numbers shown in the following table.

y 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 ... 7 7 7 7 7 7 7 7
x 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 ... 0 1 2 3 4 5 6 7

This kind of code is commonly used to extract the X-Y coordinates of an image (here a 8x8 image - cf: the chessboard script)

5- Trouble with a loop

There are common mistakes when using a loop.
5-1- The infinite loop
There is no exit in your loop and it cycles over and over.
Ex:
number=1;
while (number<=10)
{
  print(number);
}

5-2- The loop isn't executed

The initialization step isn't done or is wrong

5-3- The number of cycles is wrong
The control condition is wrong. This error is common when using an array or scanning an image because the first element is located at 0 (zero) and not 1.

5-4- Exercises

Try to debug these scripts ...

+++ IJ snippet +++
+++End of IJ snippet +++


6- Example

Go back to the horizontal lines, the following script, three different images are created with different styles of loop to compare the syntax.
The horizontal lines are drawn using a loop:
  • 'for' in lines 5 to 10
  • 'while' in lines 12 to 19.
  • 'do ... while' in lines 21 to 28.

+++ IJ snippet : drawing_horizontal_lines.ijm +++

+++ End of IJ snippet +++

No comments:

Post a Comment