Suggestions on a functional macro for specific application



I am trying to create a macro to analyze a set of experiments that will produce results that should look like the colored drawing attached to this post.

The overall goal of the macro is to determine what percentage of the pink area falls in each brown selection.

I would like the macro to automatically set the scale by first finding the center of mass of the two blue and green dots on the left side of the photo. Then it should create a line from one center of mass to the other that will be used to set the scale (as the resulting line should be ~1inch).

Then, I was thinking that the macro would use the brown lines to make an inverted mask to divide up the pink sections. Finally, the macro would then somehow measure the areas of each of the now separated section.

I have been trying to record a macro for a while but keep getting stuck because I don’t know how to pull center of mass data from the measurement window and put them back into the macro to create a line.

Someone please help!


Here is an ImageJ-macro that computes the distance between the binarized dots:

requires( "1.52d" );
run("Set Measurements...", "center redirect=None decimal=1");
img = getImageID();
setOption("BlackBackground", false);
run("Make Binary");
run("Analyze Particles...", "size=5000-Infinity display exclude");
function distance() {
       x = Table.getColumn("XM");
       y = Table.getColumn("YM");
       return sqrt((x[1]-x[0])*(x[1]-x[0])+(y[1]-y[0])*(y[1]-y[0]));

Open the test dots in ImageJ:

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

I get a distance of 420.5 pixels printed to the Log-window.

This may give you a first hint of how to proceed.




Hi Herbie,

Thanks for the help. Unfortunately when I run the macro I get an exception pop-up. How do I resolve this?


Do you run the most recent version of ImageJ which is 1.52d?

Please check and in case update!




I updated to ImageJ 1.52e and the error is still the same. Are you sure it’s 1.52d? I’m not seeing that as an update option.


ImageJ 1.52e is OK. (It is a daily build.)

Presently, I run the daily build 1.52e47 and the macro runs just fine.

Do you have a results table open before you run the macro? (Close it!)
Do you use the image I’ve posted?

You could comment the line “close(“Results”);”. Then there should be a Results table visible with two columns (titled “XM” and “YM”) and two rows.




I have no results table open when I run the macro. And I am using your exact image. When I comment out the close results line, the same error still pops up.

In my debugging process I have found that the program fails to analyze the particles, saying that “The threshold (255-255) may not be correct”.

I have also found that if I add in the line ’ run(“Measure…”); ’ above the print statement, the program outputs a table with the value 417.8.

Does this help in anyway?



I think it’s more a sizing issue with the sample image… change the minimum size in the run("Analyze Particles...", "size=5000-Infinity display exclude"); call to something like 100 or 500… And try it again. (you might have to re-measure to get an estimate for the object area of interest…)



Good day Ellen,

since the macro runs perfectly for me with the provided image, your conjecture appears to be not to the point.





I ran that same code and got the same error as the user… this is the change I made to get it to at least run.

… was just a test to try to help.



That’s funny because the dots have more than 8000 pixels^2 and of course the macro runs for me with a lower limit of say 200 pixels^2.

I fear that the original poster and perhaps you didn’t correctly download the test image. Its correct size is 1024x1536 pixels^2.





Perhaps that is the case - which is fine! :slight_smile: At least then it’s on it’s way to a solution for this particular error… @ImageMax - then make sure to not open the sample image via URL, etc … but click on it and download the image and then File > Open via ImageJ. The dimensions were read in differently - which accounted for that error in our cases.

eta :slight_smile:


This fix worked for me! Thank you both for so much help :grinning: