Skip to content

Instantly share code, notes, and snippets.

@flowernert
Last active November 25, 2022 11:17
Show Gist options
  • Save flowernert/b6ab9f8f6c415dab62a447edde3d8dc8 to your computer and use it in GitHub Desktop.
Save flowernert/b6ab9f8f6c415dab62a447edde3d8dc8 to your computer and use it in GitHub Desktop.
ImageJ FiJi : retrieving indices or names of multiple selected ROIs
//returns an array of selected ROIs numeric indices
function getSelectedROIsIndexes() {
//calls existing java method, then parse it to an ImageJ Macro langage Array
rois = eval("script","java.util.Arrays.toString(RoiManager.getInstance().getSelectedIndexes())");
rois = substring(rois, indexOf(rois, "[")+1, lastIndexOf(rois, "]"));
rois = split(rois, ",");
selectedRois = newArray(rois.length);
for (i = 0; i < rois.length; i++) {
selectedRois[i] = parseInt(rois[i]);
}
return selectedRois;
}
//leverage previously created function to return an array of selected ROIs names
function getSelectedROIsNames() {
indexes = getSelectedROIsIndexes();
names = newArray(indexes.length);
for (i = 0; i < indexes.length; i++) {
names[i] = RoiManager.getName(indexes[i]);
}
return names;
}
@flowernert
Copy link
Author

demo code

names = getSelectedROIsNames();
for (i = 0; i < names.length; i++) {
    print(names[i]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment