Controlling ImageJ from Scheme
I just spent ten days with the Bioimaging Group at EPFL, Lausanne. They’re great folks, and I at long last am on the way to having a segmenter for our microscope images. There’s just one catch: they do everything as plugins for ImageJ.
I never really learned to write Java. It’s basically C with classes and memory management bolted on top, so it’s not exactly hard to learn, but I don’t appreciate managing memory anymore. I’m not willing to track syntax errors through hundreds of lines of code. And most damningly, I can’t program in languages where functions are first-class objects anymore. I just can’t seem to do it. At every turn, I say, “Ah, I’ll just pass this function as an argument to…oh wait.”
Thankfully, I was able to escape. Kawa is a Scheme REPL and compiler for the Java virtual machine that let’s you call native Java objects seamlessly. You can define new classes which can be instantiated from Java. It has multidimensional arrays that you can reshape at runtime, and which are compiled to unboxed, one dimensional Java arrays.
I spent a couple hours while I was there trying to write ImageJ plugins in Kawa. I failed. Horribly. I still don’t know why. However, I realized I was going at the problem backwards. Instead I opened up Kawa, and typed (<ij.ImageJ>). Up pops ImageJ! Another couple of hours learning the details of Kawa and coding gave me full control of the program from native Scheme, and access to the image pixels in place as a native two dimensional array. Further, the view of the array in Scheme is dynamically typed, so code you write to manipulate images automatically works on all four pixel types (Byte, Short, Color, and Float).
Then I started coding in earnest. I ported Todd Eigenschink’s matrix05.scm linear algebra library to Kawa using the aforesaid native arrays. Then I started coding my own cubic B-spline library. This is reinventing the wheel, but I would have had to in Java as well, and matrix05.scm is far superior to either of the Java linear algebra libraries.
When I get it cleaned up and documented a bit more, I’ll probably release the interface so everyone else can stop beating their heads against Java without sacrificing the sizeable codebase already in ImageJ.
Leave a comment