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

other entires to explore:

bash scripting: youtube downloading macro
17 may 2013 | programming

<p> Once again, the command line is the root of all that is good in the world. This time, it has helped improve on a long[...]-standing issue for me: what is the easiest way to get a copy of all the <a href='http://www.youtube.com/playlist?list=PLmku2swCXQpqWAZSscjV4h9bcLennVcif' target='_blank'>luscious melodies</a> i hear on youtube? Courtesy of <a href='http://rg3.github.io/youtube-dl/' target='_blank'>youtube-dl</a>, a nifty little command line utility, this problem has been solved. However, every once in awhile it throws errors and i wanted a wrapper bash script to take care of this and some other processing. I'll briefly go over the code. </p>

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.

comment system!
05 august 2012 | website

Wanted to add the ability for people to comment on this website, but delayed adding the feature until I could write the code myself. There [...]are many pre-built PHP solutions on the market (like commentator), but the original purpose of this site was to allow me to learn how to build a website from scratch. So I've implemented the comment system using about a hundred lines of code to access the MySQL database, verify inputs and display all the comments for a particular article.

graduate student resources
19 august 2015 | graduate school

Providing links to some articles and other resources that I have found useful while in graduate school. I'll continually update the list as[...] I find more.

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