Center of Oval Selections

Hi

My programming is rusty. Can someone please tell me how I get the centroid coordinates of a oval selection in a macro?

The user will be making Oval selections, I then want to draw lines in the overlay between the centers of the oval selections.

I saw that ImageJ can get the centroid coordinates under Analyze>Set Measurements>Centroid, but I have no idea what code to use to get these values in a macro.

Thank You
JuLTeR

@JuLTeR

The Built-in Macro Functions page is the best place to look for such things… having that open while you are editing code is super helpful…

But you are correct - in that if you Measure the centroids of your selections - you can use macro code to extract those values from the Results Table. This is the code call I would use for just that (as found in the Built-in Macro Functions page I linked above):

getResult(“Column”, row)
Returns a measurement from the ImageJ results table or NaN if the specified column is not found. The first argument specifies a column in the table. It must be a “Results” window column label, such as “Area”, “Mean” or “Circ.”. The second argument specifies the row, where 0<= row <nResults. nResults is a predefined variable that contains the current measurement count. (Actually, it’s a built-in function with the “()” optional.) Omit the second argument and the row defaults to nResults-1 (the last row in the results table). See also: nResults, setResult, isNaN, getResultLabel.

So the code call would look like:

getResult("X", i); or getResult("Y",i); and most likely you’d need to use a for loop to move through the rows…

If you have more specific questions regarding code snippets… just share them with us here and we can help.

Thank You for the tip.
And thank you for the method I needed.
Much Appreciated

1 Like

Hi @JuLTeR

Just as a counterpoint to @etadobson’s excellent answer, it’s maybe worth pointing out the List functions (see link to macro functions above). This is a useful way of pulling any measurement you would normally get from the Results table.

I made a quick example that will hopefully be of some help:

https://bitbucket.org/snippets/davemason/ne5yAK

blobs

3 Likes

Thanx
More is better

Slightly unrelated.
Whenever I set a scale for the image the lines that it draws doesn’t correspond with the center of the drawn ovals. But If I remove the scale it works perfectly again. Can anyone tell me why?

The values reported by [ Analyze > Measure ] or via List.setMeasurements are reported in pixels for uncalibrated images or if your image is calibrated, they return them in calibrated units.

The command makeLine however, always takes coordinates in pixels.

Any easy way to fix this is to divide the coordinates reported by the pixelWidth (or height). Two lines have changed in the snippet which will now work with either calibrated or uncalibrated images. One at the top to pull the calibrated values:
getPixelSize(unit, pixelWidth, pixelHeight);
and the makeLine command now reads:
makeLine(x1/pixelWidth, y1/pixelWidth, x2/pixelWidth, y2/pixelWidth);

2 Likes

I understand. Thank you for providing a solution as well.