From Mathematica, I wish to pass an image to an ImageJ plugin and get the result sent from ImageJ to Mathematica.
JLink is the tool that will help, but I am familiar neither with it nor with ImageJ (making it hard to leverage existing related questions on SO). Would you know how to do that?
(I have installed ImageJ on my mac already.)
This should only serve as teaser to show you, that this is not as hard as it seems. There is no way around, that you have to be familiar with Java, the ImageJ-API, and yes, JLink.
So here is how you get started: You need the ImageJ java archive
ij.jar
which you can download from the ImageJ Website. This jar contains all classes and functions ImageJ is using by itself.What you want to do is to create an image not by opening it through the ImageJ-GUI but by using Java-methods. For this you have to get familiar with the ImageJ-API to know how you can create an image from e.g. an array of numbers, because that's how we want to transfer the Mathematica-images to Java. Skimming through this documentation brought me to the ImageProcessor classes. As you can see they provide two methods:
setPixels
andgetPixels
and both accept simple arrays. Let's use this and write a very basic filter:This function gets the image-data and the image-dimensions, does a convolution and returns the filtered image-data. To compile this, remember that
ij.process.FloatProcessor
is inside theij.jar
. You have to include this archive in your classpath. Later, you have to ensure that JLink finds both, theij.jar
and yourSimpleTest.class
. I usually pack my classes inside a jar too. For this I called itsimple.jar
.While the java-side is now ready to rock, we need a few lines to extract image-data and dimensions from a Mathematica-image
What we do now is sending the flat integer array
data
to our Java-function, taking the result and building the output-image. To make Mathematica find your jar-archives, one way is to callAppendToClassPath
:With this last step we complete the cycle and get our final result.
Final notes:
ij.jar
. This will not include all available stuff. But remember: The ImageJ-GUI just calls methods fromij.jar
to use auxiliary plugins, so you can do the same with your code.