Is it possible to fill in a non trivial ROI

Hello,

I am trying to fill in a non trivial ROI, that’s not an oval, polygon, or rectangle, it is also not entirely enclosed. There are some breaks in the edges.

is it still possible to fill in any ROI regardless of shape?

from ij import IJ, ImagePlus
from ij.process import FloatProcessor
from ij.process import ImageProcessor
from ij.gui import Roi
imp = IJ.getImage()

ip = imp.getProcessor().convertToFloat() # as a copy # Grab the image processor converted to float values

The pixels points to an array of floats

pixels = ip.getPixels()
roi = RoiManager(1); #this is the ROI in the Roimanager

roi = RoiManager(1);
ip.setRoi(roi)
ip.setValue(2.0)
ip.fill()

there is an error thrown with ip.setRoi(roi)

  • TypeError: setRoi(): 1st arg can’t be coerced to ij.gui.Roi, java.awt.Rectangle, java.awt.Polygon

it’s bizarre that it requires just a rectangle or polygon, any suggestions? \

Thanks.

Hello Iet -

It should be. Ultimately, all Rois (Roi and its many subclasses)
map to a collection of pixels.

(But if “[t]here are some breaks in the edges”, you might not get
what you expect or want.)

The problem is that your variable roi isn’t an Roi, but rather
an RoiManager, as illustrated by this tweaked version of your
jython code that prints out roi's type:

from ij import IJ, ImagePlus
from ij.process import FloatProcessor
from ij.process import ImageProcessor
from ij.gui import Roi
imp = IJ.getImage()

ip = imp.getProcessor().convertToFloat() # as a copy # Grab the image processor converted to float values

pixels = ip.getPixels()

from ij.plugin.frame import RoiManager  # need this import
roi = RoiManager(1); #this is the ROI in the Roimanager

print type (roi)

When you get that fixed, it should become more clear what you’re
asking (if things still don’t work).

Thanks, mm

The simple thing I am asking is filling a ROI using one of ROIs in the ROI manager by calling on it. I did a test and make a ROI with a simple rectangle the error is still thrown, even with the fix.
In other words, how can I set a roi from the roi manager?

Hello Iet -

I’m not entirely sure what you are asking.

I think there are potentially two questions: First, how does one
fill in a non-trivial Roi (and what does that mean)? Second, how
do you get your code to work?

Let’s start with the second: Could you post a complete,
nearly-working script that demonstrates the bad result or error?
Tell us what happens that is wrong (post output, if available) and
what you wanted to have happen. (Ideally the script will be fully
self-contained. For example, your script might create a sample
image, add an Roi to it, add that Roi to the RoiManager, and
then show us what goes wrong when you try to retrieve the Roi
from the RoiManager and process it.)

For the first question, please post a semi-realistic sample image
with the desired (non-trivial) Roi on it as an overlay. And then
post (hand-generated, if necessary) a sample image that shows
(approximately) the result you want when the Roi is filled in.

Right now it is hard to know what you are looking for. But with
those kinds of details you can probably get useful suggestion
from several forum participants.

Thanks, mm

Here is one example how to fill a ROI of the ROI Manager with Jython. It also opens the ROI manager if not available (many examples missing this part).

from ij import IJ, ImagePlus
from ij.process import FloatProcessor
from ij.process import ImageProcessor
from ij.gui import Roi
from ij.plugin.frame import RoiManager  # need this import

imp = IJ.openImage("http://wsr.imagej.net/images/Cell_Colony.jpg");
imp.show()
imp.setRoi(0,0,75,70);
rm = RoiManager.getInstance();
if rm is None:    
     rm = RoiManager();# Open the ROI Manager if not opened already!
rm.addRoi(imp.getRoi());
selected = [0]#An array with indices. Here with one index!
rm.setSelectedIndexes(selected)
IJ.run(imp, "Fill", "slice");
1 Like

Yes, my apologies. Sorry for the confusion.
Here’s my screen. So I manually made a ROI on an image.

Then, I want to apply this script to the image.
here’s the image
rubberband.tif (2.1 MB)

from ij import IJ, ImagePlus
from ij.process import ImageStatistics as IS
from ij import WindowManager as WM
from ij import ImagePlus
from ij.process import FloatProcessor
from ij.gui import Roi
from ij.process import ImageProcessor

imp = IJ.getImage()
ip = imp.getProcessor().convertToFloat()
pixels = ip.getPixels()

from ij.plugin.frame import RoiManager

roi = RoiManager(1);
ip.setRoi(roi)
ip.setValue(255)
ip.fill(roi)

what i expect to see is the inside of the rubberband filled with white pixels of intensity value 255. This is not the abnormal shape I am speaking of, but to solve the first question of setting a ROI from the ROI Manager as the active roi on the image.

the error that is thrown is this:
TypeError: setRoi(): 1st arg can’t be coerced to ij.gui.Roi, java.awt.Rectangle, java.awt.Polygon

thanks for responses. Greatly appreciate it.

It makes a new ROI and opens the ROI mananger and adds the new ROI. There seems to be a disconnect when selecting the ROI in the ROI mamanger.

selected = [1]#An array with indices. Here with one index!

Why did you make the index 1, why can’t 1 be inserted into the rm.setSelectedIndexes(1). Also does 1 point to the first roi in the roi manager by ascending order or by name?

Yes, it is an easy to use reproducible example. Apropos there is a small typo. If you have only one ROI (in the example) than the index (first ROI in the ROI Manager) is 0. I’ve corrected the typo.

Why did you make the index 1, why can’t 1 be inserted into the rm.setSelectedIndexes(1).

It shows more clearly that an array is given as argument. You can also write:
rm.setSelectedIndexes([0]) or rm.setSelectedIndexes([0,1]) for two selections (see API below).

Apropos select would also be valid: rm.select(0)

Also does 1 point to the first roi in the roi manager by ascending order or by name?

Ascending.

For more answers you can explore the ImageJ API:

https://imagej.nih.gov/ij/developer/api/ij/plugin/frame/RoiManager.html

My tip. Use the macro recorder with JavaScript to record GUI actions in ImageJ. The commands are quite similar and let you explore the ImageJ API.

Hello Iet -

You still have the same problem as in your original post.

This line:

roi = RoiManager(1);

sets roi to an object of type RoiManager, not to an object of
type Roi. Therefore you get the TypeError – you are trying
to pass an RoiManager into a method that is expecting an Roi.

The code @Bio7 posted shows a very straightforward way to
use the RoiManager to fill your Roi. That code (after fixing
the index-1 vs. index-0 typo) appears to me to do what you
want.

Thanks, mm

It’s kind of silly, but I didn’t notice that it was filling in the ROI with black pixels, so I wasn’t able to distinguish the change. but yes it works great.

I couldn’t figure out how to set the fill color but now I got it. Thanks again.