ImageJ macro to set the location (x,y) to the selection


I am new to the forum, so forgive me if this is a naiive question:

Is there a way to set the location of my focus within an image? I have a large z-stacked width/height image where I zoom in and work on adding/editing ROIs. Frequently, I am having a hard time to find where my (already added) ROI is because it is outside the zoomed area I am looking in. In the macro language, it is easy to select the ROI by the command roiManager("Select", index), however this only sets the correct z-slice but not the “focus” to the ROI in x,y.

I was looking into the built-in functions list but could not find a obvious candidate (setLocation seems to be setting the image window location, not the focus).

Any help (that it does allow to do it also fine) appreciated!

You can zoom to the selection with the macro command:

run("To Selection");

In the menu you can find the action here:

Image->Zoom->To Selection

1 Like

Note that the ImageJ menu has a function ‘Zoom to Selection’ (Image->Zoom->To Selection). You can use this after you have selected your ROI to focus on the ROI. In a macro you use run(“To Selection”) to zoom to the selected ROI.
Hope this helps,

1 Like

Thank you all!

I see that this also reduces the window just to fit to the zoom (instead of just “going” there), so I made a crude workaround to get back to the original zoom setting and screen setting.

current_zoom = getZoom();
getLocationAndSize(x, y, width, height); // This gets the current WINDOW position and size.
getSelectionBounds(sx, sy, sw, sh); // This is necessary to center the selection to the correct bounds
dx = (width / 2) / current_zoom; dy = (height / 2) / current_zoom;
setSelectionLocation( sx - dx + sw/2, sy - dy + sh/2 );
run("To Selection"); // This goes to the selection (Zoom menu) but also changes the window size, which is undesirable.
run("Set... ", "zoom=" + current_zoom*100); //This reverts the zoom but then....
Table.setLocationAndSize(x, y, width, height, getTitle() );		
setSelectionLocation( sx, sy ); // Put this back.

If there is anything that could be more easier to simply “jump” to the location, very much appreciated!