Mercurial > repos > imgteam > binary2labelimage
annotate binary2label.py @ 4:8f0dd9a58ec3 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
| author | imgteam |
|---|---|
| date | Sat, 03 Jan 2026 14:14:05 +0000 |
| parents | a041e4e9d449 |
| children |
| rev | line source |
|---|---|
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
1 import giatools |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
2 import numpy as np |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
3 import scipy.ndimage as ndi |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
4 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
5 # Fail early if an optional backend is not available |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
6 giatools.require_backend('omezarr') |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
7 |
|
0
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
8 |
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
9 def label_watershed(arr: np.ndarray, **kwargs) -> np.ndarray: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
10 import skimage.util |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
11 from skimage.feature import peak_local_max |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
12 from skimage.segmentation import watershed |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
13 distance = ndi.distance_transform_edt(arr) |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
14 local_max_indices = peak_local_max( |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
15 distance, |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
16 labels=arr, |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
17 **kwargs, |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
18 ) |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
19 local_max_mask = np.zeros(arr.shape, dtype=bool) |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
20 local_max_mask[tuple(local_max_indices.T)] = True |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
21 markers = ndi.label(local_max_mask)[0] |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
22 res = watershed(-distance, markers, mask=arr) |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
23 return skimage.util.img_as_uint(res) # converts to uint16 |
|
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
24 |
|
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
25 |
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
26 if __name__ == '__main__': |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
27 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
28 tool = giatools.ToolBaseplate() |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
29 tool.add_input_image('input') |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
30 tool.add_output_image('output') |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
31 tool.parse_args() |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
32 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
33 # Validate the input image and the selected method |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
34 try: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
35 input_image = tool.args.input_images['input'] |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
36 if (method := tool.args.params.pop('method')) == 'watershed' and input_image.shape[input_image.axes.index('Z')] > 1: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
37 raise ValueError(f'Method "{method}" is not applicable to 3-D images.') |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
38 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
39 elif input_image.shape[input_image.axes.index('C')] > 1: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
40 raise ValueError('Multi-channel images are forbidden to avoid confusion with multi-channel labels (e.g., RGB labels).') |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
41 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
42 else: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
43 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
44 # Choose the requested labeling method |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
45 match method: |
|
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
46 |
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
47 case 'cca': |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
48 joint_axes = 'ZYX' |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
49 label = lambda input_section_bin: ( # noqa: E731 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
50 ndi.label(input_section_bin, **tool.args.params)[0].astype(np.uint16) |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
51 ) |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
52 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
53 case 'watershed': |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
54 joint_axes = 'YX' |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
55 label = lambda input_section_bin: ( # noqa: E731 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
56 label_watershed(input_section_bin, **tool.args.params) # already uint16 |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
57 ) |
|
0
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
58 |
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
59 case _: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
60 raise ValueError(f'Unknown method: "{method}"') |
|
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
61 |
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
62 # Perform the labeling |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
63 for section in tool.run(joint_axes): |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
64 section['output'] = label( |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
65 section['input'].data > 0, # ensure that the input data is truly binary |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
66 ) |
|
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
67 |
|
4
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
68 # Exit and print error to stderr |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
69 except ValueError as err: |
|
8f0dd9a58ec3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit f5a4de7535e433e3b0e96e0694e481b6643a54f8
imgteam
parents:
3
diff
changeset
|
70 exit(err.args[0]) |
