Thresholding images in batch made


Two related queries (so far). I’m attempting to threshold a batch of images. I’ve already written a macro to crop a standard-sized portion of an image , rename and save the images in a different folder.
It’s based on the Process Folder IJ1 template and adapted according to these instructions

Code here:

It works nicely (at least it works from the macro editor!), with a popup box to choose the directories then saves the correct number of files as I intended but I keep getting the following error message:

Error: Index (14) out of 0-13 range in line 22:
if ( File . isDirectory ( input + File . separator + list [ i <]> ) )

Any idea why this is happening?

My next task is to to automatically threshold each image. The AutoThreshold function works fine for this, but the threshold is different for each file. Does anyone have any suggestions how to automate this process please?

If I record a macro, it will produce something along the lines of:


setThreshold(0, 81);
SaveAs(“blah blah…”)

Is there a way of running the setThreshold function with default settings then replacing the setThreshold(0,81); line here with setThreshold(0,result_for_each_image); ?

Pretty new to this so very grateful for any suggestions

Cheers, Jeff

Hi @jefwithonef,

the macro does not work as you might expect. What happens is this:
The for loop ends after line 20

for (i = 0; i < list.length; i++) 
		action(input, output, list[i]); 

The body of the for-loop is either just the next command or the list of commands in its body that starts with ‘{’ and ends with ‘}’. In your case it’s just the action.... command. The loop runs until i is equal to the length of the list of files. That means when its finished i is equal to the length of the list, but the indices run from 0 to i-1. That is why later when you access list[i], you get the error.

I think you should just go back to the original template and do what you do in your function action in the dummy-function processFile. That is how the template is expected to be used. It opens all images in the folder and all sub-folders and you tell what to do with them by implementing the processFile function.

Best regards,

1 Like

Hi again,
as for your second question: The setAutoThreshold(“Default”); already sets the threshold it found on the image so you should not set another threshold manually afterwards. What I mean is, what you want to do with setThreshold(0,result_for_each_image); is what setAutoThreshold(“Default”); all alone already does. It automatically finds the threshold value for the current image using the Default method and sets it on the image.

Best regards,

1 Like