Skip to content

Commit

Permalink
Add affine transformations of ROI shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-AnChen committed Sep 27, 2024
1 parent 90b8269 commit 2d0c120
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
28 changes: 21 additions & 7 deletions ezomero/_gets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from omero.model import enums as omero_enums
from .rois import Point, Line, Rectangle
from .rois import Ellipse, Polygon, Polyline, Label, ezShape
from .rois import AffineTransform
import importlib.util

if (importlib.util.find_spec('pandas')):
Expand Down Expand Up @@ -1761,55 +1762,68 @@ def _omero_shape_to_shape(omero_shape: Shape
mk_end = omero_shape.markerEnd
except AttributeError:
mk_end = None
try:
transform = omero_shape.transform
except AttributeError:
transform = None
if transform is not None:
transform = AffineTransform(
transform.a00.val,
transform.a10.val,
transform.a01.val,
transform.a11.val,
transform.a02.val,
transform.a12.val,
)
shape: Union[Point, Line, Rectangle, Ellipse, Polygon, Polyline, Label]
if shape_type == "Point":
x = omero_shape.x
y = omero_shape.y
shape = Point(x, y, z_val, c_val, t_val, text, fill_color,
stroke_color, stroke_width)
stroke_color, stroke_width, transform=transform)
elif shape_type == "Line":
x1 = omero_shape.x1
x2 = omero_shape.x2
y1 = omero_shape.y1
y2 = omero_shape.y2
shape = Line(x1, y1, x2, y2, z_val, c_val, t_val, mk_start, mk_end,
text, fill_color, stroke_color, stroke_width)
text, fill_color, stroke_color, stroke_width, transform=transform)
elif shape_type == "Rectangle":
x = omero_shape.x
y = omero_shape.y
width = omero_shape.width
height = omero_shape.height
shape = Rectangle(x, y, width, height, z_val, c_val, t_val, text,
fill_color, stroke_color, stroke_width)
fill_color, stroke_color, stroke_width, transform=transform)
elif shape_type == "Ellipse":
x = omero_shape.x
y = omero_shape.y
radiusX = omero_shape.radiusX
radiusY = omero_shape.radiusY
shape = Ellipse(x, y, radiusX, radiusY, z_val, c_val, t_val, text,
fill_color, stroke_color, stroke_width)
fill_color, stroke_color, stroke_width, transform=transform)
elif shape_type == "Polygon":
omero_points = omero_shape.points.split()
points = []
for point in omero_points:
coords = point.split(',')
points.append((float(coords[0]), float(coords[1])))
shape = Polygon(points, z_val, c_val, t_val, text, fill_color,
stroke_color, stroke_width)
stroke_color, stroke_width, transform=transform)
elif shape_type == "Polyline":
omero_points = omero_shape.points.split()
points = []
for point in omero_points:
coords = point.split(',')
points.append((float(coords[0]), float(coords[1])))
shape = Polyline(points, z_val, c_val, t_val, text, fill_color,
stroke_color, stroke_width)
stroke_color, stroke_width, transform=transform)
elif shape_type == "Label":
x = omero_shape.x
y = omero_shape.y
fsize = omero_shape.getFontSize().getValue()
shape = Label(x, y, text, fsize, z_val, c_val, t_val, fill_color,
stroke_color, stroke_width)
stroke_color, stroke_width, transform=transform)
else:
err = 'The shape passed for the roi is not a valid shape type'
raise TypeError(err)
Expand Down
17 changes: 16 additions & 1 deletion ezomero/_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from omero.gateway import BlitzGateway
from omero.model import RoiI, PointI, LineI, RectangleI, EllipseI
from omero.model import PolygonI, PolylineI, LabelI, LengthI, enums
from omero.model import DatasetI, ProjectI, ScreenI, Shape
from omero.model import DatasetI, ProjectI, ScreenI, Shape, AffineTransformI
from omero.grid import BoolColumn, LongColumn
from omero.grid import StringColumn, DoubleColumn, Column
from omero.gateway import ProjectWrapper, DatasetWrapper
Expand Down Expand Up @@ -752,6 +752,17 @@ def create_columns(table: Any,
return cols


def _omero_affine(a00, a10, a01, a11, a02, a12):
affine = AffineTransformI()
affine.a00 = rdouble(a00)
affine.a10 = rdouble(a10)
affine.a01 = rdouble(a01)
affine.a11 = rdouble(a11)
affine.a02 = rdouble(a02)
affine.a12 = rdouble(a12)
return affine


def _shape_to_omero_shape(shape: Union[Point, Line, Rectangle, Ellipse,
Polygon, Polyline, Label]) -> Shape:
""" Helper function to convert ezomero shapes into omero shapes"""
Expand Down Expand Up @@ -822,6 +833,10 @@ def _shape_to_omero_shape(shape: Union[Point, Line, Rectangle, Ellipse,
enums.UnitsLength.PIXEL))
else:
omero_shape.setStrokeWidth(LengthI(1.0, enums.UnitsLength.PIXEL))
if shape.transform is not None:
t_ = shape.transform
omero_shape.transform = _omero_affine(t_.a00, t_.a10, t_.a01,
t_.a11, t_.a02, t_.a12)
return omero_shape


Expand Down
15 changes: 14 additions & 1 deletion ezomero/rois.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
"Polygon",
"Polyline",
"Label",
"ezShape"]
"ezShape",
"AffineTransform"]


@dataclass(frozen=True)
class AffineTransform():
"""A dataclss for affine transformation matrix of shapes"""
a00: float = field(default=1.0)
a10: float = field(default=0.0)
a01: float = field(default=0.0)
a11: float = field(default=1.0)
a02: float = field(default=0.0)
a12: float = field(default=0.0)


@dataclass(frozen=True)
Expand All @@ -20,6 +32,7 @@ class ezShape:
This dataclass is frozen and should not be modified after instantiation
"""
transform: Union[AffineTransform, None] = field(default=None, kw_only=True)


@dataclass(frozen=True)
Expand Down

0 comments on commit 2d0c120

Please sign in to comment.