Mercurial > repos > imgteam > binary2labelimage
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 2:938e2358eb80 | 3:a041e4e9d449 |
|---|---|
| 1 import argparse | 1 import argparse |
| 2 import sys | |
| 3 import skimage.io | |
| 4 from skimage.measure import label | |
| 5 import numpy as np | |
| 6 import warnings | |
| 7 from PIL import Image | |
| 8 import skimage.util | |
| 9 | 2 |
| 3 import giatools | |
| 4 import scipy.ndimage as ndi | |
| 5 import tifffile | |
| 6 | |
| 7 | |
| 8 # Parse CLI parameters | |
| 10 parser = argparse.ArgumentParser() | 9 parser = argparse.ArgumentParser() |
| 11 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') |
| 12 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)') |
| 13 args = parser.parse_args() | 12 args = parser.parse_args() |
| 14 | |
| 15 img_in = skimage.io.imread(args.input_file.name) > 0 | |
| 16 res = label(img_in) | |
| 17 res = skimage.util.img_as_uint(res) | |
| 18 | 13 |
| 19 res = Image.fromarray(res) | 14 # Read the input image with the original axes |
| 20 res.save(args.out_file.name, "tiff") | 15 img = giatools.Image.read(args.input) |
| 16 img = img.normalize_axes_like( | |
| 17 img.original_axes, | |
| 18 ) | |
| 19 | |
| 20 # Make sure the image is truly binary | |
| 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)) |
