How to Set roiManager Properties Position(c,s,f) from ImageJ1 macro?

Having a (hyper)stack and a set of roi’s, some of which combined, in the Roi Manager window I can manually click the Properties and set Position(c,s,f), a line width and a colour (green, pink, some other).

When in the ROI manager window I select the third roi, which belongs to the third slice, and I enter in the Properties… Posistion the text 0,3,0, then enter a 4 for the fourth ROI in the fourth slice and a 5 in the fifth ROI belonging to the fifth slice, I can then click the 3,th, 4th or 5th ROI in the Roi Manager window and the particular ROI shows on the corresponding slice. So the stack slice changes on clicking in the ROI manager.

How do I perform this slice-ROI coupling in an ImageJ1 macro? Recording only gives me the Line Width and the Set Color steps, not the Position. Using

	Roi.setPosition(0, slice-1, 0);

doesn’t seem to work; clicking a ROI in the manager does change the ROI but the slice selected remains the same and does not update to the slice belonging to the ROI.

But you can easily set the slice with:

Displays the n th slice of the active stack. Does nothing if the active image is not a stack. For an example, see the MeasureStack macros. See also: getSliceNumber, nSlices.


Thanks for the quick answer, but the intended ‘product’ is a browsable stack.
I’m talking dozens if not hundreds of stacks, each with tens of slices containing hundreds of sub-ROIs each.
Generating the ROIs is one thing, inspection is a necessary second step. Browsing back and forth through the stack while the ROI and Slice change concurrently is essential. The only way I could do that using setSlice(n) is to build a navigation dialog to this end. Or painstakingly enter 0,i,0 for every ROI i. Unless I can simply automate the linking of the ROI to its slice.

So the question is how do I select a ROI in the ROI Manager which updates the shown slice in the stack?

To be precise: how do I select a ROI in the ROI Manager which updates the shown slice in the stack by clicking in the Roi Manager or by moving the stack slice slider.

If I click on ROI’s in different slices in the ROIManager the ROI is selected and the slice is shown, too.

Then there must be a setting in my ImageJ1 (1.52t) environment, or in the ROI Manager settings (Show All unticked, More>>Options>Assiciate “Show All” ROIs with slices’ unticked) that is the cause that for some slices this works properly and for other slices it doesn’t work.

As soon as I have manually set the Properties, the slice changes with the ROI clicked. If I click the the ROIs generated with a macro roiManager("add"); command, the slice doesn’t change with the ROI.

And what happens if you add the information for the ROI after that command?:

Roi.setPosition(channel, slice, frame) - Sets the selection position.

Is it working then?

Ok, you already did that!

But what about if you add:

Roi.setProperty(key, value)
Adds the specified key and value pair to the selection properties. Assumes a value of “1” (true) if there is only one argument.

I can change the slice selection with the properties dialog. So it might work programmatically, too.


This works for me:

Roi.setProperty("Slice", "50")

Any link to where possible key and value pairs are specified? I hardly speak JAVA, so this documentation page didn’t help me much further.

key = "Position";
value = "0,9,0);

did not protest, but pos was “”, and clicking ROI Manager line 1, then line 9 changed ROI displayed but remained at (already selected) slice 27.

Update: willl try.

See my previous post example update.
Open the properties dialog to see the properties name.

I also found this:

run("Properties... ", "stroke=red point=Circle size=[Extra Large]");


With an information from Wayne for the recorder:

this test macro will work as expected if it modified to set the point properties using run(“Properties… “,”…”) calls, as in the following version. The run(“Properties… “,”…”) calls can be generated by using the Edit>Selection>Properties command with the recorder running.

Which then resulted for a hyperstack in:

run("Properties… ", “name=0294-0065-0095 position=0,2,33 group=none stroke=yellow width=0 fill=none”);

Ah, I went for the Properties… in the ROI Manager, not for Edit>Selection>Properties.
I had found the link to Wayne’s post already but didn’t find it that helpful.

Looks like you made my day. One of the convolving factors was that the ROIs are generated by the Analyse Particles, have to be combined and then added. With your solution I just have to sneak in the Roi.setProperty("Slice", "50") between the roiManager("combine") and the roiManager("add");

This works for a Hyperstack (artificial example):

run("Mitosis (26MB, 5D stack)");
run("Properties... ", "name=10 position=1,2,15 group=none stroke=yellow width=0 fill=none");
run("Properties... ", "name=30 position=1,4,30 group=none stroke=yellow width=0 fill=none");

We are talking ImageJ1, right?

With me this sets the ROIs, while slice (c,z,t) 1/1/1 is in view. But.
Then I move the t-slider to 16/51.
Then I double-click the first ROI in the ROI Manager (the one named 10 indeed)
The ROI shows and the t-slider moves to 1. Clicking the second ROI in the ROI Manager hides the ROI named 10, shows the ROI named 30 but the t-slider remains on slice 1.

This leads me to believe that the current slice being selected and the stack being frontmost is essential for connecting the ROI manager to the current slice, not the ‘Position’ command.
That complicates the adding of ROIs (to the stack slice) using Analyse Particles (on a separate window).

If I change the code to the following, clicking in the ROI Manager moves me to c1z1t6 and c1z1t7 respectively:

run("Mitosis (26MB, 5D stack)");
run("Properties... ", "name=10 position=1,2,15 group=none stroke=yellow width=0 fill=none");
run("Properties... ", "name=30 position=1,4,30 group=none stroke=yellow width=0 fill=none");

So it looks as if the setSlice() and having the stack having focus when adding the ROI is necessary.

When I remove the run("Properties... ", lines, the name doesn’t change to 10 and 30, but the slice still changes with clicking the ROI.

My example loads a Hyperstack in ImageJ 1.

Adds two ROI’s to the Roi Manager with the properties.

In the ROI Manager if I click on the second ROI it selects the slices of the ROI.
If I click on the first ROI it moves to the slices of the first ROI.

I’ll mark your answer as the correct one.
Meanwhile I’ve got the desired effect, which also combines the separate ROIs to one single ROI.

run("Confocal Series (2.2MB)");
run("Split Channels");

run("Set Measurements...", "area mean modal min integrated redirect=None decimal=0");
if(nResults!=0){run("Clear Results");}
getDimensions(width, height, channels, slices, frames);

	run("Select None");
	run("Duplicate...", "title=red");
	run("Minimum...", "radius=3");
	run("Maximum...", "radius=12");
	//find area covered by red fluorescence
	setAutoThreshold("Default dark no-reset");
	run("Analyze Particles...", "add");
	run("Properties... ", "name="+i+" position="+i+" group=none stroke=yellow width=0 fill=none");

run("Merge Channels...", "c1=C1-hs c2=C2-hs keep");roiManager("Show None");
run("Tile");selectWindow("ROI Manager");