How to link ROI manager to "analyze particle" function

Cheers.

I’m trying to analyze the porous structure of the bone, using the “Analyze particles” function. I want to analyze only the region within the boundary of the bone, in order to get data on the distribution and shape of the porous channels, as well as the ratio of porosity on the bone cross-section surface.


I know I should use the ROI manager, but I cannot find a way to link the ROI manager to the “Analyze particles” function. Within the ROI manager, I can only see the “measure” function, which doesn’t segment the porous, particle-like structures.

I have to mention that I’m working with stacks, and my ROIs are changing from slide to slide.
I will be grateful for the guidelines.

One approach is to use the duplicate function (run(“Duplicate…”, " ") ) to duplicate the selection. This will copy the ROI into a new imagej window and allow you to run analyze particle for only that section. If you have multiple ROIs you need to analyze, for example this script will cycle through all the ROIs and analyze particles within:

numROIs= roiManager("Count");
for (j=0; j<numROIs; j++)
{
	selectImage(imgID);
	roiManager("select",j);
	run("Duplicate...", " ");	
	run("Analyze Particles...", "size=0-20 circularity=0.0-1.00 show=Overlay clear include");;
	run("Close");
}
selectImage(imgID);
run("Close");

If you want to add the analyze particle output to the ROI manager for each iteration, you just need to save the roi Manager before entering the loop with the function

	roiManager("save", fileDir + "\\roiManager.zip") 

and open it again at the end of each step through

	roiManager("open", fileDir + "\\roiManager.zip")

Many thanks for the reply.
I understand the proposed solution, but I have limited experience with how to run codes in ImageJ.
How can I execute this code? This is what I did sequentially.

  • Plugins > New > Macro
  • Copied and pasted the code
    Untitled
  • Create ROIs in the ROI manager -> Save
  • Click “Run” in the Macro window

It produces this error:
Untitled2

What am I doing wrong?

Cheers,
Shahed

Hi,
you’ll need to define your Id “imgID” as such:

imgID= getImageID();

This will associate the variable imgID with the ID of the current windows.

Hi,
Thanks for the guideline. It goes through all the images with their corresponding ROI now, which is fantastic.
I don’t see the results table, nor the summary table though! Usually they pop-up automatically after the analysis is finished. Are they stored somewhere else when you run a Macro?

Cheers,
Shahed

I would advise you to use “Macro > Record” to have the “Analyze Particles” command as you want it.
I just did it and the command that would open a summary and a result table is the following

run("Analyze Particles...", "size=50-Infinity display summarize add");

The same with hole filling (maybe not of interest for you):

run("Analyze Particles...", "size=50-Infinity display include summarize add");

Hope that helps

I’m a dummy! I just realized how the code functions :slight_smile:
Thank you for the explanation.

One minor issue still, that I don’t get the summary table, even though the word “summarize” is added in the command.

numROIs= roiManager(“Count”);
imgID= getImageID();
for (j=0; j<numROIs; j++)
{
selectImage(imgID);
roiManager(“select”,j);
run(“Duplicate…”, " ");
run(“Analyze Particles…”, “size=0-Infinity circularity=0.0-1.00 display exclude summarize”);
run(“Close”);
}
selectImage(imgID);
run(“Close”);

Instead, try

run(“Analyze Particles…”, “size=0-Infinity circularity=0.0-1.00 display summarize”);

You may want to save the output also

selectWindow(“Summary”);
saveAs(“Text”, getDirectory(“home”)+“Summary”+j+“.txt”);

Thanks a lot! It works now the way I want it

Ok, I thought the issue was solved, but it seems that I still have a minor problem, which is all the duplicated selections are open at the end of the analysis, and I have no other option than to use Ctrl+Alt+Del (task manager) to forcibly close Fiji, and run the analysis for the next sample. Considering I have +200 samples, it’d help me a lot if there was a line to include in the above macro to automatically close each slide after the analysis. Any clue?

Best,
Shahed