Login  Register

Re: Using API to create mask with multiple rectangles

Posted by Albert Cardona on Nov 19, 2007; 10:55pm
URL: http://imagej.273.s1.nabble.com/Using-API-to-create-mask-with-multiple-rectangles-tp3697968p3697971.html

Leonard,

You're right, the ShapeRoi doesn't have an 'add' method, but it does
have the 'or', 'and', 'not' and 'xor' methods. To add, use the 'or':
unary union operator.

What the 'or' will give you is a new Roi composed of two rectangles
,which may be intersecting each other or not. If they do, then you get a
single continuous area; if they don't, two disconnected islands.

The mask: it's meant to specify which image pixels fall under the ROI
and which don't. It's a binary.
The masks enables non-rectangular ROIs to still be able to be filled
with color properly, or measured, or whatever.

In jython:

 >>> s = ShapeRoi(Roi(100,100,300,300))
 >>> imp = IJ.getImage()
 >>> imp.setRoi(s)
 >>> s.or(Roi(500,500,300,300))
 >>> s.or(ShapeRoi(Roi(500,500,300,300)))
 >>> imp.updateAndDraw()
 >>> s.or(ShapeRoi(Roi(700,400,300,300)))
 >>> imp.updateAndDraw()

The above generates a ShapeRoi with a rectangle at top left and then two
intersecting rectangles more to lower right.

To fill it with the foreground color:

 >>> imp.getProcessor().fill(s.getMask())
 >>> imp.updateAndDraw()

The s.getMask() returns a ByteProcessor which has all its pixels as 0 or
255 (-1, in byte)


The mask, notice, is only for the pixels within the bounds of the ShapeRoi:

 >>> ip = s.getMask()
 >>> print ip
ip[width=900, height=700, min=0.0, max=255.0]

 >>> print s.getBounds()
java.awt.Rectangle[x=100,y=100,width=900,height=700]

So to find out which pixels are in and which out, you have to translate
the coordinates by the x,y of the ROI bounds.

Also: don't use ip.setColor(Color.white), which is deprecated, but the
ip.setValue().

I hope this helped. The above can be translated to java code trivially.
I used the JythonInterpreter to do it (
http://www.mcdb.ucla.edu/research/hartenstein/acardona/software.html ).

Albert

--
Albert Cardona
http://www.mcdb.ucla.edu/Research/Hartenstein/acardona