ROI Combine Function Glitch

Hi everyone!

I originally thought that I had another issue to deal with, but turns out this is a much bigger issue. My problem is that when I run my macro, the goal of one portion of the macro is to combine all of the selected macros into one. However, I realized that the process of combining, which would take a long time since I have several thousand ROIs, is being cut short, and as a result, only one of the ROIs is being added to the new combined ROI. Please help! I am on a time constraint and I have been trying to figure out the problem for so long now and I think I have finally found the culprit. I appreciate any and all advice and help!

The section of code in question is as follows:

roiSelect = newArray();
size = roiManager(“count”);
for (x=0; x<size ; x++)
roiSelect = Array.concat(roiSelect ,x);
roiManager(“select”, roiSelect);
roiManager(“combine”); ***
print(“combining ROIs…”);
roiManager(“select”, 0);

The line with the 3 asteriks are where ImageJ is cutting short the process of combining. Thank you again for the help!

Dear jchen,
I think the issue is that you do not saved the combined ROI. Try replacing the last 2 lines with this:


length = roiManager("count");

roiManager("Select", length-1);

roiManager("Save", "C:/Users/Desktop/Example.roi");

This will make sure the combined ROI is added to the ROi and saved, since it doesn’t look like you save it anywhere. Also, the last line of your code specifies the first ROI in the ROI manager, which is probably the reason why only one ROI is saved.

Dear jchen,

I think there are some slight misconceptions regarding the use of roiManager(“Combine”) in your code. Firstly, roiManager(“Combine”) doesn’t combine the existing/selected ROIs and then replaces the old ROIs, but creates a new ROI that contains all the existing/selected ROIs. Secondly, the combined ROIs are not automatically added to the ROI manager. If you want the combined ROI to be added to the ROI manager, you have to follow the roiManager(“Combine”) with an roiManager(“Add”).
Below is a short sample macro to illustrate this. It creates a new image and 2 rectangular ROIs. Using the roiManager(“Deselect”) deselects all ROIs, which makes sure that the next roiManager command operates on all ROIs (see roiManager macro function descriptions: The two ROIs are then combined and the new combined ROI is added to the ROI manager. Each of the original ROIs followed by the combined ROI is then shown on the image for 1s.

Hope this helps you to use the roiManager(“Combine”) successfully,

run(“Image…”);newImage(“Test”, “8-bit black”, 2000, 2000, 1);
makeRectangle(344, 360, 428, 320);
makeRectangle(700, 828, 668, 284);
roiManager(“Show None”);
roiManager(“Select”, 0);
roiManager(“Select”, 1);
roiManager(“Select”, 2);

1 Like

Ahh I see, yes I think my problem was that I was selecting the 0th ROI. Thank you so much, I will try and see!

Thank you for the advice, I will test it and see if it works! Much appreciated!

Thank you guys for helping me to solve this issue! I now have yet ANOTHER issue which is that my code has been saving the results to an excel file completely fine, but all of a sudden it says that “access is denied” :(. It was working fine before, and now I randomly cannot save it to the exact same folder that I’ve been saving the excel file to.

Also, when I measure the ROIs, the mean and the median values are all 0 in the results table. Any ideas why this would happen?

It was literally working every single time, and all of a sudden it doesn’t work even though I didn’t touch a single thing that has anything to do with saving.

My code is as follows:

excel = getDirectory(" DESTINATION TO SAVE EXCEL ");
roiManager(“select”, roiSelect2);


saveAs(“Results”, excel); ****

roiDelete = newArray();

for (x=1; x<size ; x++)


roiDelete = Array.concat(roiDelete, x);


roiManager(“select”, roiDelete);


Dear @jchen
I was able to replicate the issue that you had. It is because the saveAs function does not accept “Results” as an input. It only takes image filename extensions.
Since you want to save the measurement as an excel file, I would look at this example of how to use the Read and Write Excel plugin. You will need to add the plugin under Help -> Update... -> Manage Update Sites.

Hi Spencer!

I actually just fixed it, the reason for this was because I had to write it as:

saveAs(“Results”, excel + subj + “.csv”);

^^ This works.

I still have the issue of the mean and median not showing up, however, which is very strange.

Dear @jchen
On the results table, you can change which values are displayed. Results -> Set Measurments.

Hi Spencer,

Yes I have already checked those boxes off, and the mean and median still show up at 0.


Depending on your previous unlisted steps, your mean and median results should be either 0 or 255, since you binarized your image to run analyze particles. It sounds like you want to add the ROIs back to the original image first and then get the Results.

Yes that is exactly the goal. And that’s strange though because when I run the exact same process manually, there is no issue.


run(“Auto Local Threshold…”, “method=Bernsen radius=15 parameter_1=40 parameter_2=0 white”);


run(“Analyze Particles…”, “size=10-Infinity clear summarize add”);

^^ These are the processes I am running on the images.

Not sure why it is different on yours, but once I generate the mask, the results are usually 255. When I reselect the original image, “Add” the ROIs to it, then Measure again, I get the updated values from that image.

Hmm, that’s really strange, and super annoying haha. And for some reason, when I run my macro, rather than selecting the new combined ROI at the bottom of the ROI Manager, it selects the top one. But when I run the code line by line, it works perfectly.

I use this little macro to add selections to the ROI manager. It allows me to name the new ROI and keeps track of it’s index for me. The index is the number you need to recall the ROI programmatically. It would be nice to have it as a first column in the ROI manager. I just added it to my startup macros, replacing the “t” keyboard shortcut. It really makes ROI processing easier to keep track of since you can name your ROIs something meaningful to yourself.

macro ‘Selection to ROI [t]’
roiname = getString(“Enter the name of this ROI”, “ROI”);
roiindex = roiManager(“count”) -1;
roiManager(“select”, roiindex);
roiManager(“rename”, roiname+" index= "+roiindex);

To use it in a program just make it a function and pass in the roiname, which you can get from the user, or generate in the code.


function roiAdd(name)
roiindex = roiManager(“count”) -1;
roiManager(“select”, roiindex);
roiManager(“rename”, name+" index = "+roiindex);

roiAdd(“My ROI”);