0

I'm using ImageJ's Java API and need to calculate some data based on multiple selected ROIs (regions of interest).

First I get an instance of the current ROI Manager by using

RoiManager roiMng = RoiManager.getInstance();

Then, I get all ROIs in the manager by using Roi[] rois = roiMng.getRoisAsArray(); .

From here, I need to get the image in the ROI and get some data from it. However, I seem to only get Null back when calling getImage() on a ROI.

Doing something like,

Roi roi = rois[0];
ImagePlus foo = roi.getImage();
int height = foo.getHeight();

gives me 'java.lang.NullPointerException' at the foo.getHeight() line.

Anyone got any ideas as to what may be going here?

Thanks!

user2402616
  • 1,434
  • 4
  • 22
  • 38

1 Answers1

1

you need to set the roi on the ImagePlus . Then you can duplicate the part of the image defined by the active ROI by calling the duplicate() method.

ImagePlus imp = IJ.getImage(); // get the (current) image from the active/selected window
...
imp.setRoi(rois[0]);
ImagePlus roiImp = imp.duplicate();

This way you will get the image defined by the ROI's bounding box. The duplicate methods either creates a copy of the entire image or the roi-image, if a ROI is set.

In case you do not need the pixel data, but you need to get ROI based stats, you might want to have a look at https://imagej.nih.gov/ij/developer/api/ij/ImagePlus.html#getStatistics-- and https://imagej.nih.gov/ij/developer/api/ij/gui/Roi.html#getStatistics--

hope that helps

Felix

felix the cat
  • 165
  • 2
  • 9