imagej / pyimagej

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

Integrating pyImagej with ImJoy website #147

Open giovanniani opened 2 years ago

giovanniani commented 2 years ago

I'm looking to integrate pyImagej with a website we are working on with ImJoy, and would like to see how we can send the data from the results back to the website, we already can call macro functions from the website but we still haven't been able to get the results back.

Thanks

ctrueden commented 2 years ago

@giovanniani Do you have a minimal example somewhere we can look at, to better understand what you are trying to do?

CC @oeway

oeway commented 2 years ago

Hi @giovanniani I think what you are looking for is something like this:

import scyjava as sj
WindowManager = sj.jimport('ij.WindowManager')

# ...run your macro with your image

# Get the image of your current window
current_image = WindowManager.getCurrentImage()

# Convert your image to a numpy array
current_image_ndarray = ij.py.from_java(current_image)

# Display the ndarray in ImJoy e.g. using Kaibu: https://kaibu.org/docs/#/api?id=view_imageimage-options
ctrueden commented 2 years ago

@giovanniani Please close if @oeway's code will work for you, or give more details if not. Thanks!

giovanniani commented 2 years ago

@oeway thanks for the help with the issue, I have been trying to solve some issues I encountered with the scyjava library.

I hope I can solve them soon and close this issue. @ctrueden I apologyse for my late reply, as soon as I find a solution to the library I will close the issue.

Thanks

giovanniani commented 2 years ago

So I tried running the code above but I keep geeting the next issue Class ij.WindowManager is not found.

ctrueden commented 2 years ago

@giovanniani How are you initializing your ImageJ2 gateway? Try this:

import imagej
ij = imagej.init()

It should spin up an ImageJ2+ImageJ environment, which will include ij.WindowManager.

Double check you are running the latest pyimagej. It should be 1.0.2. (import imagej; print(imagej.config.__version__))

In the next release of pyimagej (1.1.0), you will also have direct access to the WindowManager class by writing ij.WindowManager from there. But for the current release, you can use the suggestion above:

from scyjava import jimport
WindowManager = jimport('ij.WindowManager')
giovanniani commented 2 years ago

I was using a local version of Fiji

I believe that's why it didn't work for me on the first time. Now with ij = imagej.init() I don't get the error.

Do you think we can solve this with any workarounds or I should stick to the normal init?

ctrueden commented 2 years ago

@giovanniani A local version of Fiji should certainly work; you should have jars/ij-1.53f.jar in your Fiji folder, which contains all the original ImageJ classes, including ij.WindowManager. No idea why you wouldn't be able to load that class when wrapping a local installation.

As I said above: if you could please post a minimal example demonstrating the problem, that would be very helpful in trying to reproduce and troubleshoot.

giovanniani commented 2 years ago

This is the code that I've been running We have a local version of Fiji that runs the macro below,

The data is mounted on a separate drive

I can send the data as well if needed.

I've been running the code inside a virtual machine with ubuntu 20.04

import imagej
import scyjava as sj

ij = imagej.init('/home/giovanni/Documents/Fiji.app')

scyjava.config.add_option('-Xmx6g')
WindowManager = sj.jimport('ij.WindowManager')

macro = """
open("/run/user/1001/gvfs/smb-share:server=truenas.local,share=data_storage/giovanni/test/a.tif");
run("3D Objects Counter", "threshold=407 slice=151 min.=10 max.=19857408 exclude_objects_on_edges objects surfaces centroids centres_of_masses statistics summary");
selectWindow("Surface map of a.tif");
selectWindow("Centroids map of a.tif");
selectWindow("Centres of mass map of a.tif");
selectWindow("a.tif");
"""
result = ij.py.run_macro(macro)

ij.window().getOpenWindows()
ij.window().clear()
giovanniani commented 2 years ago

@ctrueden @oeway Do you think we could have a meeting one day to discuss the issues I've been facing?