passing command-line parameters to imagej


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 (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

  1. :: biafra ahanonu
  2. :: updated: 2013.10.11
  3. :: wrapper to call imagej plugin
  5. @echo off
  6. setlocal enabledelayedexpansion
  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
  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
  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
  32.     echo what's a folder without files!?
  33.     goto ENDLOOP
  36.     echo folders already exists.
  37.     goto ENDLOOP
  40.     echo .tif isn't formatted correctly.
  41.     goto ENDLOOP
  43. :SUCCESS
  44.     echo AMERICA!
  45.     goto ENDLOOP
  47. :ENDLOOP
  48. exit /b
download passCmdLineParameters.ijm

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

  1. /*
  2. biafra ahanonu
  3. updated: 2013.10.11
  4. example plugin that accepts an input parameter and prints it
  7. 0 - normal exit, everything is fine
  8. 1 - you decide
  9. 2 - see above
  10. 3 - see above
  11. 4 - WTF happened?
  12. */
  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*;
  23. import java.awt.*;
  24. import*;
  25. import javax.swing.*;
  26. import java.lang.reflect.*;
  27. import java.lang.System.*;
  28. import java.util.*;
  30. public class myPlugin implements PlugIn {
  32.         public void run(String arg) {
  33.                 // this function runs when the plugin is called
  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. }



comments powered by Disqus

additional articles to journey through:

dealing with variable options (varargin) in matlab
17 february 2014 | programming

There are various ways to handle variable input arguments (varargin) in Matlab. I present the standard method i use that is relatively flex[...]ible and allows for readable code.

USA Sympathy
12 October 2011 | designs

Removing the 'Sympathy Bombs' changes the overt meaning, but the message stays the same.[...]

filugori reboot
15 may 2012 | filugori

Several months ago I took a hard look at Filugori: The Long Tale, the story I started in grade school that was meant to be a mash-[...]up of my favorite books and fictional universes. However, it lacked a certain vision. The story was fun, frantic and fanciful, but there was no heart. It lacked cohesion and the universe did not appear to justify its own existence. Why should someone care to read this tale? What would they gain from it? While fleshing out the background of the universe, providing details on the four major epochs that define the story, I came to realize that I wanted to tell a very different tale than originally planned.

bio42: notes
12 may 2013 | teaching

<p>While teaching bio42 (cell biology and animal physiology) I created weekly notes to help students in my section study [...]and focus on the important materials presented in the class. I built off of the latex boilerplate that I have been improving over time to create weekly notes. This highlights why I love <a href='' target='_blank'>LaTeX</a> so much, especially for larger projects that are heavily linked&mdash;it allows easy annotation, indexing, creation of new document styles, and other related processes rapidly and consistently. Plus, separating content and style is always a plus and images stay uncoupled from a propriety source (e.g. Word files). </p> <p>I really love the resulting notes and student feedback was quite positive. I thought sharing them might be useful for others in the future. The source latex files and raw images can be sent upon request (I'm considering making a Github repository in the future). I'll briefly talk about the document below and certain decisions that were made to get it to its current state. </p>
©2006-2017 | biafra ahanonu | updated 19 june 2017
biafra ahanonu