Mercurial > repos > imgteam > binary2labelimage
annotate binary2label.py @ 3:a041e4e9d449 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
author | imgteam |
---|---|
date | Mon, 12 May 2025 08:15:32 +0000 |
parents | 938e2358eb80 |
children |
rev | line source |
---|---|
0
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
1 import argparse |
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
2 |
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
3 import giatools |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
4 import scipy.ndimage as ndi |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
5 import tifffile |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
6 |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
7 |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
8 # Parse CLI parameters |
0
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
9 parser = argparse.ArgumentParser() |
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
10 parser.add_argument('input', type=str, help='input file') |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
11 parser.add_argument('output', type=str, help='output file (TIFF)') |
0
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
12 args = parser.parse_args() |
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
13 |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
14 # Read the input image with the original axes |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
15 img = giatools.Image.read(args.input) |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
16 img = img.normalize_axes_like( |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
17 img.original_axes, |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
18 ) |
0
1cde6ba34356
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binary2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
19 |
3
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
20 # Make sure the image is truly binary |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
21 img_arr_bin = (img.data > 0) |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
22 |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
23 # Perform the labeling |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
24 img.data = ndi.label(img_arr_bin)[0] |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
25 |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
26 # Write the result image (same axes as input image) |
a041e4e9d449
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 48df7d9c58fb88e472caeb4d4a1e14170d79b643
imgteam
parents:
2
diff
changeset
|
27 tifffile.imwrite(args.output, img.data, metadata=dict(axes=img.axes)) |