diff points2label.py @ 3:2ae122d5d85a draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
author imgteam
date Wed, 23 Apr 2025 14:37:31 +0000
parents 714a57d6f3a1
children 64c155acb864
line wrap: on
line diff
--- a/points2label.py	Fri Sep 27 17:41:06 2024 +0000
+++ b/points2label.py	Wed Apr 23 14:37:31 2025 +0000
@@ -33,6 +33,17 @@
                 radius_list = [0] * len(pos_x_list)
 
             try:
+                width_column = giatools.pandas.find_column(df, ['width', 'WIDTH'])
+                height_column = giatools.pandas.find_column(df, ['height', 'HEIGHT'])
+                width_list = df[width_column]
+                height_list = df[height_column]
+                assert len(pos_x_list) == len(width_list)
+                assert len(pos_x_list) == len(height_list)
+            except KeyError:
+                width_list = [0] * len(pos_x_list)
+                height_list = [0] * len(pos_x_list)
+
+            try:
                 label_column = giatools.pandas.find_column(df, ['label', 'LABEL'])
                 label_list = df[label_column]
                 assert len(pos_x_list) == len(label_list)
@@ -45,7 +56,7 @@
             pos_x_list = df[0].round().astype(int)
             pos_y_list = df[1].round().astype(int)
             assert len(pos_x_list) == len(pos_y_list)
-            radius_list = [0] * len(pos_x_list)
+            radius_list, width_list, height_list = [[0] * len(pos_x_list)] * 3
             label_list = list(range(1, len(pos_x_list) + 1))
 
         # Optionally swap the coordinates
@@ -53,7 +64,9 @@
             pos_x_list, pos_y_list = pos_y_list, pos_x_list
 
         # Perform the rasterization
-        for y, x, radius, label in zip(pos_y_list, pos_x_list, radius_list, label_list):
+        for y, x, radius, width, height, label in zip(
+            pos_y_list, pos_x_list, radius_list, width_list, height_list, label_list,
+        ):
             if fg_value is not None:
                 label = fg_value
 
@@ -61,10 +74,23 @@
                 raise IndexError(f'The point x={x}, y={y} exceeds the bounds of the image (width: {shape[1]}, height: {shape[0]})')
 
             # Rasterize circle and distribute overlapping image area
-            if radius > 0:
-                mask = np.ones(shape, dtype=bool)
-                mask[y, x] = False
-                mask = (ndi.distance_transform_edt(mask) <= radius)
+            # Rasterize primitive geometry
+            if radius > 0 or (width > 0 and height > 0):
+
+                # Rasterize circle
+                if radius > 0:
+                    mask = np.ones(shape, dtype=bool)
+                    mask[y, x] = False
+                    mask = (ndi.distance_transform_edt(mask) <= radius)
+                else:
+                    mask = np.zeros(shape, dtype=bool)
+
+                # Rasterize rectangle
+                if width > 0 and height > 0:
+                    mask[
+                        y:min(shape[0], y + width),
+                        x:min(shape[1], x + height)
+                    ] = True
 
                 # Compute the overlap (pretend there is none if the rasterization is binary)
                 if fg_value is None: