passing command-line parameters to imagej

Summary

The processes of passing command-line parameters to ImageJ plugins isn't the most direct. Illustrated is a simple solution to this problem, full code included.

The processes of passing command-line parameters to ImageJ plugins isn't obvious. However, one can easily send arguments to macros and macros can send arguments; thus, the simplest solution would be to make an empty macro that just accepts command-line parameters and passes them to a chosen plugin. That is outlined below.

First, i created a dummy text file—inputParameters.txt—with some parameters, let's say some values for image processing (you could just as easily put directories to be used to batch image analysis). Then cmdLineFxn.bat loops over each line in inputParameters.txt and passes it to the macro in passCmdLineParameters.ijm, which should be located in the ImageJ ~/macro folder. This macro then calls myPlugin.java (should be compiled into a .class file beforehand), which should display the numbers in the log. Of course, you can do a myriad of other things with the passed arguments (i use it to batch analyze microscope movies), but this simple example should suffice.

The code is shown below along with links to each file.

download cmdLineFxn.bat

DOS
  1. :: biafra ahanonu
  2. :: updated: 2013.10.11
  3. :: wrapper to call imagej plugin
  4.  
  5. @echo off
  6. setlocal enabledelayedexpansion
  7.  
  8. :: go line-by-line in file, get directories to analyze
  9. for /F "tokens=*" %%A in (inputParameters.txt) do (
  10.     echo %%A
  11.    :: call imagej, pass along ImageJ directory along with name of macro used to pass parameters.
  12.     javaw -Xmx62000m -Xms62000m -Xincgc -XX:+DisableExplicitGC -XX:+UseCompressedOops -Dplugins.dir="C:\Program Files\ImageJ" -jar "C:\Program Files\ImageJ\ij.jar" -macro passCmdLineParameters.ijm %%A
  13.  
  14.     ::check the exit status
  15.     echo %errorlevel%
  16.    ::display different messages depending on how ImageJ exited.
  17.     if errorlevel 4 call :WTF
  18.     if errorlevel 3 call :MISSINGFILES
  19.     if errorlevel 2 call :FOLDERERROR
  20.     if errorlevel 1 call :TIFFERROR
  21.     if errorlevel 0 call :SUCCESS
  22.  
  23.     echo ---------------------
  24. )
  25. :: error handling, done this way to allow for loop to continue correctly.
  26. exit /b
  27. :WTF
  28.     echo i don't know what went wrong.
  29.     goto ENDLOOP
  30.  
  31. :MISSINGFILES
  32.     echo what's a folder without files!?
  33.     goto ENDLOOP
  34.  
  35. :FOLDERERROR
  36.     echo folders already exists.
  37.     goto ENDLOOP
  38.  
  39. :TIFFERROR
  40.     echo .tif isn't formatted correctly.
  41.     goto ENDLOOP
  42.  
  43. :SUCCESS
  44.     echo AMERICA!
  45.     goto ENDLOOP
  46.  
  47. :ENDLOOP
  48. exit /b
download passCmdLineParameters.ijm

Java
  1. // biafra ahanonu
  2. // updated: 2013.10.11
  3. // macro to pass command-line inputs to plugin
  4. // place in ImageJ's ~/macro folder
  5.  
  6. macro "passCmdLineParameters" {
  7.         // pass command-line arguments to plugin
  8.         run("myPlugin",getArgument);
  9. }
  10.  
download myPlugin.java

Java
  1. /*
  2. biafra ahanonu
  3. updated: 2013.10.11
  4. example plugin that accepts an input parameter and prints it
  5.  
  6. ERROR CODES
  7. 0 - normal exit, everything is fine
  8. 1 - you decide
  9. 2 - see above
  10. 3 - see above
  11. 4 - WTF happened?
  12. */
  13.  
  14. import ij.*;
  15. import ij.process.*;
  16. import ij.gui.*;
  17. import ij.measure.*;
  18. import ij.text.*;
  19. import ij.plugin.*;
  20. import ij.plugin.filter.*;
  21. import ij.plugin.frame.*;
  22. import ij.io.*;
  23. import java.awt.*;
  24. import java.io.*;
  25. import javax.swing.*;
  26. import java.lang.reflect.*;
  27. import java.lang.System.*;
  28. import java.util.*;
  29.  
  30. public class myPlugin implements PlugIn {
  31.  
  32.         public void run(String arg) {
  33.                 // this function runs when the plugin is called
  34.  
  35.                 // catch any unknown exceptions
  36.                 try{
  37.                         // call the main function
  38.                         mainFxn("go");
  39.                 }catch(Exception e){
  40.                         // exit with unknown
  41.                         System.exit(4);
  42.                         return;
  43.                 }
  44.                 // exit imagej, returns control back to the command line
  45.                 System.exit(0);
  46.                 return;
  47.         }
  48.         private void mainFxn(String argj){
  49.                 // check that options actually passed
  50.                 if (IJ.isMacro() && Macro.getOptions() != null && !Macro.getOptions().trim().isEmpty()) {
  51.                         // get the arguments, put separator in trim if passed multiple arguments, e.g. '100, 20'
  52.                         String args = Macro.getOptions().trim();
  53.                         // print out arguments to command line
  54.                         IJ.log(args);
  55.                         IJ.log("starting plugin");
  56.                         /*
  57.                         Do something
  58.                         */
  59.                         return;
  60.                 }else{
  61.                         return;
  62.                 }
  63.         }
  64.         private void printTime(long startTime){
  65.                 // simple method to print the time since startTime
  66.                 double estimatedTime = (double) (System.nanoTime() - startTime)/3600000000000L;
  67.                 double hrs = (double) Math.floor(estimatedTime);
  68.                 double min = (double) Math.floor((estimatedTime - hrs)*60);
  69.                 double sec = (double) Math.floor(((estimatedTime - hrs)*60 - min)*60);
  70.                 IJ.log(hrs + " hour(s), " + min + " minute(s), and " + sec + " second(s).");
  71.         }
  72. }
  73.  

-biafra
bahanonu [at] alum.mit.edu

©2006-2024 | Site created & coded by Biafra Ahanonu | Updated 21 October 2024
biafra ahanonu