ROI Save Overwriting Old ROIs

Hi everyone!

I just want to first thank everyone who has helped me in my journey in completing my code, it works well now and I am so grateful for all the help I have received from the community and I really appreciate the time and the help you all have given me!

With that being said, I do have one issue, which is that my old ROIs are still being overwritten every time I save a new ROI.


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


^^ this is the only line of code that deals with saving the ROI. “roiSave” is the source directory of where the ROIs will be saved (the folder where they are saving), but for some reason, rather than saving my ROIs like this:

  • ROI1.zip
  • ROI2.zip
  • ROI3.zip

It is instead either overwriting the previous ROI, or it is only saving the very last ROI drawn. I would really appreciate any help!

Thanks!

Dear jchen,

Your code

will save the selected ROIs to whatever location is specified in the variable roiSave. Unless you change the variable roiSave in our code, it will overwrite existing files with the same name. It will not automatically increment the filename if the filename already exists.
I would suggest to include print(roiSave) in your code prior to that code line to see the actual path/filename that is used for saving the ROIs.
In order to fix your issue make sure that your code gives each ROI set that you want to save a unique name.
If roiSave is the path to your source directory, you could try something like this:
roiManager(“save selected”, roiSave +“ROIset”+index+ “.zip”);
index=index+1;

Include a line at the start of your code to define index and set it as 0:
var index=0

This should make sure that the index is incremented every time an ROI is saved. Obviously, if you re-process a folder that has already been processed and it contains files with the name ROIset+index.zip, they would be overwritten.

Good luck,
Volko

1 Like

Hi @jchen
And this?
Can this answer your problem.
Thank you for your reply.

run("Blobs (25K)");
run("Duplicate...", "title=1 ");
close("\\Others");
run("Duplicate...", "title=2");
//-------------------------------------------
selectImage("2");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(126, 255);
run("Convert to Mask");
run("Set Measurements...", "area add redirect=None decimal=3");
run("Analyze Particles...", "size=350-Infinity display 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");
roiManager("Select", 0);
roiManager("Rename", "Roi-2-Zip");
roiManager("Deselect");
//--------------------------------------
selectImage("1");
setAutoThreshold("Default");
//run("Threshold...");
//setThreshold(126, 255);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=0-100 display clear add");
roiManager("Show All without labels");
n=roiManager("count");
a=Array.getSequence(n);
roiManager("select",a);
roiManager("Combine");
roiManager("add");
roiManager("Delete");
RoiManager.setPosition(0);
roiManager("Set Fill Color", "red");
//--------------
roiManager("Select", 0);
roiManager("Rename", "Roi-1-Zip");
close("1");
selectImage("2");
roiManager("Add");
//--------------------------------
close("Results");
roiManager("Deselect");
roiManager("Show All");

If the combined ROI is not suitable you can “split”

Yep. Still the same problem as before.

Hi Research_Associate!

The actually renamed the selected ROI every time according to the subject number!


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


^^ this line will increase the “subj” number every time a new ROI is created, thus it should be saving it as a different name every time, which is very strange.

Hi Volko!

Sorry I should have mentioned that my code will increment the number every time!


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


^^ the line above will add 1 to the “subj” number every time a new ROI is created, however, I it is still being overwritten :confused: it is very strange!

Hi Matthew!

I will look into it and take your advice into consideration. Thank you so much!

It should not be saving it as a different name every time, you are still using the same name.

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

In other words, it doesn’t matter what you rename in the ROI manager if you still write the output file with the same name each time.

Hi Research!

I changed it to where it will get the name of the image file and rename the ROI as that, but I am still getting the same problem for some reason. I changed it to this:


name = File.getNameWithoutExtension(source+list[i]);
roiManager(“rename”, name + “: total Sox9 ROI”); // renames the ROI
roiManager(“select”, (size-1));
roiManager(“save selected”, roiSave + “.zip”);


“source + list[i]” is just the image that the ROI was drawn from and can be ignored. So basically, it will get the image name, save it to the “roiSave” destination, and append a “.zip” to the end. However, it is still only saving the very last one for some reason.

You still haven’t shown what roiSave is. Whatever roiSave is, the resulting file should be
roiSave.zip.

It should be the ROI(s) from the last image you created. None of your naming increments there are being applied to the file name. None of the ROIs from your previous images are being saved.

Have you tried roiSave+name+".zip" ?

Hi Research!

“roiSave” is simply the folder that it is being saved to.


roiSave = getDirectory(" DESTINATION TO SAVE ROI ");


So if I am not mistaken, it is just indicating where the ROI will be saved to, and has nothing to do with the naming.

Hi jchen,

roiSave has everything to do with the filename under which your ROIs will be saved. It won’t automatically use the name of the ROI as the filename for saving the ROI.
You NEED to specify the directory plus the FILENAME in your code, so something like
roiManager(“rename”, name + “: total Sox9 ROI”); // renames the ROI
roiManager(“select”, (size-1));
roiManager(“save selected”, roiSave + name + "_total Sox9 ROI.zip”);

Please try this.
Volko

1 Like

Hi Volko!

This works perfectly! Thank you so much for your help! I really appreciate it :slight_smile:

Hi Volko!

One last question, do you know if there is a way to open the ROI Manager without having any ROIs? Is there some button I can click to open the ROI Manager?

You can get an empty ROI manager simply by clicking Image->Selection->Add to Manager or with the keyboard shortcut ‘Ctrl-t’ (the menu/keyboard shortcut to add a selection to the ROI manager; just ‘t’ also works). If there is no selection on the image, it will open an empty ROI manager. If there is a selection, it will open the ROI manager and add the selection to it.
Glad we managed to solve your problems,
Volko

Hi Volko,

Hmm, it seems like I do not have that option.

image

Unless you mean when an image is actually open? And yes I am so grateful for all the help!

Analyze->Tools->ROI manager… doesn’t work?

Ahh, yes got it, that works! Thanks so much!