Numpy to ImagePlus/ImagePlus to Numpy conversion with pyimagej

Dear forum,

I’m trying to go a little further regarding the integration of ImageJ and Python functionality. Ideally, I would like to use the Spyder IDE in conjunction with pyimagej and be able to freely pass image arrays between the python environment and the ImageJ environment.

Here’s a simple example of how I would imagine my ideal usecase:

import imagej
import numpy as np

# initialize imagej
ij_dir = 'F:\\Programme\\Fiji.app\\'
try:
    ij = imagej.init(ij_dir, headless=False)
except:
    pass

# get Java class representations
BG_subtract_class = jnius.autoclass('ij.plugin.filter.BackgroundSubtracter')
BG_subtractor     = BG_subtract_class()
ImagePlusClass    = jnius.autoclass('ij.ImagePlus')

# generate some random data
frame = np.zeros((1000,1000))
frame[250:750, 250:750] = 1.0

# convert frame to Image Plus object
imp = ij.py.to_java(frame)
imp = ij.dataset().create(imp)
imp = ij.convert().convert(imp, ImagePlusClass)

BG_subtractor.rollingBallBackground(imp.getProcessor(), 100, False, False, False, False, False)

res = ij.py.from_dataset(imp)

Problem is, the resulting array “res” and the initial array “frame” are identical. So I assume that I have applied the ImageJ Backgroundcorrection correctly, I have just no idea how to grab the output in Python. Does anyone have an idea?

Thanks in advance!