Skip to content

omero/rois.py

Helps access ROIs from OMERO image objects

create_roi(img, shapes)

Creates an omero RoiI object for an image from an array of shapes.

Parameters:

Name Type Description Default
img ImageWrapper

Omero Image object from conn.getObjects().

required
shapes list[ShapeWrapper]

List of omero shape objects (Polygon, Rectangle, etc).

required
Returns
required
omero_model_RoiI

Local Omero ROI object, needs to be saved

required
Source code in lavlab/omero/rois.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def create_roi(img: ImageWrapper, shapes: list[ShapeWrapper]):
    """
    Creates an omero RoiI object for an image from an array of shapes.

    Parameters
    ----------
    img: omero.gateway.ImageWrapper
        Omero Image object from conn.getObjects().
    shapes: list[omero_model_ShapeI.ShapeI]
        List of omero shape objects (Polygon, Rectangle, etc).

    Returns
    omero_model_RoiI.RoiI
        Local Omero ROI object, needs to be saved
    -------

    """
    # create an ROI, link it to Image
    roi = RoiI()
    # use the omero.model.ImageI that underlies the 'image' wrapper
    roi.setImage(img._obj)  # pylint: disable=W0212
    for shape in shapes:
        roi.addShape(shape)
    return roi

get_rois(img, roi_service=None)

Gathers OMERO RoiI objects.

Parameters:

Name Type Description Default
img ImageWrapper

Omero Image object from conn.getObjects()

required
roi_service

Allows roiservice passthrough for performance

None
Source code in lavlab/omero/rois.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def get_rois(img: ImageWrapper, roi_service=None) -> RoiWrapper:
    """
    Gathers OMERO RoiI objects.

    Parameters
    ----------
    img: omero.gateway.ImageWrapper
        Omero Image object from conn.getObjects()
    roi_service: omero.RoiService, optional
        Allows roiservice passthrough for performance
    """
    if roi_service is None:
        roi_service = img._conn.getRoiService()  # pylint: disable=W0212
        close_roi = True
    else:
        close_roi = False

    rois = roi_service.findByImage(
        img.getId(), None, img._conn.SERVICE_OPTS  # pylint: disable=W0212
    ).rois
    rois = [RoiWrapper(img._conn, roi) for roi in rois]  # pylint: disable=W0212
    if close_roi:
        roi_service.close()

    return rois

get_shapes_as_points(img, point_downsample=4, img_downsample=1, roi_service=None)

Gathers Rectangles, Polygons, and Ellipses as a tuple containing the shapeId, its rgb val, and a tuple of xy points of its bounds.

Parameters:

Name Type Description Default
img ImageWrapper

Omero Image object from conn.getObjects().

required
point_downsample

Grabs every nth point for faster computation.

4
img_downsample

How much to scale roi points.

1
roi_service

Allows roiservice passthrough for performance.

None

Returns:

Name Type Description
returns list[id, (r, g, b), list[tuple(x, y)]]

list of tuples containing a shape's id, rgb value, and a tuple of row and column points

Source code in lavlab/omero/rois.py
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def get_shapes_as_points(  # pylint: disable=R0914
    img: ImageWrapper, point_downsample=4, img_downsample=1, roi_service=None
) -> list[tuple[int, tuple[int, int, int], list[tuple[float, float]]]]:
    """
    Gathers Rectangles, Polygons, and Ellipses as a tuple containing the
    shapeId, its rgb val, and a tuple of xy points of its bounds.

    Parameters
    ----------
    img: omero.gateway.ImageWrapper
        Omero Image object from conn.getObjects().
    point_downsample: int, Default: 4
        Grabs every nth point for faster computation.
    img_downsample: int, Default: 1
        How much to scale roi points.
    roi_service: omero.RoiService, optional
        Allows roiservice passthrough for performance.

    Returns
    -------
    returns: list[ shape.id, (r,g,b), list[tuple(x,y)] ]
        list of tuples containing a shape's id, rgb value, and a tuple of row and column points
    """
    yx_shape = (img.getSizeY() / img_downsample, img.getSizeX() / img_downsample)

    shapes = []
    for roi in get_rois(img, roi_service):
        points = None
        for shape in roi.copyShapes():

            if isinstance(shape, RectangleI):
                x = float(shape.getX().getValue()) / img_downsample
                y = float(shape.getY().getValue()) / img_downsample
                w = float(shape.getWidth().getValue()) / img_downsample
                h = float(shape.getHeight().getValue()) / img_downsample
                # points = [(x, y),(x+w, y), (x+w, y+h), (x, y+h), (x, y)]
                points = draw.rectangle_perimeter(
                    (y, x), (y + h, x + w), shape=yx_shape
                )
                points = [(points[1][i], points[0][i]) for i in range(len(points[0]))]

            if isinstance(shape, EllipseI):
                points = draw.ellipse_perimeter(
                    float(shape.getY().getValue() / img_downsample),
                    float(shape.getX().getValue() / img_downsample),
                    float(shape.getRadiusY().getValue() / img_downsample),
                    float(shape.getRadiusX().getValue() / img_downsample),
                    shape=yx_shape,
                )
                points = [(points[1][i], points[0][i]) for i in range(len(points[0]))]

            if isinstance(shape, PolygonI):
                point_string_array = shape.getPoints().getValue().split(" ")

                xy: list[tuple[float, float]] = []
                for i, points in enumerate(point_string_array):
                    coords = points.split(",")
                    xy.append(
                        (
                            float(coords[0]) / img_downsample,
                            float(coords[1]) / img_downsample,
                        )
                    )
                if xy:
                    points = xy

            if points is not None:
                color_val = shape.getStrokeColor().getValue()

                r, g, b, _ = uint_to_rgba(color_val)

                points = [
                    (float(x), float(y))
                    for x, y in zip(
                        points[0][::point_downsample], points[1][::point_downsample]
                    )
                ]

                # Ensure shape ID is an integer
                shape_id = int(shape.getId().getValue())
                shapes.append((shape_id, (r, g, b), points))

    # Sorting by the first element of the tuple which is the ID
    return sorted(shapes, key=lambda x: x[0])