Help to Update ImageJ Code


My partner and I edited a code for a project we are working on:

Roi.getCoordinates(xpoints, ypoints);
setResult("Xi",nResults-1, x0)
setResult("Yi",nResults-1, y0)
setResult("Xf",nResults-1, x1)
setResult("Yf",nResults-1, y1)
setResult("Displacement",nResults-1, d)

This works great for what we are trying to do, however it is not very efficient. Is there any way to update this, so we can examine multiple ROIs at once? For example, if we were to use the ‘Measure’ tool on multiple points, they would all show up at once. Can we change this code to do the same"?

Currently, I have to individually click on each ROI from the ROI Manager and run this code, but if we eventually use an image with 500 ROIs, then this is not going to work well.

We are unsure of what the best way to do this would be-- whether we need to add some sort of for loop, or change the code entirely. We are unsure if this is even possible.

Please let me know if you have any advice on this situation. Thank you very much.

Hi @Talha_Lone,

Broadly you can iterate through the ROI manager using a combination of roiManager("count") to see how many ROIs you have and roiManager("select", i) to select the ith ROI in the list (see the macro function list for details on these functions).

I’ve written a quick example using your code here:

but the business end of it is

//-- Get the number of ROIs
nROI = roiManager("count");
//-- Iterate through them
for (i = 0; i < nROI; i++) {
	roiManager("select", i);
	//-- Your code goes here as if you've selected the ROI


It’s worth noting that in the linked example, I removed the run("Measure") line as you can use the count variable (i) to dicatate the results table row (previously nResults-1), which (I think) makes the measure line redundant.

Hope that helps!