Looping over ROI results table and extracting XY coordinate values

imagej
macro

#1

Hi all,

Sorry if this questions is a little basic, but I’ve had not had much luck searching the forums or macro examples for a specific answer to my problem. As a follow up to my last post, I’ve been trying to figure out the best way to iterate over multipoint ROIs in multiple stacks, before extracting the XY values for each point. I’m still learning the macro language and this is what I have with so far:

numROIs = roiManager("count");
for(i=0; i<numROIs;i++) // loop through ROIs
	{ 
	roiManager("Select", i);
	results=roiManager("Measure");
	for (j=0 j<results;j++) {
		xValue = getResult("X", j);
		yValue = getResult("Y", j);
		print(xValue);
		print(yValue);
	}

However, I don’t seem to be referring to the results table correctly (i.e. in the debug window, the results variable appears to be empty). Can anyone point out where I’m going astray?


#2

Maybe this macro does what you want?
I did not figure out a way to add a multi-point-ROI with a macro command, so these have to be added manually. For more than 1 multi-point ROI it seems to be necessary to switch to a different tool (e.g. rectangle) in bewteen.
The macro does not involve the results table, but still I think you get what you want.

//prepare image with 3 point-ROIs
newImage("Untitled", "8-bit black", 500, 500, 1);
waitForUser("add 1 or more multipoint ROIs to the image and ROI Manager (T)");

numROIs = roiManager("count");
for(i=0; i<numROIs;i++) {// loop through ROIs
	roiManager("Select", i);
	getSelectionCoordinates(x, y);//if the ROI is a multipoint ROIs x and y are arrays
	print("\n----------ROI "+i);
	print("x-array size: "+x.length);
	print("y-array size: "+y.length);
	print("");
	for (j=0; j<x.length; j++) {
		print("x"+j+" coordinate: "+x[j]);
		print("y"+j+" coordinate: "+y[j]);
	}
}

print("\nor print the x and y Arrays directly ...");
Array.print(x);
Array.print(y);

#3

Thanks, I think this is exactly what I need! Maybe the fact that multi-point ROI coordinates are stored as an array was the piece of the puzzle I was missing.