Drawing lines from array on image using ImageJ

Hi there! First post here. I am trying to draw a set of lines onto an image in ImageJ. I have tried looking for previous posts and there are some suggestions how to do this but I can’t seem to get them working. The code I have been trying to get to work so far looks like this:

v = newArray(10,20,30,40,50);

 for (i=0; i<lengthOf(v); i++) {
	run("Specify...", "width=1 height=500 x=v[i] y=85");
setForegroundColor(255, 0, 0);
run("Fill", "slice");
 	
 }

I was hoping to just start with an array and then iterate through it with a loop drawing a red line onto an image each time. The error I am getting is as follows:

Error: Numeric value expected in run() function

Dialog box title: “Specify”
Key: “x”
Value or variable name: “v[i]” in line 6:
run ( “Specify…” , “width=1 height=500 x=v[i] y=85” <)> ;

If anyone could offer any advice or help that would be great!

Thanks for your time.

Andy

Hi welcome to image.sc,

the lengthOf() function you are using takes only a string.
You need to call array.length to get the length of an array.
Have a look at the array functions: https://imagej.nih.gov/ij/macros/examples/ArrayFunctions.txt

Then you have a problem in line 4.
You want to feed the value of the array into the macro string command.
Strings are within "" and appear magenta. So your v[i] is just a string and does not extract the value from the array. You need to use string concatenation (https://imagej.net/Introduction_into_Macro_Programming#Using_variables) to feed stuff like variables, outputs from functions or arrays into these string commands like so:

run("Specify...", "width=1 height=500 x=" + v[i] + " y=85");

The corrected macro would look like this:

v = newArray(10,20,30,40,50);

for (i=0; i<v.length; i++) {
	run("Specify...", "width=1 height=500 x=" + v[i] + " y=85");
	setForegroundColor(255, 0, 0);
	run("Fill", "slice");
 	
 }

Have fun!

2 Likes

Hi, thanks so much for your quick reply - that works a treat!

Best wishes,

Andy