Mercurial > repos > imgteam > binary2labelimage
annotate 2d_split_binaryimage_by_watershed.py @ 4:984358e43242 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
author | imgteam |
---|---|
date | Tue, 14 Nov 2023 08:27:27 +0000 |
parents | |
children |
rev | line source |
---|---|
4
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
1 import argparse |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
2 import sys |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
3 |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
4 import skimage.io |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
5 import skimage.util |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
6 from scipy import ndimage as ndi |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
7 from skimage.feature import peak_local_max |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
8 from skimage.morphology import watershed |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
9 |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
10 |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
11 if __name__ == "__main__": |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
12 parser = argparse.ArgumentParser(description='Split binaryimage by watershed') |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
13 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
14 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
15 parser.add_argument('min_distance', type=int, default=100, help='Minimum distance to next object') |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
16 args = parser.parse_args() |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
17 |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
18 img_in = skimage.io.imread(args.input_file.name) |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
19 distance = ndi.distance_transform_edt(img_in) |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
20 local_maxi = peak_local_max(distance, |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
21 indices=False, |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
22 min_distance=args.min_distance, |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
23 labels=img_in) |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
24 markers = ndi.label(local_maxi)[0] |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
25 res = watershed(-distance, markers, mask=img_in) |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
26 |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
27 res = skimage.util.img_as_uint(res) |
984358e43242
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binary2labelimage/ commit 00199464fa2aa1928a49e2379edf199c3db91533
imgteam
parents:
diff
changeset
|
28 skimage.io.imsave(args.out_file.name, res, plugin="tifffile") |