Mercurial > repos > imgteam > binary2labelimage
diff 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 |
line wrap: on
line diff
--- a/binary2label.py Wed Dec 18 09:56:41 2019 +0000 +++ b/binary2label.py Mon May 12 08:15:32 2025 +0000 @@ -1,20 +1,27 @@ import argparse -import sys -import skimage.io -from skimage.measure import label -import numpy as np -import warnings -from PIL import Image -import skimage.util +import giatools +import scipy.ndimage as ndi +import tifffile + + +# Parse CLI parameters parser = argparse.ArgumentParser() -parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') -parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') +parser.add_argument('input', type=str, help='input file') +parser.add_argument('output', type=str, help='output file (TIFF)') args = parser.parse_args() - -img_in = skimage.io.imread(args.input_file.name) > 0 -res = label(img_in) -res = skimage.util.img_as_uint(res) + +# Read the input image with the original axes +img = giatools.Image.read(args.input) +img = img.normalize_axes_like( + img.original_axes, +) -res = Image.fromarray(res) -res.save(args.out_file.name, "tiff") +# Make sure the image is truly binary +img_arr_bin = (img.data > 0) + +# Perform the labeling +img.data = ndi.label(img_arr_bin)[0] + +# Write the result image (same axes as input image) +tifffile.imwrite(args.output, img.data, metadata=dict(axes=img.axes))