Beispiel #1
0
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
Beispiel #2
0
def add_point(us, img, x, y, z):
    """ Adds a point ROI to the given image
    Args:
    us The UpdateService
    img The image (ImageWrapper)
    x x (in pixels)
    y y (in pixels)
    z z plane index
    """
    p = omero.model.PointI()
    p.x = rdouble(x)
    p.y = rdouble(y)
    p.theZ = rint(z)
    ch = ColorHolder.fromRGBA(r, g, b, a)
    p.setStrokeColor(rint(ch.getInt()))
    roi = omero.model.RoiI()
    roi.setImage(img._obj)
    roi.addShape(p)
    us.saveAndReturnObject(roi)
Beispiel #3
0
mask_x = 50
mask_y = 50
mask_h = 100
mask_w = 100
# Create [0, 1] mask
mask_array = numpy.fromfunction(lambda x, y: (x * y) % 2, (mask_w, mask_h))
# Set correct number of bytes per value
mask_array = mask_array.astype(numpy.uint8)
# Convert the mask to bytes
mask_array = mask_array.tostring()
# Pack the bytes to a bit mask
mask_packed = create_mask(mask_array, 1)

# Define mask's fill color
mask_color = ColorHolder()
mask_color.setRed(255)
mask_color.setBlue(0)
mask_color.setGreen(0)
mask_color.setAlpha(100)

# create an ROI with a single mask
mask = omero.model.MaskI()
mask.setTheC(rint(0))
mask.setTheZ(rint(0))
mask.setTheT(rint(0))
mask.setX(rdouble(mask_x))
mask.setY(rdouble(mask_y))
mask.setWidth(rdouble(mask_w))
mask.setHeight(rdouble(mask_h))
mask.setFillColor(rint(mask_color.getInt()))
    def testShapeColors(self, color):
        """Test create an ROI with various shapes & colors set."""

        color_holder = ColorHolder()
        color_holder.setRed(color[0])
        color_holder.setGreen(color[1])
        color_holder.setBlue(color[2])
        color_holder.setAlpha(color[3])
        colorInt = color_holder.getInt()
        assert colorInt == color[4]

        img = self.new_image("testCreateRois")
        img = self.update.saveAndReturnObject(img)

        roi = omero.model.RoiI()
        roi.setImage(img)

        rect = omero.model.RectangleI()
        rect.x = rdouble(5)
        rect.y = rdouble(5)
        rect.width = rdouble(100)
        rect.height = rdouble(100)
        rect.fillColor = rint(colorInt)
        rect.strokeColor = rint(colorInt)
        rect.theZ = rint(0)
        rect.theT = rint(0)
        rect.textValue = rstring("test-Rectangle")
        roi.addShape(rect)

        ellipse = omero.model.EllipseI()
        ellipse.x = rdouble(50.0)
        ellipse.y = rdouble(35.5)
        ellipse.radiusX = rdouble(2000)
        ellipse.radiusY = rdouble(300.04)
        ellipse.fillColor = rint(colorInt)
        ellipse.strokeColor = rint(colorInt)
        ellipse.theZ = rint(1)
        ellipse.theT = rint(2)
        ellipse.textValue = rstring("test-Ellipse")
        roi.addShape(ellipse)

        line = omero.model.LineI()
        line.x1 = rdouble(0)
        line.x2 = rdouble(500.9)
        line.y1 = rdouble(-100)
        line.y2 = rdouble(201.0)
        line.theZ = rint(-1)
        line.theT = rint(1)
        line.strokeColor = rint(colorInt)
        line.fillColor = rint(colorInt)
        line.textValue = rstring("test-Line")
        roi.addShape(line)

        point = omero.model.PointI()
        point.x = rdouble(1000)
        point.y = rdouble(0)
        point.strokeColor = rint(colorInt)
        point.fillColor = rint(colorInt)
        point.textValue = rstring("test-Point")
        roi.addShape(point)

        new_roi = self.update.saveAndReturnObject(roi)

        roi_service = self.client.sf.getRoiService()
        result = roi_service.findByImage(img.id.val, None)
        assert result is not None
        shapeCount = 0
        for roi in result.rois:
            assert roi.id.val == new_roi.id.val
            for s in roi.copyShapes():
                assert s.getFillColor().val == colorInt
                assert s.getStrokeColor().val == colorInt
                shapeCount += 1
        # Check we found 4 shapes
        assert shapeCount == 4