Changing intensity calibration by loading a Calib.txt

I have multiple Rodbard calibrations saved in Calibration text files and need to switch between them in the course of one macro.
When loading the calibration file the content of the file gets hardcoded into the macro as text1 and text2. When my calibration file changes I can load my new calibration file. But the generated calibration still is based on the old calibration that was cemented into text1 and text2.

    setBatchMode(true);
    run("Calibrate...", "open=[G:\\testout\\calibration14bad .txt] function=Rodbard unit=[Gray Value] text1=[61655.867 33259.547 30635.748 25851.332 11491.261 8069.031 5715.9487 4028.8975 2820.229 1997.9462 1397.5448 2255.6309 623.3149 423.29984 269.10858 ] text2=[0.0 0.14405705 0.29035884 0.58565223 0.73256266 0.88338906 1.0319473 1.1825278 1.329682 1.4793421 1.625644 1.7744467 1.9239843 2.0706513 2.221109 ] global show");
    close();
    print("14closed");
    selectWindow("100xV_0003.tif");
    run("Calibrate...", "open=G:\\testout\\calibration15good.txt function=Rodbard unit=[Gray Value] text1=[61655.867 43259.547 30635.748 15851.332 11491.261 8069.031 5715.9487 4028.8975 2820.229 1997.9462 1397.5448 955.63086 623.3149 423.29984 269.10858 ] text2=[0.0 0.14405705 0.29035884 0.58565223 0.73256266 0.88338906 1.0319473 1.1825278 1.329682 1.4793421 1.625644 1.7744467 1.9239843 2.0706513 2.221109 ] global show");
    close();
    print("15closed");
    // ***calibration15good.txt is edited here from elsewhere.***
    selectWindow("100xV_0003.tif");
    run("Calibrate...", "open=G:\\testout\\calibration15good.txt function=Rodbard unit=[Gray Value] global show");
    print("this should be the new 15");
    setBatchMode(false); 

