imagej / imagej2

Open scientific N-dimensional image processing :microscope: :sparkler:
https://imagej.net/
BSD 2-Clause "Simplified" License
1.2k stars 337 forks source link

Starting ImageJ from a java program leaves ImagePlus detached. #212

Closed odinsbane closed 6 years ago

odinsbane commented 6 years ago

I have some programs that I run, and they start imagej while running. For example.

ImageJ.main(new String[] {});
Path path = Paths.get(IJ.getFilePath("select segmentation to label"));
ImagePlus plus = new ImagePlus(path.toAbsolutePath().toString());
plus.show();

If I run this once, everything works as expected. If I run this again while the first imagej window is still visible, the the second ImagePlus will open without an ImageJ window. I don't mind the lack of a second window, but I cannot access the second ImagePlus, eg save the file, process or adjust contrast.

I think this is a new behavior, but I am not sure. I am using ImageJ 1.51r

imagejan commented 6 years ago

It seems you're using ij.ImageJ (not net.imagej.ImageJ) in your code snippet, so this issue rather belongs to imagej1.

Why do you need to call ImageJ.main at all?

odinsbane commented 6 years ago

True, this should probably be an imagej1 issue.

I don't always like to start my programs from imagej, especially when I am developing them. But I like to have imagej running so I can do things like save and adjust contrast. Which I cannot do with just an ImagePlus window.

imagejan commented 6 years ago

I recommend using a main method in your plugin then, modeled after the one in imagej/example-legacy-plugin:

https://github.com/imagej/example-legacy-plugin/blob/6d0eb4b7e3a289acc580a0da72e21821c624ef14/src/main/java/com/mycompany/imagej/Process_Pixels.java#L163-L187

odinsbane commented 6 years ago

I am using my own main method in the example I pasted. The main difference is the new ImageJ() instead of using ImageJ.main(String[] args). Which works!

Using ImageJ.main will not start a new ImageJ window if one is running already.

Thank you

ctrueden commented 6 years ago

@odinsbane From the exchange above, I take it your issue is solved? Therefore I'm closing this issue. If I am incorrect and there is still a problem, please feel free to reopen with details.