Example #1
0
    def test_empty_mask_from_binary_image(self, args, raise_on_no_mask):
        empty_binary_image = np.array([[0]])
        if raise_on_no_mask:
            with pytest.raises(NoMaskFound):
                mask = mask_from_binary_image(
                    empty_binary_image,
                    raise_on_no_mask=raise_on_no_mask,
                    **args)
        else:
            mask = mask_from_binary_image(empty_binary_image,
                                          raise_on_no_mask=raise_on_no_mask,
                                          **args)
            assert unwrap(mask.getWidth()) == 0
            assert unwrap(mask.getHeight()) == 0
            assert unwrap(mask.getX()) == 0
            assert unwrap(mask.getY()) == 0
            assert np.array_equal(mask.getBytes(), [])

            if args:
                assert unwrap(mask.getTheZ()) == 1
                assert unwrap(mask.getTheC()) == 2
                assert unwrap(mask.getTheT()) == 3
                assert unwrap(mask.getTextValue()) == 'test'
            else:
                assert unwrap(mask.getTheZ()) is None
                assert unwrap(mask.getTheC()) is None
                assert unwrap(mask.getTheT()) is None
                assert unwrap(mask.getTextValue()) is None
Example #2
0
    def test_mask_from_binary_image(self, binary_image, args):
        im = self.conn.createImageFromNumpySeq(iter([binary_image]),
                                               'omero-rois-integration')
        # Reload
        im = self.conn.getObject('Image', im.id)
        px = im.getPrimaryPixels().getPlane()

        mask = mask_from_binary_image(px, **args)
        mask = self.save_and_return_masks(im, [mask])[0]

        # The rest of this is more or less the same as the unit test

        assert unwrap(mask.getWidth()) == 2
        assert unwrap(mask.getHeight()) == 2
        assert unwrap(mask.getX()) == 1
        assert unwrap(mask.getY()) == 1

        assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8),
                              np.array([224], dtype=np.uint8))

        if args:
            assert unwrap(mask.getTheZ()) == 1
            assert unwrap(mask.getTheC()) == 2
            assert unwrap(mask.getTheT()) == 3
            assert unwrap(mask.getTextValue()) == 'test'
        else:
            assert unwrap(mask.getTheZ()) is None
            assert unwrap(mask.getTheC()) is None
            assert unwrap(mask.getTheT()) is None
            assert unwrap(mask.getTextValue()) is None
Example #3
0
def create_rois(im, labels):
    """
    Create single-Z masks for all labels and Zs.
    Combine all Zs for a given label into one ROI.
    """
    nz = im.getSizeZ()
    assert im.getSizeT() == 1
    if not all(
            np.equal(
                labels.shape,
                [im.getSizeZ(), im.getSizeY(),
                 im.getSizeX()])):
        raise ValueError(
            'Incompatible ZYX dimensions image:{} labels:{}'.format(
                [im.getSizeZ(), im.getSizeY(),
                 im.getSizeX()], labels.shape))

    rois = []
    ulabels = np.unique(labels)
    for n in range(1, len(ulabels)):
        print('Creating ROI index {}'.format(n))
        roi = omero.model.RoiI()
        roi.setName(rstring(n))
        for z in range(nz):
            try:
                mask = mask_from_binary_image(labels[z] == ulabels[n],
                                              rgba=RGBA,
                                              z=z)
                roi.addShape(mask)
            except NoMaskFound:
                pass
        rois.append(roi)

    return rois
Example #4
0
    def test_empty_mask_from_binary_image(self, args):
        empty_binary_image = np.array([[0]], dtype=np.uint8)
        raise_on_no_mask = False
        im = self.conn.createImageFromNumpySeq(iter([empty_binary_image]),
                                               "omero-rois-integration")
        # Reload
        im = self.conn.getObject("Image", im.id)
        px = im.getPrimaryPixels().getPlane()

        mask = mask_from_binary_image(px,
                                      raise_on_no_mask=raise_on_no_mask,
                                      **args)
        mask = self.save_and_return_masks(im, [mask])[0]

        # The rest of this is more or less the same as the unit test

        assert unwrap(mask.getWidth()) == 0
        assert unwrap(mask.getHeight()) == 0
        assert unwrap(mask.getX()) == 0
        assert unwrap(mask.getY()) == 0
        assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8), [])

        if args:
            assert unwrap(mask.getTheZ()) == 1
            assert unwrap(mask.getTheC()) == 2
            assert unwrap(mask.getTheT()) == 3
            assert unwrap(mask.getTextValue()) == "test"
        else:
            assert unwrap(mask.getTheZ()) is None
            assert unwrap(mask.getTheC()) is None
            assert unwrap(mask.getTheT()) is None
            assert unwrap(mask.getTextValue()) is None
Example #5
0
    def test_mask_from_binary_full_image(self, binary_image):
        binim = np.ones((4, 4))
        mask = mask_from_binary_image(binim)

        assert unwrap(mask.getWidth()) == 4
        assert unwrap(mask.getHeight()) == 4
        assert np.array_equal(mask.getBytes(),
                              np.array([255, 255], dtype=np.uint8))
Example #6
0
    def test_mask_from_binary_full_image(self):
        binim = np.ones((4, 4), dtype=np.uint8)
        im = self.conn.createImageFromNumpySeq(iter([binim]),
                                               'omero-rois-integration')
        # Reload
        im = self.conn.getObject('Image', im.id)
        px = im.getPrimaryPixels().getPlane()

        mask = mask_from_binary_image(px)
        mask = self.save_and_return_masks(im, [mask])[0]

        # The rest of this is more or less the same as the unit test

        assert unwrap(mask.getWidth()) == 4
        assert unwrap(mask.getHeight()) == 4
        assert np.array_equal(np.frombuffer(mask.getBytes(), np.uint8),
                              np.array([255, 255], dtype=np.uint8))
Example #7
0
def create_mask(mask_path, x_offset, y_offset, t, text):
    try:
        mask_data = imread(mask_path)
        mask_data = np.invert(mask_data)
        h, w = mask_data.shape[0], mask_data.shape[1]
        mask = mask_from_binary_image(mask_data,
                                      rgba=(255, 255, 255, 128),
                                      t=t,
                                      text=text)
        mask.setX(rdouble(mask.getX().getValue() + x_offset - w / 2))
        mask.setY(rdouble(mask.getY().getValue() + y_offset - h / 2))
        roi = omero.model.RoiI()
        roi.addShape(mask)
        return roi
    except Exception as e:
        print(f"Mask creation failed for {mask_path}")
        print(e)
        return None
Example #8
0
    def test_mask_from_binary_image(self, binary_image, args):
        mask = mask_from_binary_image(binary_image, **args)

        assert unwrap(mask.getWidth()) == 2
        assert unwrap(mask.getHeight()) == 2
        assert unwrap(mask.getX()) == 1
        assert unwrap(mask.getY()) == 1

        assert np.array_equal(mask.getBytes(), np.array([224], dtype=np.uint8))

        if args:
            assert unwrap(mask.getTheZ()) == 1
            assert unwrap(mask.getTheC()) == 2
            assert unwrap(mask.getTheT()) == 3
            assert unwrap(mask.getTextValue()) == 'test'
        else:
            assert unwrap(mask.getTheZ()) is None
            assert unwrap(mask.getTheC()) is None
            assert unwrap(mask.getTheT()) is None
            assert unwrap(mask.getTextValue()) is None