From 96664c3923737df0b09aa8f35df388f9fd630b55 Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Tue, 21 Oct 2025 14:38:44 +0200 Subject: [PATCH] feat(image_roi): enhance get_coordinates to include rectangle center and dimensions --- bec_widgets/cli/client.py | 6 +++--- bec_widgets/widgets/plots/roi/image_roi.py | 23 ++++++++++++++++++---- tests/unit_tests/test_image_rois.py | 2 +- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/bec_widgets/cli/client.py b/bec_widgets/cli/client.py index 84eb99ab..71324b71 100644 --- a/bec_widgets/cli/client.py +++ b/bec_widgets/cli/client.py @@ -3653,8 +3653,8 @@ class RectangularROI(RPCBase): @rpc_call def get_coordinates(self, typed: "bool | None" = None) -> "dict | tuple": """ - Returns the coordinates of a rectangle's corners. Supports returning them - as either a dictionary with descriptive keys or a tuple of coordinates. + Returns the coordinates of a rectangle's corners, rectangle center and dimensions. + Supports returning them as either a dictionary with descriptive keys or a tuple of coordinates. Args: typed (bool | None): If True, returns coordinates as a dictionary with @@ -3662,7 +3662,7 @@ class RectangularROI(RPCBase): the value of `self.description`. Returns: - dict | tuple: The rectangle's corner coordinates, where the format + dict | tuple: The rectangle's corner coordinates, rectangle center and dimensions, where the format depends on the `typed` parameter. """ diff --git a/bec_widgets/widgets/plots/roi/image_roi.py b/bec_widgets/widgets/plots/roi/image_roi.py index d21bb642..9c22f7bc 100644 --- a/bec_widgets/widgets/plots/roi/image_roi.py +++ b/bec_widgets/widgets/plots/roi/image_roi.py @@ -558,8 +558,8 @@ class RectangularROI(BaseROI, pg.RectROI): def get_coordinates(self, typed: bool | None = None) -> dict | tuple: """ - Returns the coordinates of a rectangle's corners. Supports returning them - as either a dictionary with descriptive keys or a tuple of coordinates. + Returns the coordinates of a rectangle's corners, rectangle center and dimensions. + Supports returning them as either a dictionary with descriptive keys or a tuple of coordinates. Args: typed (bool | None): If True, returns coordinates as a dictionary with @@ -567,13 +567,17 @@ class RectangularROI(BaseROI, pg.RectROI): the value of `self.description`. Returns: - dict | tuple: The rectangle's corner coordinates, where the format + dict | tuple: The rectangle's corner coordinates, rectangle center and dimensions, where the format depends on the `typed` parameter. """ if typed is None: typed = self.description x_left, y_bottom, x_right, y_top = self._normalized_edges() + width = x_right - x_left + height = y_top - y_bottom + cx = x_left + width / 2 + cy = y_bottom + height / 2 if typed: return { @@ -581,8 +585,19 @@ class RectangularROI(BaseROI, pg.RectROI): "bottom_right": (x_right, y_bottom), "top_left": (x_left, y_top), "top_right": (x_right, y_top), + "center_x": cx, + "center_y": cy, + "width": width, + "height": height, } - return (x_left, y_bottom), (x_right, y_bottom), (x_left, y_top), (x_right, y_top) + return ( + (x_left, y_bottom), + (x_right, y_bottom), + (x_left, y_top), + (x_right, y_top), + (cx, cy), + (width, height), + ) def _lookup_scene_image(self): """ diff --git a/tests/unit_tests/test_image_rois.py b/tests/unit_tests/test_image_rois.py index 936f0a63..f667a37b 100644 --- a/tests/unit_tests/test_image_rois.py +++ b/tests/unit_tests/test_image_rois.py @@ -81,7 +81,7 @@ def test_data_extraction_matches_coordinates(bec_image_widget_with_roi): # For rectangular ROI: pixel bounding box equals coordinate bbox if isinstance(roi, RectangularROI): - (x0, y0), (_, _), (_, _), (x1, y1) = roi.get_coordinates(typed=False) + (x0, y0), (_, _), (_, _), (x1, y1), *_ = roi.get_coordinates(typed=False) # ensure ints inside image shape x0, y0, x1, y1 = map(int, (x0, y0, x1, y1)) expected = widget.main_image.image[y0:y1, x0:x1]