Problem with "working" macro

Hello everybody,

I have found a useful macro in another thread on this forum that I wanted to use. It however does not seem to run for me as intended:

When I run the macro, it starts normally by asking for a rectangular ROI. After pressing “ok” it starts creating thousands of lineson the same spot instead of multiple lines in the defined distance (50 Pixel). After aborting the macro manually, the ROI “list” shows me, that the first line is duplicated 100k times and only the last line is shifted by the required 50 pixels.
Does anybody know why this is happening?
Code:

waitForUser("Draw your rectangular ROI... then click 'OK'.");
roiManager("Add"); // add rectangle to ROI Manager
roiManager("Select", 0); // select it
Roi.getBounds(x, y, width, height); // get dimensions
roiManager("Deselect"); 
roiManager("Delete"); // delete rectangle ROI (we don't need it anymore)

makeLine(x, y, x, y+height); // draw line at beginning of rectangular ROI...
roiManager("Add");

pixelDist = 50;
xPosition = x+width; // end of rectangular ROI
i = 0;
while (x < xPosition) {
	if ((pixelDist + x) < xPosition){	 // only add ROI lines within rectangular ROI
		roiManager("Select", i);
		roiManager("translate", pixelDist, 0);
		roiManager("Add");
		Roi.getBounds(x,y,width,height); // update x position
		i++; // update ROI that is selected
	}
}
roiManager("Show All");
roiManager("Deselect");
roiManager("Multi Plot");

Output:

Additional information:
I am using Win7 64bit, ImageJ build 1.52o, Java 1.0.8_172.

I would appreciate any help or comments,
Best,
KG

@KasparG

That macro definitely wasn’t working correctly. Sorry about that. Give this a try:

waitForUser("Draw your rectangular ROI... then click 'OK'.");
roiManager("Add"); // add rectangle to ROI Manager
roiManager("Select", 0); // select it
Roi.getBounds(x, y, width, height); // get dimensions
roiManager("Deselect"); 
roiManager("Delete"); // delete rectangle ROI (we don't need it anymore)

makeLine(x, y, x, y+height); // draw line at beginning of rectangular ROI...
roiManager("Add");

pixelDist = 50;
xPosition = x+width; // end of rectangular ROI
i = 0;
while (x < xPosition) {
	if ((pixelDist + x) < xPosition) {	 // only add ROI lines within rectangular ROI
		makeLine(x+pixelDist, y, x+pixelDist, y+height); // draw line 
		roiManager("Add");
		Roi.getBounds(x,y,width,height); // update x position
		i++; // update ROI that is selected
	}
}

Does that work for you now??

Hello Ellen (and Kaspar) -

Two issues:

First, this macro has an infinite loop (as did the original
macro). This can be shown, for example, by adding a print
statement right after the while.

Specifically, after x grows such that
(pixelDist + x) >= xPosition but has not yet reached
x >= xPosition, the while loop will keep running, but the
if-clause will prevent x from being further incremented.

This version of the macro eliminates the infinite loop:

// fix infinite loop
//
roiManager("Show All"); // don't know why we need this ...
//
waitForUser("Draw your rectangular ROI... then click 'OK'.");
roiManager("Add"); // add rectangle to ROI Manager
roiManager("Select", 0); // select it
Roi.getBounds(x, y, width, height); // get dimensions
roiManager("Deselect"); 
roiManager("Delete"); // delete rectangle ROI (we don't need it anymore)

makeLine(x, y, x, y+height); // draw line at beginning of rectangular ROI...
roiManager("Add");

pixelDist = 50;
xPosition = x+width; // end of rectangular ROI

// "(pixelDist + x) < xPosition" tests false before "x < xPosition"
// therefore the while loop never terminates
//
// while (x < xPosition) {
//	if ((pixelDist + x) < xPosition) {

while ((pixelDist + x) < xPosition) {	 // only add ROI lines within rectangular ROI
	makeLine(x+pixelDist, y, x+pixelDist, y+height); // draw line 
	roiManager("Add");
	Roi.getBounds(x,y,width,height); // update x position
}

Second – something I don’t understand – unless I add the call
to roiManager("Show All") at the beginning of the macro
(or the ROI Manager is already open), I get incorrect (and maybe
not fully reproducible) results. My only guess is that perhaps there
is some kind of race condition when using the ROI Manager
programmaticaly, and that having the ROI Manager open slows
things down enough to dodge this race condition.

Anyway, that explains why I added 'roiManager(“Show All”)` at
the beginning of the macro.

(I see this behavior running a recently auto-updated version
of Fiji, ImageJ 2.0.0-rc-69/1.52n; Java 1.8.0_66 [64-bit], on
64-bit ubuntu 16.04 LTS.)

Thanks, mm

2 Likes

hey the both of you,

thanks for taking the time to look over this (apparently flawed) macro. The solution seems very simple, but I was of the opinion that the “translate” line takes care of “moving” the line in the desired direction and adds the pixelDist.

@etadobson your version now works in my hands, thanks very much!

@mountain_man thanks aswell for your input, I will keep the “race” condition in mind should there be problems with the macro in the future.

Best,
Kaspar

Hello Kaspar -

Your opinion is right. In the original macro “translate” does move
the line and, in effect, adds pixelDist. The problems in the
original macro were not specific to the use of “translate.”
Regardless, I like the version without “translate” better, as, for
me, it’s a little easier to understand.

(If it were me, I would also not bother using Roi.getBounds()
to recover the updated value of x; I would just call
x += pixelDist and be done with it. But that’s just my
preference.)

Thanks, mm