Friday, September 23, 2011

Test image: square wave



To try new features of ImageJ or debug a macro/script, there is always a need of test images. Here is a simple script to create one of them...

The square wave corresponds in 2D to an image of vertical strips alternating black and white colors as shown in Fig.1 (and resembling the artwork of Daniel Buren [Wikipedia]).
Fig. 1: Vertical black and white strips

Why is it called a square wave? Because, if you calculate a horizontal plot profile, you get a curve with steps.
Fig.2: Plot computed from a horizontal selection line (Tool #5)


As described in Wikipedia, there are many ways to draw such an image.

Algorithm #1

For each X-coordinate, if the division of x by the strip's width is even (or odd), the pixel value is assigned a value of 1.0 otherwise -1.0.

if (floor(x/w/f/2)%2==0) 
  v=1.0; 
else 
  v=-1.0;

In the following script, a small dialog box asks for the frequency (corresponding to the number of pairs (white and black strips, by default 4) and the image is created using two functions:
- newImage(...) creates a 256x256 32-bit image.
- run("Macro...",...) available in Process > Math > Macro...  calculates the pixel values.

+++ IJ snippet +++ +++ end of IJ snippet +++

Algorithm #2

Another algorithm (described in Wikipedia) consists of calculating the sign of the sine function.

sign(sint(t))

In ImageJ, there is no sign function, thus, I use a division of the sine function by its absolute value, the formula becomes...

sin(t)/abs(sin(t))

... and the script to draw this image is here.
+++ IJ snippet +++ +++ end of IJ snippet +++

No comments:

Post a Comment