"Fit Sphere" throws error despite 5 points being on different slices

Hi there,

im trying to use the “Fit Sphere” function with 5 points. 4 of them are on one slice, the last is in their center and above them so all 5 points lay on the surface of it. However BoneJ tells me all points are on the same slice. I tested with output-to-logs etc. and they arent on the same slice. Clicking through the ROIs moves the image on the last one, aswell as the coordinates being different. So i am sure that my last point is on a different slice, but yet i get told it is. I cant attach images showing what I try to explain here in my bad english as I’m a new user.

Any help is really appreciated. I tried finding what I’m missing in the source code but all looks perfectly fine.

I think this must be the bit of code that’s throwing the error:

 		final double i = points[0][2];
		double j = i;
		for (final double[] p : points)
			if (p[2] != i) j = p[2];
		if (j == i) {
			IJ.showMessage("Can't fit sphere to points.\n" +
				"All points are on the same slice.\n" +
				"Check the ROI Manager option More >> Options... >\n" +
				"Associate ROIs with stack positions.\n");
			return;
		}

Did you follow the instructions to set the ROI Manager option to associate ROIs with stack positions? It is possible to set point ROIs that don’t have the z position defined, which may be the source of your problem.

Thanks for the fast answer!
I have tried activating the option "Associate “Show All” ROIs with slices " but it didnt help. The option which the text message suggests is not present for me. To test I also selected all 5 points and pressed “Meassure”. In the results table it correctly shows each points X and Y coordinates along with the right slice. Apparently I can now attach pictures now so here they are, its hard to explain without :slight_smile:
I use 1.53c and all plugins are updated

Code:

Log file:

log

Message:

message

ROI-Manager:
roi

Could you please post your macro code as text, within a pair of code markup notations (```)? That will make it much easier to reproduce.

You should debug your macro with something like this, which will check to see what the ROI Manager has stored as the coordinates of each point: (you may need to update your ImageJ version).

  n = roiManager("count");
  for (i=0; i<n; i++) {
      roiManager("select", i);
      Roi.getBounds(x, y, width, height);
      Roi.getPosition(channel, slice, frame);
      setResult("x",  i, x);
      setResult("y", i, y);
      setResult("z", i, slice);
  }
  updateResults();

This test macro runs OK:

run("Bat Cochlea Volume (19K)");
makePoint(58, 27, "small yellow hybrid");
roiManager("Add");
makePoint(16, 61, "small yellow hybrid");
roiManager("Add");
makePoint(50, 103, "small yellow hybrid");
roiManager("Add");
makePoint(101, 66, "small yellow hybrid");
roiManager("Add");
run("Next Slice [>]");
makePoint(55, 63, "small yellow hybrid");
roiManager("Add");
run("Fit Sphere", "  padding=2 crop=1");

I edited my code to include your part. The result table shows that all points are on slice 0.

After that i copied your test macro and ran it. I get the same message. The option that the error message suggests was activated.

Here is the code i ran on 1.53c and 1.52v as a test:

run("Bat Cochlea Volume (19K)");
makePoint(58, 27, "small yellow hybrid");
roiManager("Add");
makePoint(16, 61, "small yellow hybrid");
roiManager("Add");
makePoint(50, 103, "small yellow hybrid");
roiManager("Add");
makePoint(101, 66, "small yellow hybrid");
roiManager("Add");
run("Next Slice [>]");
makePoint(55, 63, "small yellow hybrid");
roiManager("Add");

a=0;
b=0;
c=0;
d=0;
channel=0;
slice=99999;
frame=0;

  n = roiManager("count");
  for (i=0; i<n; i++) {
      roiManager("select", i);
      Roi.getBounds(a, b, c, d);
      Roi.getPosition(channel, slice, frame);
      setResult("x",  i, a);
      setResult("y", i, b);
      setResult("z", i, slice);
  }
  updateResults();

run("Fit Sphere", "  padding=2 crop=1");

Results table:

results

I found a workaround now! By adding the Roi.setPosition between makePoint and the Adding it works

			setSlice(z[i]+1);
			print("Slice1 = "+getSliceNumber());
			
			makePoint(xTemp[0],y2[0]);
			Roi.setPosition(z[i]+1);
			roiManager("Add");
			
			makePoint(xTemp2[0],y2[0]);
			Roi.setPosition(z[i]+1);
			roiManager("Add");
			
			makePoint(x2[0],yTemp[0]);
			Roi.setPosition(z[i]+1);
			roiManager("Add");
			
			makePoint(x2[0],yTemp2[0]);
			Roi.setPosition(z[i]+1);
			roiManager("Add");
			
			setSlice(zTemp[0]+1);
			print("Slice2 = "+getSliceNumber());
			
			makePoint(x2[0],y2[0]);
			Roi.setPosition(zTemp[0]+1);
			roiManager("Add");
		
			run("Fit Sphere", " padding=2 crop=1 add_to_roi_manager");
1 Like