ShapeRoi.or() and ShapeRoi.getContainedPoints() fail with Line (line-roi)

A ShapeRoi constructed from a Line doesn’t work properly.
In particular, .getContainedPoints() returns an empty array,
and ShapeRoi.or() (and presumably ShapeRoi.unaryOp()
in general) returns a degenerate ROI.

This is with ImageJ 2.0.0-rc-69/1.52p; Java 1.8.0_66 [64-bit].

These issues are illustrated by the following script:

from ij.gui import Line
from ij.gui import ShapeRoi

l1 = Line (10, 50, 110, 50)
l2 = Line (10, 60, 110, 60)
sl1 = ShapeRoi (l1)
sl2 = ShapeRoi (l2)

print 'l1:', l1
print 'l1-points:', len (l1.getContainedPoints())
print 'l1-bounds:', l1.getBounds()

print 'l2:', l2
print 'l2-points:', len (l2.getContainedPoints())
print 'l2-bounds:', l2.getBounds()

print 'sl1:', sl1
print 'sl1-points:', len (sl1.getContainedPoints())  # contained-points is empty
print 'sl1-bounds:', sl1.getBounds()                 # but bounds still okay

print 'sl2:', sl2
print 'sl2-points:', len (sl2.getContainedPoints())  # contained-points is empty
print 'sl2-bounds:', sl2.getBounds()                 # but bounds still okay

sl2.or (sl2)

print 'after sl2.or (sl1) ...'
print 'sl2:', sl2
print 'sl2-points:', len (sl2.getContainedPoints())  # contained-points still empty
print 'sl2-bounds:', sl2.getBounds()                 # but now bounds have collapsed

The results of this script are:

l1: Roi[Straight Line, x=10, y=50, width=100, height=0]
l1-points: 101
l1-bounds: java.awt.Rectangle[x=10,y=50,width=100,height=0]
l2: Roi[Straight Line, x=10, y=60, width=100, height=0]
l2-points: 101
l2-bounds: java.awt.Rectangle[x=10,y=60,width=100,height=0]
sl1: Roi[Composite, x=10, y=50, width=100, height=0]
sl1-points: 0
sl1-bounds: java.awt.Rectangle[x=10,y=50,width=100,height=0]
sl2: Roi[Composite, x=10, y=60, width=100, height=0]
sl2-points: 0
sl2-bounds: java.awt.Rectangle[x=10,y=60,width=100,height=0]
after sl2.or (sl1) ...
sl2: Roi[Composite, x=0, y=0, width=0, height=0]
sl2-points: 0
sl2-bounds: java.awt.Rectangle[x=0,y=0,width=0,height=0]

Thanks, mm

I had similar issues a while back. The hacky trick I used was to convert a line to a very thin rectangle, so that it would allow me to use these operations.

Hope this helps

1 Like

Hello Olivier -

Yes, I looked at that, but it only works for horizontal and vertical
lines. I also considered building ShapeRois for diagonal lines
out of the .or() of a bunch of single-pixel rectangles,
ShapeRoi (Roi (10, 10, 1, 1)) (but, upon further
consideration, I thought better of it).

Thanks, mm

Sorry, by rectangle I meant that I computed the angle of the line and created a shape roi with 4 points, not using rectangle rois directly. If you’re still interested I can dig up the code on Friday.

Best

Oli

dear @mountain_man,
yes I also think the trick is the one @oburri pointed out.
If you want you can try to use this command directly

IJ.run(imp, "Line to Area", "");

that I think will do something very similar to what oburri was proposing.

hth,
Emanuele Martini

Hello Olivier and Emanuele -

Just out of curiosity (if you remember), did you construct your
skinny-rectangle ShapeRoi from a PolygonRoi or from a
java.awt.Shape?

That makes sense. Just to note, it does look like “Line to Area”
creates a PolygonRoi for its area result.

(One last note, when I draw my Rois – including Lines – on
an image and use the ROI Manager to perform the “OR”, I get
results somewhat different from constructing a Line ROI with
code, and the result of the “OR” makes sense, rather than
being degenerate.)

Thanks, mm

This bug should be fixed in the latest ImageJ daily build (1.52q47).