Using macro to apply threshold and record settings

I will be analyzing pictures where I want to threshold out interesting objects and then use the particle plugin to count them etc. This will be done one a lot of pictures and I want to use a few different automatic threshold methods and a manual one to make sure that the findings are not dependent on a specific threshold setting.

The automatic ones are easy to do but for the manual one I want to save the settings for each file that I do to make sure that there is not a big difference between groups.

The question is: Is there a way to create a macro/script that opens the threshold windows, lets you set the values for the threshold and store them. And then continue the script. Any help that could point me in the right direction to get started would be very appreciated.

The closes I have gotten is


waitForUser("select threshold, then hit OK"); 
print("your thresholds are; "+lower, "to "+ upper);  

This does what I want but it requires the user to set the threshold press OK then hit apply. If apply is hit first it adds the mask and all value become 255. Hence, It is very sensitive to pressing buttons in the correct order.

I tried it also in javascript but then i did not even get to chose anything in the dialog box:


Why is “waitForUser()” needed at all?

// wait4threshold
do {
} while ( !is("binary") );
print("Manually Set: "+upper);

Do you mean you want the user to decide the threshold on the first image and then apply that threshold for the rest of the images?

Are you looping through your images? I would do that something like below

(i = 0; i < listOfImages.length; i++){
	if (i = 0){
		waitForUser("select threshold, then hit OK"); 
		print("your thresholds are; "+lower, "to "+ upper);  
	} else {
		setThreshold(lower, upper);


Thank you for taking time to answer.
If i do this it returns

Manually Set: 255

Even though I set the lower at 11 000 and the upper at 25 000. So the wait is there to make sure that it records the settings before i press apply as this seams to be the point where it becomes 255 instead of the value that i chose.

Can’t reproduce your result (tested with wait(10); to wait(500);).
The ImageJ-macro works for me using ImageJ v1.53h (daily) with Java 1.8.0_271 on a Mac.


1 Like

Yes this would be great. The problem is that it would be nice not to have to wait for things. Right after the Threshold i run an other plugin (Analyze particles) and as such if i do not use wait it starts before I am able to hit apply and as there are many images i want the wait to be as short (or in best case no wait to save time)

I’m not totally sure what your issue is here, you actually don’t need to press apply to run Analyze Particles, it works if there’s a threshold set on the image, HOWEVER, the command run("Convert to Mask"); does the same thing as the apply button.