ROI Save function overwriting old ROIs

Hi everyone!

I am currently writing a macro in batch mode to analyze multiple images at once. In this section of the code, I want the program to combine all of the ROIs into one huge ROI and save it with a specified name, however, every time it saves, it overwrites the old ROI that was originally there, rather than saving as a new one. My code is as follows:


roiManager(“select”, 0);

roiManager(“rename”, subj + “total Sox9 ROI”);

print(“Renamed ROI…”);

roiManager(“select”, 0);

roiManager(“save selected”, roiSave + “.zip”);

selectWindow(“ROI Manager”);


At this point, all of the ROIs needed to combine into one have been selected, and it should be saving each ROI, however, it is overwriting the old ones. For example, I have 3 images and the code should save 3 ROIs to the folder with the names:

  • 1total Sox9 ROI.zip
  • 2total Sox9 ROI.zip
  • 3total Sox9 ROI.zip

Instead, it is only saving the latest one and overwriting the old ones, so once the macro is completed, it only saves:

  • 3total Sox9 ROI.zip

I would really appreciate any and all help!

Dear jchen,
It looks like you are just selecting the first ROI in the ROIManager when you call roiManager(“select”, 0);.
I don’t know how you are iterating thorough the different ROI, but it looks like you are just calling and saving the first ROI each time you run this loop. Is their more code to this macro?

Hi Spencer!

Yes there is more code! Sorry I should have mentioned that. I have a for loop above this section that groups all the ROIs into an array and combines them into one, so the line where the ROIManager “selects” is where it’s selecting the combined ROI. My main issue now is that it does not save it as a separate ROI, rather, it overwrites the old ones.

1 Like

Dear jchen,
Could you include that code as well? I don’t see any issues in the code you posted, so I would guess there is an issue somewhere else.

Sure thing!

for (x=0; x<size ; x++)
{
roiSelect = Array.concat(roiSelect ,x);
}


Here is the for loop that would get all of the ROIs. I think it’s actually fine if I just keep the ROI Manager open since it works like that. Do you know how I can open the ROI Manager by itself rather than having to run some random process to have it pop up?

Also, do you know how I can open a specific ROI.zip file that I previously saved in one of my local files? I’m hoping to have the code open a different specific file name whenever needed, is this possible?

I greatly appreciate all the help!

1 Like

Dear jchen
I guess it would make sense to keep the ROIManager open if that is easier. The command to open the ROIManager is:

run("ROI Manager...");

As for opening a specific ROI.zip, I’m assuming you have the file path and name? Because you can just run either:

open("C:/Desktop/Folder/Example_File.zip");

or

roiManager("Open", "C:/Desktop/Folder/Example_File.zip");

If you don’t have the file path, you could use this script to tell your script to iterate through files in a folder.

//Get File from Folder
filepath=File.openDialog("Select ROI");
roiDir=File.directory;

//Get List of files in folder
fileList = getFileList(roiDir);

//Iterate through all files in specified folder
for(i = 0; i< fileList.length; i++){
       //Check if file ends with .zip
       if (matches(fileList[i], ".*zip.*")){
              roiManager("Open", roiDir+fileList[i]);
       }
}

Perfect thank you! I think the last one to interate through each file will work perfectly for my work. I appreciate all the help you’ve given me!

Sorry I just realized that I still had the error of the ROIs overwriting each other when I save! Do you know what may have caused this?

Dear jchen,
Can you post some images of the results you are getting? I am having some issues understanding what you want the macro to do.
Looking back at how your original comment, it sounds like you have multiple images open which each have multiple ROI on each image. From what I understand, you want to combine all of the ROI on an image and save it? Just as a heads up, if all of the ROI are open in the roiManager, then their is no way to distinguish which ROI originated from which image.
Also, are you iterating through the images in some manner? The code you posted seems to indicate subj should be changing, but I can’t see where this is occurring. If your just saving over old files, then that means you are not properly iterating through the images.

Hi Spencer,

I think that I can solve this issue on my own. If you do not mind helping, I have another bigger issue, which is that when I try to combine all my ROIs, the macro does not wait for all the ROIs to finish combining, rather, it cuts the process short, therefore only 1 ROI is added to the “new total combined” ROI. Any ideas how to fix this?

@jchen
Hi
If I understand this:
[

so test this:

run("Blobs (25K)");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(126, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Set Measurements...", "area add redirect=None decimal=3");
run("Analyze Particles...", "display exclude clear add");
n=roiManager("count");
a=Array.getSequence(n);
roiManager("select",a);
roiManager("Combine");
roiManager("add");
roiManager("Delete");
RoiManager.setPosition(0);
roiManager("Set Fill Color", "blue");

also try this

run("Blobs (25K)");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(126, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Set Measurements...", "area add redirect=None decimal=3");
run("Analyze Particles...", "display exclude clear add");
n=roiManager("count");
a=Array.getSequence(n);
roiManager("select",a);
roiManager("Combine");
roiManager("add");
//roiManager("Delete");
RoiManager.setPosition(0);
roiManager("Set Fill Color", "blue");

It looks like you are only saving with a single name, so the expected result would be one file with the most recently processed results. All previous results would be overwritten.

If “roiSave” is changing, I don’t see that indicated anywhere in your script.

Again, this seems to be the expected behavior. If you calling combine on all ROIs, you expect one ROI as a result. It should also be the last ROI on your list.

" roiManager(“combine”)
Uses the union operator on the selected ROIs to create a composite selection. Combines all ROIs if none are selected."
https://imagej.net/developer/macro/functions.html

Looking back, I see someone else already answered this question, though.