Closed NoahDolev closed 3 years ago
@NoahDolev Thanks for the report!
I dug into this. Calling ImageJ1-based plugins can present some challenges, depending on the plugin. ImageJ1 was not designed to work outside of the usual ImageJ application. We do our best to enable ImageJ1 code to work in a wider variety of ways, but there are still some limits.
Firstly: I cannot reproduce the "Unrecognized command" errors. This message suggests that maybe your plugins.dir
is somehow not set as it should be. What does the following script report?
It should print e.g. /Applications/Fiji.app/plugins
if your installation is /Applications/Fiji.app
.
Secondly: there are several issues with the particular plugins you are trying to use:
This plugin cannot work headless, because of this code. It will throw an exception if you try.
Here is some Python code the reproduces the issue:
And here is the resulting exception:
This plugin cannot work with virtual images opened from ImageJ2. I filed an issue for it at imglib/imglib2-ij#27.
Hi @ctrueden,
Thanks for your answer. As you suspected, indeed, the result of your script is "None" so for some reason the plugin directory is not found.
How do I fix that?
Hi @ctrueden ,
FYI, I tried:
System.setProperty('plugins.dir', '/Applications/Fiji.app/plugins')
But the plugins still don't seem to be found because I get the same error.
indeed, the result of your script is "None" so for some reason the plugin directory is not found.
Ah, this is good news! If it had been set, I would have been pretty stumped on how to troubleshoot further. 😄
How do I fix that?
The plugins.dir
system property is always set here. Do you see the message Added 375 JARs to the Java classpath.
? (Or however many plugins you have, of course.)
VM is already running, can't set options
?import imagej
first on its own line before importing other supporting libraries like jnius
or scyjava
or imglyb
?conda env create -f environment.yml
using an environment.yml
like this:
name: imagej
channels:
- conda-forge
dependencies:
- openjdk=8
- pyimagej
(Consider including scikit-image
if you want scikit-image functions. Consider including beakerx
if you want to also use Groovy notebooks in that environment. )
I tried:
System.setProperty('plugins.dir', '/Applications/Fiji.app/plugins')
But the plugins still don't seem to be found
Did you set it before creating any net.imagej.ImageJ
instances? This is rather tricky since init
both initializes the JVM and creates a net.imagej.ImageJ
in one swoop. You'd have to work a little harder to load the JVM first.
Hi,
Thanks for answering!!
I tried this:
import imagej
ij = imagej.init('<my path>/Fiji.app')
from jnius import autoclass
System = autoclass('java.lang.System')
System.setProperty('plugins.dir', '<my path>/Fiji.app/plugins/')
import imglyb
I do see Added 370 JARs to the Java classpath Even though the plugin is in the folder, still the call:
macro = """open("{}");
run("Kalman Stack Filter", "acquisition_noise=0.05 bias=0.80");
saveAs("Tiff", "{}");""".format(inpath, outpath)
ij.script().run('Macro.ijm', macro, True).get()
Gives me the unrecognized command message.
P.S. I don't see any warnings.
What does ij.getVersion()
return? As of this writing, it should say 2.0.0-rc-69/1.51i
. If it instead says 2.0.0-rc-69/Inactive
or simply 2.0.0-rc-69
then your ImageJ1 is not active.
The following code should also say 1.51i
currently:
legacy = ij.get('net.imagej.legacy.LegacyService')
legacy.getVersion()
As for calling ImageJ1 plugins from Jupyter in general, I think there are still some kinks to iron out. For example, the following code crashes the kernel on my macOS system:
Reports from others welcome. Sorry I don't have better news. Will certainly keep developing this fucntionality, though!
I was also looking into local contrast enhancements, so here is what worked for me for anyone else coming across this:
Normalize Local Contrast
is not the same as CLAHE
. While CLAHE
is a well documented algorithm, Normalize Local Contrast
seems very useful as well, but is basically undocumented. The best I could find was this announcement that it exists and its source code.For CLAHE
:
ij = imagej.init('/Applications/Fiji.app')
from jnius import autoclass
IJ = autoclass('ij.IJ')
image_plus_img = IJ.openImage(str(img_path))
Flat = autoclass('mpicbg.ij.clahe.Flat')
blockRadius = 63
bins = 255
slope = 3
mask = None
composite = False
Flat.getFastInstance().run(image_plus_img, blockRadius, bins, slope, mask, composite)
IJ.run(image_plus_img, "8-bit", "")
IJ.saveAsTiff(image_plus_img, output_filename)
For Normalize Local Contrast
:
ij = imagej.init('/Applications/Fiji.app')
from jnius import autoclass
IJ = autoclass('ij.IJ')
image_plus_img = IJ.openImage(str(img_path))
NormLocalContrast = autoclass('mpicbg.ij.plugin.NormalizeLocalContrast')
brx = 300
bry = 300
stds = 4
cent = True
stret = True
NormLocalContrast.run(image_plus_img.getChannelProcessor(), brx, bry, stds, cent, stret)
IJ.run(image_plus_img, "8-bit", "")
IJ.saveAsTiff(image_plus_img, output_filename)
PS: @NoahDolev I don't know why you'd run NormalizeLocalContrast and CLAHE in a row, they perform very similar tasks.
@NoahDolev Just an update that as of imagej/imagej-legacy#228, the plugins.dir
variable no longer needs to be set. So that may eliminate the "Unrecognized command" in your environment. I'll try to make a new release of imagej soon that includes this fix.
This issue has been mentioned on Image.sc Forum. There might be relevant details there:
I'm closing this due to inactivity. But @NoahDolev please reopen if you are still stuck and the newly released pyimagej 1.0.0 doesn't solve it for you!
Hi, I'm having a similar issue as well. The Bio-Formats
plug-in did not work with pyimagej-1.0.0
installed through conda.
import imagej
ij = imagej.init("sc.fiji:fiji:2.1.1")
image_url = "https://samples.fiji.sc/new-lenna.jpg"
ij.py.run_macro(
f"""
run("Bio-Formats Importer", "open=https://samples.fiji.sc/new-lenna.jpg autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
"""
)
plugin = "Bio-Formats Importer"
args = dict(open=image_url, rois_import="[ROI manager]", view="Hyperstack", stack_order="XYCZT")
ij.py.run_plugin(plugin, args)
Returns
[java.lang.Enum.toString] Macro Error: Unrecognized command: "Bio-Formats Importer" in line 2
run ( "Bio-Formats Importer" , "open=https://samples.fiji.sc/new-lenna.jpg autoscale color_mode=Default rois_import=[RO...[java.lang.Enum.toString]
[java.lang.Enum.toString] Macro Error: Unrecognized command: "Bio-Formats Importer" in line 1
var ; initializeSciJavaParameters ( ) ; run ( "Bio-Formats Importer" , " open=[https://samples.fiji.sc/new-lenna.jpg] r...[java.lang.Enum.toString]
<java object 'org.scijava.script.ScriptModule'>
Thanks!
I am also having this issue on Windows 10. I installed pyimagej via conda. Anyone has a fix yet?
Hi,
I have successfully setup and used Py ImageJ. However, I am not able to call upon any of the numerous plugins from python.
Returns:
However, when I attempt to run a Macro:
I receive the following error message:
The macro successfully runs from within ImageJ. Any idea why the same macro doesn't work from within Python?
Thanks.
Best, Noah