How to return selection coordinates?


I am trying to write a macro to crop a bunch of images. The images are registered so I want to specify the crop region for the first image and then apply for the rest.

Can someone tell me how can I save the coordinates of the selection? For example if I make a rectangle

 makeRectangle(406, 780, 1092, 510);

how can I return the coordinates [406, 780, 1092, 510] into a variable?

Thank you!

Good day,

while this is possible (see the available ImageJ macro functions), you should first consider:

run( "Restore Selection" );

Also available from “Edit >> Selection >> Restore Selection”.



Unfortunately, this doesn’t seem to work. If I close the image, then open another one, then run( “Restore Selection” ); the previous selection does not appear again.

Am I misunderstanding you? Could you please clarify?


you didn’t state that you close the image before the ROI-transfer to a new one.

As I’ve written before, you may use macro functions:

The most general:
getSelectionCoordinates(xpoints, ypoints);
The one for rectangular selections:
getSelectionBounds(x, y, width, height);