annotate auto_local_threshold.py @ 0:a20c14eb5f98 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
author imgteam
date Thu, 18 Jul 2019 09:22:07 -0400
parents
children c90b91f4a07b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
1 import argparse
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
2 import sys
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
3 import skimage.io
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
4 import skimage.filters
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
5 import skimage.util
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
6
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
7 threshOptions = {
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
8 'gaussian': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='gaussian'),
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
9 'mean': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='mean'),
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
10 'median': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='median')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
11 }
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
12
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
13 if __name__ == "__main__":
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
14 parser = argparse.ArgumentParser(description='Segment Foci')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
15 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
16 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
17 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood which is used to calculate the threshold value')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
18 parser.add_argument('thresh_type', choices=threshOptions.keys(), help='thresholding method')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
19 parser.add_argument('dark_background', default=True, type=bool, help='True if background is dark')
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
20 args = parser.parse_args()
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
21
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
22 img_in = skimage.io.imread(args.input_file.name)
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
23 thresh = threshOptions[args.thresh_type](img_in, args.block_size)
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
24
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
25 if args.dark_background:
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
26 res = img_in > thresh
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
27 else:
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
28 res = img_in <= thresh
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
29
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
30 res = skimage.util.img_as_uint(res)
a20c14eb5f98 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_local_threshold/ commit f3903c908786b9b0ea5a46e9cc35ee025770ecda
imgteam
parents:
diff changeset
31 skimage.io.imsave(args.out_file.name, res, plugin="tifffile")