Mercurial > repos > imgteam > binary2labelimage
comparison 2d_split_binaryimage_by_watershed.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 | 984358e43242 |
| children |
comparison
equal
deleted
inserted
replaced
| 4:984358e43242 | 5:7f8102bdbfa1 |
|---|---|
| 1 import argparse | 1 import argparse |
| 2 import sys | 2 import sys |
| 3 | 3 |
| 4 import numpy as np | |
| 4 import skimage.io | 5 import skimage.io |
| 5 import skimage.util | 6 import skimage.util |
| 6 from scipy import ndimage as ndi | 7 from scipy import ndimage as ndi |
| 7 from skimage.feature import peak_local_max | 8 from skimage.feature import peak_local_max |
| 8 from skimage.morphology import watershed | 9 from skimage.segmentation import watershed |
| 9 | 10 |
| 10 | 11 |
| 11 if __name__ == "__main__": | 12 if __name__ == "__main__": |
| 12 parser = argparse.ArgumentParser(description='Split binaryimage by watershed') | 13 parser = argparse.ArgumentParser(description='Split binaryimage by watershed') |
| 13 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') | 14 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') |
| 15 parser.add_argument('min_distance', type=int, default=100, help='Minimum distance to next object') | 16 parser.add_argument('min_distance', type=int, default=100, help='Minimum distance to next object') |
| 16 args = parser.parse_args() | 17 args = parser.parse_args() |
| 17 | 18 |
| 18 img_in = skimage.io.imread(args.input_file.name) | 19 img_in = skimage.io.imread(args.input_file.name) |
| 19 distance = ndi.distance_transform_edt(img_in) | 20 distance = ndi.distance_transform_edt(img_in) |
| 20 local_maxi = peak_local_max(distance, | 21 |
| 21 indices=False, | 22 local_max_indices = peak_local_max( |
| 22 min_distance=args.min_distance, | 23 distance, |
| 23 labels=img_in) | 24 min_distance=args.min_distance, |
| 24 markers = ndi.label(local_maxi)[0] | 25 labels=img_in, |
| 26 ) | |
| 27 local_max_mask = np.zeros(img_in.shape, dtype=bool) | |
| 28 local_max_mask[tuple(local_max_indices.T)] = True | |
| 29 markers = ndi.label(local_max_mask)[0] | |
| 25 res = watershed(-distance, markers, mask=img_in) | 30 res = watershed(-distance, markers, mask=img_in) |
| 26 | 31 |
| 27 res = skimage.util.img_as_uint(res) | 32 res = skimage.util.img_as_uint(res) |
| 28 skimage.io.imsave(args.out_file.name, res, plugin="tifffile") | 33 skimage.io.imsave(args.out_file.name, res, plugin="tifffile") |
