ImageJ mark centroid macro was working but has now stopped


I am currently measuring experimentally induced ballistic wounds in bone. I have been getting my head around running macros for the first time and managed to get the mark centroid macro working in ImageJ (version 1.52a). I followed the instructions of copying and pasting the code into an editor window. However, after returning to my laptop to run it again when I click “run macro” the selected region of interest vanishes and the centroid is not marked on the image. I did not change anything in the settings or the code. No matter what I try I cannot get the centroid to appear again.

Can anyone suggest what might have gone wrong and how I might rectify this issue?

Thanks in advance,



Can you share with us the code you are running as well as an image file? This way - we can replicate what you are seeing…

You can also try updating to the more recent 1.52p version and run it again just to be sure.


Thanks for responding so quickly. I have uploaded one example image which is of the exit side of a bone defect, which I hope opens OK. I use the wand tool (I have found the settings 30 tolerance and 8 connected works best) to make the darker entry hole in the centre the ROI; this is where I want the centroid marked. The code I used was from the examples provided in the ImageJ website as was as follows:

 n = nResults;
  if (n==0)
     exit("No Results");
  if (isNaN(getResult("X",0)))
     exit("No centroids in Results");
  x = newArray(n);
  y = newArray(n);
  for (i=0; i<n; i++) {
      x[i] = getResult("X", i);
      y[i] = getResult("Y", i);
  makeSelection("point", x, y);

I hope the picc opens; any problems and I will attempt to re-load,

Kindest regards,



I don’t think you need any code quite so fancy… once you have that region selected with the wand tool (which I recommend you saving as a ROI to the ROI Manager)… you can simply retrieve the XY coordinates of the calculated centroid. Make sure in your Set Measurements - that you check “Centroids” and then when you Measure your selected region - you can retrieve those XY coordinates from the Results Table - they are the X and Y headings.

So your code can be as simple as:

// First, select the region you want via the wand tool... 
waitForUser("Select the wand tool and make your selection.\n(Settings: 30 tolerance, 8-connected)>\nClick 'OK' when finished.");
roiManager("Add"); // save that selected region
// Second, generate point selection
makePoint(List.getValue("X"), List.getValue("Y")); // you can also add this point to the ROI Manager by calling roiManager("Add"); - then all those ROIs can be saved.

Another solution provided by Herbie - one that is more automated (no wand tool):

setOption("BlackBackground", true);
run("Duplicate...", "title=copy");
setThreshold(0, 30);
run("Convert to Mask");
run("Analyze Particles...", "size=150000-Infinity circularity=0.50-1.00 add");
roiManager("Select", 0);
run("From ROI Manager");
run("Overlay Options...", "stroke=green width=0 fill=none");
makePoint(List.getValue("X"), List.getValue("Y"));
run("Add Selection...");

Hi again,

Thanks so much for your help on this and especially for the code. Sorry for another question; I am really just beginning to get to grips with the macro side of things. Do I simply copy and paste the code into the editor window and select run macro for the centroid to be displayed?

Thanks again your support has been fantastic and very much appreciated,

Kind regards,


Yes. Copy/paste the code into the Script Editor - select the “IJ1 Macro” language… and then click ‘Run’.

For more info on scripting in ImageJ, here are some helpful links:

Hi again,

Thanks for the information on this I will continue to read through those. The automated code looks great; the only problem I literally just thought of is that I use the wand tool in order to be able to adjust the position of the line marking the ROI. For example, in the image I supplied there is a section on the top right of the black entry hole that is not actually part of the edge of the entry; rather, it is a detached bone fragment bent downwards. Accordingly, I think I am correct in thinking that the centroid located with this as part of the perimeter would be in a different place to that when the fragment is ignored?

I have tried to run the macro today but it appears I do not have the facility to change the language in the script editor; I have no idea why! I have attached a screen shot showing the tabs available in my script editor. I will try and run the code again and see if I am missing something!

Screen shot.pdf (201.9 KB)
Kind regards,


To start - you should be using the Fiji distribution of ImageJ… that will help. Then you can access the Script Editor as I described above and select a language, etc.

Too - if you have ‘problem’ images and would rather set your selection with the wand tool more interactively - you can always use the short code snippet I provided in my second post: