I use ImageJ 1.x API in my Java application, where I run ImageJ in invisible mode:
ImageJ imageJApplication = new ImageJ(2);
In my Java application I generate several images and open them for the user interactions:
Opener opener = new Opener();
String imageFilePath = getLastImageFilePath();
ImagePlus imp = opener.openImage(imageFilePath);
imp.show();
I provide a user with the possibility to make a straight line selection on any of the opened images:
IJ.setTool(4);
If the user has drawn a straight line on an image, then I need to get length of this line. To do this I try to get access to the straight line selection object that was produced by the user interaction. Unfortunately I do not know where ImageJ creates and keeps this object.
I assume that it should be an object of ij.gui.Line
class (the class that extends ij.gui.Roi
). I know that to draw a Roi
type object myself in a java program I would create an ij.gui.Overlay
instance, add the Roi
object to the overlay and then set the overlay to my target instance of ImagePlus
. So I tried to look for the straight line selection objects in overlays of my opened images:
ImagePlus imp = WindowManager.getCurrentImage();
double lineLength = 0;
Overlay overlay = imp.getOverlay();
if (overlay!=null){
for (int i = 0; i < overlay.size(); i++){
if (overlay.get(i).isLine()){
Line currentImageLine = (Line) overlay.get(i);
lineLength = currentImageLine.getLength();
}
}
}
But for an image where a user has drawn a straight line the call imp.getOverlay()
in the code above returns null
. So, apparently, overlay is a wrong place to look for the user-produced straight line selection object instance. Does somebody know, how to get access to the straight line selection object?
For questions regarding the ImageJ API, usually the ImageJ forum is the best place to ask.
Does somebody know how to get access to the straight line selection object?
You were on the right track, but mislead by the ij.gui.Overlay
class (that can contain an arbitrary number of ij.gui.Roi
objects, but needs to be explicitly added to an ImagePlus
).
To get the current selection from an ImagePlus
, simply call imp.getRoi()
(see also the javadoc):
ImagePlus imp = WindowManager.getCurrentImage();
double lineLength = 0;
Roi roi = imp.getRoi();
if (roi != null && roi.isLine()) {
lineLength = roi.getLength();
}
One more comment:
ImageJ imageJApplication = new ImageJ(2);
...
IJ.setTool(4);
To keep your code more readable, use the defined constants instead of arbitrary integers:
ImageJ imageJApplication = new ImageJ(ImageJ.NO_SHOW);
...
IJ.setTool(Toolbar.LINE);