imagej / pyimagej

Use ImageJ from Python
https://pyimagej.readthedocs.io/
Other
475 stars 82 forks source link

How can I create a ImagePlus object by pyimagej? #86

Closed lizhogn closed 3 years ago

lizhogn commented 4 years ago

In ImageJ with Python Kernel, there is a example about Running a plugin:

from jnius import autoclass
WindowManager = autoclass('ij.WindowManager')
ij.py.run_macro("""run("Blobs (25K)");""")
blobs = WindowManager.getCurrentImage()
print(blobs)

if I want to process the numpy array by imagej macro like this:

# python script
numpy_colony = io.imread('new-colony.jpg')
# Send it to java_array
java_colony = ij.py.to_java(numpy_colony)
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# How can I process above java_colony by the following macro?

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ij.py.run_macro("""run("Convert to Mask");""")
ij.py.run_macro("""run("Convert to Mask");""")

If I don't do something in the <<<>>>, it will say "There are no images open." I think there is a function which can tranlate the java_colony array into a ImagePlus object.

hfathian commented 3 years ago

I have the same issue. Did you find any solution for that?

lizhogn commented 3 years ago

I have the same issue. Did you find any solution for that?

No,I just give up😅

hfathian commented 3 years ago

I have the same issue. Did you find any solution for that?

No,I just give up😅

Look into porespy.imagej function. It may solve your problem.

lizhogn commented 3 years ago

I have the same issue. Did you find any solution for that?

No,I just give up😅

Look into porespy.imagej function. It may solve your problem.

OK,thanks.

elevans commented 3 years ago

@lizhogn Since your post we've moved pyimagej's java bridge from pyjnius to JPype so the syntax is a little different if you have revisited pyimagej since your question. Instead of using ij.py.to_java(numpy_colony) you should use ij.py.to_dataset instead. The to_java method sends the numpy array to java land and returns the java equivalent of the array. Note that the to_java method is more generic and will also take numbers, lists and other objects and convert them to java objects. The to_dataset however returns an imagej dataset which is a wrapper for the ImgPlus object

import imagej
from skimage import io

# initialize imagej
ij = imagej.init('sc.fiji:fiji')

# load image
numpy_colony = io.imread('Cell_Colony.jpg') # type: numpy.ndarray
java_colony = ij.py.to_dataset('numpy_colony) # type: net.imagej.DefaultDataset

# run macro
ij.py.run_macro("""run("Convert to Mask");""")

Alternatively, if you open your imagej with Imagej's opener instead of skimage you'll get a net.imagej.DefaultDataset object.

image = ij.io().open('Cell_Colony.jpg') # returns net.imagej.DefaultDataset

In my use I'll use to_dataset to convert images to net.imagej.DefaultDataset objects when the imagej is coming from some other python source that I don't have much control over. If I'm loading images from my local disk I'll just use ij.io().open().

lizhogn commented 3 years ago

@lizhogn Since your post we've moved pyimagej's java bridge from pyjnius to JPype so the syntax is a little different if you have revisited pyimagej since your question. Instead of using ij.py.to_java(numpy_colony) you should use ij.py.to_dataset instead. The to_java method sends the numpy array to java land and returns the java equivalent of the array. Note that the to_java method is more generic and will also take numbers, lists and other objects and convert them to java objects. The to_dataset however returns an imagej dataset which is a wrapper for the ImgPlus object

import imagej
from skimage import io

# initialize imagej
ij = imagej.init('sc.fiji:fiji')

# load image
numpy_colony = io.imread('Cell_Colony.jpg') # type: numpy.ndarray
java_colony = ij.py.to_dataset('numpy_colony) # type: net.imagej.DefaultDataset

# run macro
ij.py.run_macro("""run("Convert to Mask");""")

Alternatively, if you open your imagej with Imagej's opener instead of skimage you'll get a net.imagej.DefaultDataset object.

image = ij.io().open('Cell_Colony.jpg') # returns net.imagej.DefaultDataset

In my use I'll use to_dataset to convert images to net.imagej.DefaultDataset objects when the imagej is coming from some other python source that I don't have much control over. If I'm loading images from my local disk I'll just use ij.io().open().

That's great, thank you very much