annotate auto_local_threshold.py @ 3:0bbcb6a31a20 draft default tip

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