Mercurial > repos > imgteam > binary2labelimage
comparison 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 |
comparison
equal
deleted
inserted
replaced
4:984358e43242 | 5:7f8102bdbfa1 |
---|---|
1 import argparse | 1 import argparse |
2 import sys | |
3 | 2 |
4 import skimage.io | 3 import giatools |
5 import skimage.util | 4 import scipy.ndimage as ndi |
6 from PIL import Image | 5 import tifffile |
7 from skimage.measure import label | |
8 | 6 |
7 | |
8 # Parse CLI parameters | |
9 parser = argparse.ArgumentParser() | 9 parser = argparse.ArgumentParser() |
10 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') | 10 parser.add_argument('input', type=str, help='input file') |
11 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') | 11 parser.add_argument('output', type=str, help='output file (TIFF)') |
12 args = parser.parse_args() | 12 args = parser.parse_args() |
13 | 13 |
14 img_in = skimage.io.imread(args.input_file.name) > 0 | 14 # Read the input image with the original axes |
15 res = label(img_in) | 15 img = giatools.Image.read(args.input) |
16 res = skimage.util.img_as_uint(res) | 16 img = img.normalize_axes_like( |
17 img.original_axes, | |
18 ) | |
17 | 19 |
18 res = Image.fromarray(res) | 20 # Make sure the image is truly binary |
19 res.save(args.out_file.name, "tiff") | 21 img_arr_bin = (img.data > 0) |
22 | |
23 # Perform the labeling | |
24 img.data = ndi.label(img_arr_bin)[0] | |
25 | |
26 # Write the result image (same axes as input image) | |
27 tifffile.imwrite(args.output, img.data, metadata=dict(axes=img.axes)) |