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