If calibration15good.txt is later edited with even better values (or better matched to the current measurement) The run(“calibrate…” still relies on text1 and text2. Even when I try to remove them from the command or empty out the numbers or ….
So, what might be a workaround to call saved intensity calibrations in the middle of a macro and actually use the content of the current file?
Later I thought it may be related to not having setbatchmode but it did not make any difference.

Win7 64-bit ImageJ 1.51g

I can only make a guess. When I record a calibration macro and reopen it then the calibration values belong to the macro. In your last calibration step the text1 and text2 values are missing.

For me it looks like this (maco recorded):

run("Calibrate...", "open=I:\\Calibration\\calibration.txt function=[Rodbard (NIH Image)] unit=[Gray Value] text1=[1.0 100.0 200.0 ] text2=[10.0 15.0 20.0 ] show");

@Wayne I would also expect that open the file as an argument would be sufficient for a calibration.

You can easily reproduce this in the macro recorder.

See also: https://imagej.nih.gov/ij/docs/guide/146-30.html#toc-Subsection-30.9

1 Like

The third run calibrate of the original post is not from the recorder I edited it in an attempt to make the macro actually use the numbers in the TXT and not the obsolete numbers in the table. I assumed the TXT is first loaded and then overwritten by the text arrays.
But still the macro calibrate window would remember the prior calibration values and not replace with the values in the file. I have tried many variations of trying to remove the content of the table. This was just one example. I was thinking maybe it actually loads Calib.TXT and then overwrites the fresh calibration with the old calibration in the text1 text2 arrays.
I tried ot create intermediate calibration that does not require any values and then load the calib.txt.
Again the values of the current state of the TXT are hardwired into the code.
Deleting them out of the results in an error that the table is empty.
I think that proofed that the TXT is never actually loaded.
I will try to manipulate the Calibrate window from an external AHK macro.
Is there any way to start an external program or macro or send something to the DOS command prompt from within ImageJ macro language?

run("Calibrate...", "function=[Pixel Inverter] unit=[Gray Value] text1=[ ] text2=[ ] global show");
selectWindow("100xV_0003.tif");
run("Calibrate...", "open=G:\\testout\\calibration15good.txt function=Rodbard unit=[Gray Value] text1=[ ] text2=[ ]  global show"); //empty text arrays ==> ERROR
run("Calibrate...", "open=G:\\testout\\calibration15good.txt function=Rodbard unit=[Gray Value] text1=[ ] text2=[ ]  show"); //leaving global out does not change anything -->same ERROR
run("Calibrate...", "open=G:\\testout\\calibration15good.txt function=Rodbard unit=[Gray Value] show"); //leaving global and the text1 text2 out does not change anything -->same ERROR

Yes you can do that with exec, see:

https://imagej.nih.gov/ij/macros/ExecExamples.txt

I would need something like “exec and wait”. Does that exist?
Maybe, I first need to create a Dummy file in ImageJ; then have that file deleted as a last step in the AHK macro that copies in the calibration values. At the same time have ImageJ parked in a ‘do while’ statement that checks if “file exists”.

i = 0;
   do {
      print(i);
      i = i + 1;
   } while File.exists(myDir);
print(“done AHK”)

Please see
https://imagej.nih.gov/ij/developer/macro/functions.html#S
It looks to me that

saveAs("Measurements",

has a problematic format string but the saveAs command apparently works
What is the difference between

saveAs("Measurements",

and

saveAs("results",

The macro recorder gives me

saveAs("Results",

Are there other format arguments?
Is capitalization in imageJ arguments important?
Is
https:\imagej.nih.gov/ij\developer\macro/functions.html
possibly an obsolete place to look up syntax?

I still can not get

exec("cmd 

to do anything. I do not get an error or a DOS box and Excel does not open.
I first believed it is a user rights issue so I run FIJI as Administrator.
Then I experimented extensively with the exec here are some results:

if (nResults==0) exit("Results table is empty");
{
   path = getDirectory("home")+"Results.csv";
   print(path);
   saveAs("Measurements", path);
   exec("open", path);//this opens the file in Excel
   exec("cmd", "/c", "start", "excel.exe", path); // this does not do anything
   print("exampleFINISHED");
   exec("cmd", " /c ", " start ", " excel.exe ", path); // giving it some space does not help
   print("exampleSpaced");
   exec("cmd"); // this Prints the Windows version into the log file. But there is no DOS box
   print("PureCMD");
   exec("open", "http://rsb.info.nih.gov/ij/"); // that worked
   output = exec("cmd","dir /w"); 
   print ("output: ", output); // this prints nothing but the windows version into the log file. 
} // curly brackets for the if do not help

I read


(Any links to .nabble.com are blocked for us.)
Ideally it would still be desirable to have the calibration loaded from a TXT.

PS: If “Measurements” is an error than someone with editing rights to
https://imagej.nih.gov/ij/macros/ExecExamples.txt
should fix that.
I do not understand if the --if-- actually would need Curly Brackets?

I think exec is not appropriate for your calibration which should be run simply by opening and load the file.

This for example works for me:

run("Blobs (25K)");
makeRectangle(69, 68, 116, 81);
run("Calibrate...", "function=[Rodbard (NIH Image)] unit=[Gray Value] text1=[1.0 100.0 200.0 ] text2=[10.0 140.0 230.0 ] ");
run("Measure");
run("Calibrate...", "function=[Rodbard (NIH Image)] unit=[Gray Value] text1=[1.0 100.0 200.0 ] text2=[5.0 12.0 20.0 ] ");
run("Measure");
run("Calibrate...", "function=[Rodbard (NIH Image)] unit=[Gray Value] text1=[1.0 100.0 200.0 ] text2=[30.0 120.0 222.0 ] ");
run("Measure");

As long as I stay in the value range of the image type.

At design time the good values for text1 and text2 are not known. They should be loaded from a file or need to be pasted in to the GUI externally.

I guess this function has a bug.

As a workaround the text1, text2 values could be loaded from a text file within the macro, see for example:

http://imagejdocu.tudor.lu/doku.php?id=macro:multiple_points

https://imagej.nih.gov/ij/macros/Import_Results_Table.txt

So far I have this macro snippet and it seems to work.
If someone else has a similar problem this is perhaps helpful.

Thank you very much for your help.

// Import Calibration table that was saved with Analyze>Calibrate>Save 
//
// This macro populates the calibration table with two strings reconstructed 
// from the saved TAB delimited CALIB file saved from the same window

// Grayvalues in left column become Text1
// OD values in right column become Text2 

	pathfile= "G:\\testout\\calibration14bad.txt";
	calibUnit ="Gray Value";
	calibGlobal = "global";
	calibShow = "show";

CalibFromFile(pathfile, calibUnit, calibGlobal, calibShow )

function CalibFromFile(pathfile, calibUnit, calibGlobal, calibShow){
     lineseparator = "\n";
     cellseparator = ",\t";
	text1 = "["
	text2 = "["
     // copies the whole Table to an array of lines
     lines=split(File.openAsString(pathfile), lineseparator);

     // dispatches the data into the text parmeters
     for (i=0; i<lines.length; i++) {
        items=split(lines[i], cellseparator);
        text1= text1+items[0]+" ";
        text2= text2+items[1]+" ";
     }
      text1= text1+"]";
      text2= text2+"]";
     updateResults();
print("text1: " + text1) //Format like this "[1.0 100.0 200.0 ]";
print("text2: " + text2) //="[5.0 12.0 20.0 ]";
calibString ="function=Rodbard unit="+calibUnit+" " +text1 +" " +text2+ " " + calibGlobal+" "+ calibShow;
run("Calibrate...", calibString) //"function=Rodbard unit=Gray Value text1 text2  show");
}