def create_mask( binim, rgba=None, z=None, c=None, t=None, text=None, raise_on_no_mask=False): """ Create a mask shape from a binary image (background=0) :param numpy.array binim: Binary 2D array, must contain values [0, 1] only :param rgba int-4-tuple: Optional (red, green, blue, alpha) colour :param z: Optional Z-index for the mask :param c: Optional C-index for the mask :param t: Optional T-index for the mask :param text: Optional text for the mask :param raise_on_no_mask: If True (default) throw an exception if no mask found, otherwise return an empty Mask :return: An OMERO mask :raises NoMaskFound: If no labels were found :raises InvalidBinaryImage: If the maximum labels is greater than 1 """ # Find bounding box to minimise size of mask xmask = binim.sum(0).nonzero()[0] ymask = binim.sum(1).nonzero()[0] if any(xmask) and any(ymask): x0 = min(xmask) w = max(xmask) - x0 + 1 y0 = min(ymask) h = max(ymask) - y0 + 1 submask = binim[y0:(y0 + h), x0:(x0 + w)] if (not np.array_equal(np.unique(submask), [0, 1]) and not np.array_equal(np.unique(submask), [1])): raise Exception("Invalid binary image") else: if raise_on_no_mask: raise Exception("No mask found") x0 = 0 w = 0 y0 = 0 h = 0 submask = [] mask = MaskI() mask.setBytes(np.packbits(np.asarray(submask, dtype=int))) mask.setWidth(rdouble(w)) mask.setHeight(rdouble(h)) mask.setX(rdouble(x0)) mask.setY(rdouble(y0)) if rgba is not None: ch = ColorHolder.fromRGBA(*rgba) mask.setFillColor(rint(ch.getInt())) if z is not None: mask.setTheZ(rint(z)) if c is not None: mask.setTheC(rint(c)) if t is not None: mask.setTheT(rint(t)) if text is not None: mask.setTextValue(rstring(text)) return mask
def shapes(self): """Create a bunch of unsaved Shapes.""" rect = RectangleI() rect.x = rdouble(10) rect.y = rdouble(20) rect.width = rdouble(30) rect.height = rdouble(40) # Only save theT, not theZ rect.theT = rint(0) rect.textValue = rstring("test-Rectangle") rect.fillColor = rint(rgba_to_int(255, 255, 255, 255)) rect.strokeColor = rint(rgba_to_int(255, 255, 0, 255)) # ellipse without saving theZ & theT ellipse = EllipseI() ellipse.x = rdouble(33) ellipse.y = rdouble(44) ellipse.radiusX = rdouble(55) ellipse.radiusY = rdouble(66) ellipse.textValue = rstring("test-Ellipse") line = LineI() line.x1 = rdouble(200) line.x2 = rdouble(300) line.y1 = rdouble(400) line.y2 = rdouble(500) line.textValue = rstring("test-Line") point = PointI() point.x = rdouble(1) point.y = rdouble(1) point.theZ = rint(1) point.theT = rint(1) point.textValue = rstring("test-Point") polygon = PolygonI() polygon.theZ = rint(5) polygon.theT = rint(5) polygon.fillColor = rint(rgba_to_int(255, 0, 255, 50)) polygon.strokeColor = rint(rgba_to_int(255, 255, 0)) polygon.strokeWidth = LengthI(10, UnitsLength.PIXEL) points = "10,20, 50,150, 200,200, 250,75" polygon.points = rstring(points) mask = MaskI() mask.setTheC(rint(0)) mask.setTheZ(rint(0)) mask.setTheT(rint(0)) mask.setX(rdouble(100)) mask.setY(rdouble(100)) mask.setWidth(rdouble(500)) mask.setHeight(rdouble(500)) mask.setTextValue(rstring("test-Mask")) mask.setBytes(None) return [rect, ellipse, line, point, polygon, mask]