Extracting and storing Local Threshold parameters

Folks,

I’m looking for a way to extract Auto Local Threshold parameters, specifically pixel radius below, and store into a data file containing an image’s analysis results. Is there a straight forward way of doing this? I’ve looked at the list of macros and the only thing I saw was retrieving current threshold method. However, I would like to retrieve radius, parameter_1, etc. so I can trace an images analysis to specific thresholding.

Many thanks.

Neil

run("Auto Local Threshold", "method=Phansalkar radius=250 parameter_1=.5 parameter_2=.5 white");
		setOption("BlackBackground", false);
		run("Convert to Mask");
		run("Remove Outliers...", "radius=1 threshold=50 which=Bright");
		run("Despeckle");
		saveAs("tiff",dir2 + baset + "_a" + fil_num + "__bin"); 			

Hi Neil,
Perhaps I am missing something, but these are parameters that are defined when calling the Auto Local Threshold (as in your sample script). So, they are known and I don’t quite understand why you are trying to retrieve them as they are already in the code.
If you add the parameters as variables to the code (e.g. to try different values), the variables could then be used somewhere else to write them to a record file or add them as metadata to the image file.
Here is an example that defines variables for the three local threshold parameters, runs the local threshold command and then adds the parameters to the image metadata. You can view the metadata by using Image → Show Info.

r=250;
p1=0.5;
p2=0.5;
run("Auto Local Threshold", "method=Phansalkar radius="+r+" parameter_1="+p1+" parameter_2="+p2+" white");
setMetadata("Local Threshold Parameters - Radius: "+r+", Parameter 1: "+p1+", Parameter 2: "+p2);

Not sure whether this is what you had in your mind, but hope it helps,
Volko

1 Like

Volko,

Many thanks!! That is what I’m after. To your question, some users of my code will want to make adjustments to the radius value. I will be getting the results of the analysis, but will have to check what the user used for the radius. Therefore, I wanted to track this parameter.

The updated code is as follows. I had implemented initially without the “+r+”, and it still works. I definitely understand the r variable, but for my education, what does the “+r+” operation do? As I said I still get it working without it, but would like to know its purpose.

Neil

rad = 250; //***//***// // adjust pixel radius for local threshold and storage run("Auto Local Threshold", "method=Phansalkar radius=rad parameter_1=.5 parameter_2=.5 white");

Hi Neil,
The run command in an ImageJ macro takes two strings as input - the first is the command name and the second string contains all the parameter options for that command (see: https://imagej.nih.gov/ij/developer/macro/functions.html#R for the documentation).
Just including the variable name in the option string doesn’t pass the value of the variable to the option string. So, you need to insert the value of the variable into the option string by concatenating it with the rest of the option string. Here, “method-Phansalkar radius=” is the first constant part of the string, +r+ then adds the value of the variable rad to the string, which is then followed by the next constant part of the string , here " parameter_1=" and so on.
So, just inserting the name of the variable doesn’t work and wouldn’t pass the value of the variable as an option. The reason that it appears to work in your example is that the Phansalkar method doesn’t use the radius parameter as an option (it is just ignored), but only uses parameter_1 and parameter_2, which are both hard-coded in the option string in your example (see Auto Local Threshold - ImageJ for an explanation which parameters are used by the various local threshold methods).
Instead of the “string”+var+“string” method to insert the value of a variable into a string, it is also possible to use &var in a string to pass on values of a variable (see the ArgumentPassingDemo macro , https://imagej.nih.gov/ij/macros/ArgumentPassingDemo.txt), but for some reason this doesn’t appear to be working with the the setMetaData() command.
I hope that my explanation makes some sense,
Volko

1 Like

Update: &var only works with option strings in the run() function - see separate discussion here: Problem with @var to insert variable value into string - #6 by Volko

1 Like

Volko,

I can’t express enough gratitude for the valuable time you invested in my question. Some of the “run” commands I’ve used so far have been using the recording option. So I’m thrilled that you re-oriented me to the macro list to understand the finer details, and help enhance my scripting toolset. I verified my output by:

  1. getting baseline output by manually changing r value (used two distinct values) in run command, then…
  2. Used variable and concat ("" + “”) approach to verify above values in step 1
  3. Used variable and & approach to verify above values in step 1.

Everything aligns and a great learning experience.

Many thanks again!!!

Neil