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