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
and getPixels
and both accept simple arrays. Let's use this and write a very basic filter:
import ij.process.FloatProcessor;
public class SimpleTest {
public static float[] func(float []bm, int nx, int ny) {
FloatProcessor p = new FloatProcessor(nx,ny);
p.setPixels(bm);
float[] kernel = new float[{0.111f,0.111f,0.111f,0.111f,
0.111f,0.111f,0.111f,0.111f,0.111f};
p.convolve(kernel, 3, 3);
return (float[]) p.getPixels();
}
}
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 the ij.jar
. You have to include this archive in your classpath. Later, you have to ensure that JLink finds both, the ij.jar
and your SimpleTest.class
. I usually pack my classes inside a jar too. For this I called it simple.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
img = ColorConvert[ExampleData[{"TestImage", "Lena"}], "Grayscale"];
{nx, ny} = ImageDimensions[img];
data = ImageData[img] // Flatten;
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 call AppendToClassPath
:
Needs["JLink`"]
AddToClassPath["/pathTo/ij.jar","pathTo/simple.jar"];
simpleTest = LoadJavaClass["SimpleTest", StaticsVisible -> True];
output = SimpleTest`func[data, nx, ny];
Image[Partition[output, nx]]
With this last step we complete the cycle and get our final result.
Final notes:
- You should maybe (and this means "do it") use the same java-version JLink is using for the compilation of your code.
- There are already plugins in the
ij.jar
. This will not include all available stuff. But remember: The ImageJ-GUI just calls methods from ij.jar
to use auxiliary plugins, so you can do the same with your code.
- Some plugins use native (JNI) code which may lead to unexpected errors, when you use this from within Mathematica