Search code examples
wekaimagej

How can I call/execute a java program from an ImageJ macro?


What I am trying to do is to classify an image using a trained WEKA model. I am aware of the existence of the Advanced Weka Segmentation plugin of ImageJ/Fiji, but I am using a custom-made feature vector, which will not work with it.

Anyway, within an ImageJ macro I am trying to execute:

exec("java", "-cp", "/home/dionysis/weka-3-7-7/weka.jar", "weka.classifiers.trees.RandomForest",  "-T", "/home/dionysis/Desktop/CNH/cnh-data/2class-deffeat-data-20121218.arff", "-l", "/home/dionysis/Desktop/CNH/cnh-data/2class-deffeat-ranfor-classifier-20121218.model.model", "-p", "0", ">", "/home/dionysis/Desktop/testrun-data.out");

which in essence is running the terminal command for weka. In the linux terminal this is running fine, however, calling it within the macro it doesn't do anything. It does not even produce an error.

In the beginning, I thought the problem was that I was trying to run a java program on the system's JVM called from a java program running on a different JVM (ImageJ/Fiji are bundled with their own JVM). For this reason, I tried running ImageJ/Fiji using the system's JVM, but no luck.

Does anybody have any hints on this? I would greatly appreciate it... Thanks!


Solution

  • Since you are running Java already when running ImageJ/Fiji, there's no need to call the Java runtime again via exec, just use the call("Class.method", arg1, ...) macro function.

    With Weka being included in Fiji, you should be able to run any public static method like call("trainableSegmentation.Weka_Segmentation.loadData", "/home/user/data.arff"); (see here). However, if you like to interact more with the Java classes, it's advisable to use some more powerful scripting language like JavaScript or Python, both included in Fiji as well.