6

I am using Python to design a software, and the image processing is one of the steps. I am using ImageJ to realize this.

Since there is a Jython interpreter within ImageJ, which can be opened within ImageJ software, there must be a way to connect ImageJ to Python and call all the functions within Python.

I wonder how I can do that to finish all the processing in Python rather than open the interpreter in ImageJ?

Jarvis Du
  • 359
  • 1
  • 4
  • 13
  • Have you looked at opencv2 for python? The step you're performing with imageJ may have a built in function. – Scott Apr 20 '15 at 16:01
  • Is it based on openCV? It is not supporting Python34. – Jarvis Du Apr 22 '15 at 00:20
  • There is a beta opencv3 for python 3.4, otherwise cv2 for 2.7x. – Scott Apr 22 '15 at 01:06
  • It seems a lot of people have the problem that they can't find python 3.4 package in /builds/python after they installed 3.0.0 beta version. [Here](http://answers.opencv.org/question/60190/building-opencv-300-beta-windows-python-34/) is one of the questions. – Jarvis Du Apr 23 '15 at 18:58
  • Justification for working with cv2 and python2.7.x until all that gets worked out. – Scott Apr 23 '15 at 19:21

4 Answers4

5

There is this https://github.com/imagej/imagej.py module which provides an integration between Python and ImageJ.

With it you can easily use ImageJ in Python. Here is a sample script:

# Spin up ImageJ.
import imagej
ij = imagej.init('/Applications/Fiji.app')

# Import an image with scikit-image.
import skimage
from skimage import io
# NB: Blood vessel image from: https://www.fi.edu/heart/blood-vessels
img = io.imread('https://www.fi.edu/sites/fi.live.franklinds.webair.com/files/styles/featured_large/public/General_EduRes_Heart_BloodVessels_0.jpg')
import numpy as np
img = np.mean(img, axis=2)

# Invoke ImageJ's Frangi vesselness op.
vessels = np.zeros(img.shape, dtype=img.dtype)
import imglyb
ij.op().filter().frangiVesselness(imglyb.to_imglib(vessels), imglyb.to_imglib(img), [1, 1], 20) 
Helder
  • 428
  • 5
  • 17
2

EDIT 2018-07-02: There is now an ImageJ python module allowing to start up and use ImageJ from Python. https://github.com/imagej/imagej.py

I wonder how I can do that to finish all the processing in Python rather than open the interpreter in ImageJ?

You can write a Jython script with a special header that uses the ImageJ launcher to execute, ensuring all needed libraries are on the classpath:

#!/bin/sh
''''exec "$(dirname "$0")"/ImageJ.sh --jython "$0" "$@" # (call again with fiji)'''

But this hack only works on POSIX-compatible platforms (i.e., in the case of Windows you would need CYGWIN).

Fiji includes several example scripts like this—a short and sweet one is screenshot.py.

Note that Jython cannot call Python libraries that are backed by native code (e.g., scipy and numpy). But there is an experimental CPython script language, which uses the javabridge. It is still immature, and not distributed with Fiji yet. But it allows to call native Python code, including libraries backed by C/C++, from ImageJ. The plan is to include this language in Fiji in the future. In the meantime, you can build it yourself and drop the JAR file into ImageJ's jars folder. And of course, feedback is welcome.

If you develop a more turnkey "Python-centric" solution as you desire, it would be awesome to add a page to the ImageJ wiki about it!

ctrueden
  • 6,394
  • 3
  • 32
  • 64
  • 1
    Thank you for the advice! But I wonder how I could import "ij" package in Python? How can I add the package to sys.path? – Jarvis Du Apr 23 '15 at 18:17
2

The imglib2-imglyb project allows to access numpy arrays from Java with ImgLib2 and vice versa.

Some illustrating examples can be found on imglyb-examples.

Jan Eglinger
  • 3,876
  • 1
  • 22
  • 49
1

Depending on the size of the image, you could use the javabridge connector developed by the Cell Profiler group. It is however notoriously slow.

If you want to design your own binding to ImageJ, you can use jepp/jpype couple, but it will be a lot of work.

The easiest way of doing it would probably be to save your image from ImageJ to an external file and write a macro that would save image, start your custom python processing script, and then re-open the processed image.

he1ix
  • 384
  • 2
  • 11
chiffa
  • 1,808
  • 2
  • 24
  • 37
  • For the last method, you mean process the image offline? – Jarvis Du Apr 20 '15 at 14:08
  • 1
    @chiffa If the javabridge is really "notoriously slow" then I would recommend writing to the CellProfiler developers. The guy who developed it, Lee Kamentsky, is awesome and certainly could help address any bottlenecks. – ctrueden Apr 21 '15 at 21:59
  • @ctrueden: Thanks for the pointer. We've opted for the third solution in our case. I will however shoot an e-mail to Lee Kamentsky if I will be working on Python plugins for ImageJ in the future: the current solution is not very user-friendly. – chiffa Apr 21 '15 at 22:03
  • @chiffa Thank you for the advice! But I don't think processing offline is what I want. I tried several alternatives, including openCV(not support Python34) and simpleCV(same problem). I feel FIJI is still the one I want to connect with Python. – Jarvis Du Apr 22 '15 at 00:20
  • Image processing in Python is heavily dependent on C/Fortran routines pulled by numpy/scipy couple, so jython will have trouble pulling them. Regarding the OpenCV, it uses a bunch of meta programming trick from Python 2 and has not been properly ported to Python 3. However, in addition to Python3 incompatibility, it is also a pretty hard work to create GUI with it. – chiffa Apr 22 '15 at 00:37