annotate auto_threshold.py @ 3:5224cc463a97 draft default tip

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
author imgteam
date Sat, 19 Feb 2022 15:16:52 +0000
parents f40e5d11eb47
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
1 """
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
2 Copyright 2017-2022 Biomedical Computer Vision Group, Heidelberg University.
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
4 Distributed under the MIT license.
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
6
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
7 """
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
8
0
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
9 import argparse
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
10
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
11 import skimage.filters
0
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
12 import skimage.io
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
13 import skimage.util
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
14 import tifffile
0
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
15
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
16 thOptions = {
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
17 'otsu': lambda img_raw, bz: skimage.filters.threshold_otsu(img_raw),
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
18 'li': lambda img_raw, bz: skimage.filters.threshold_li(img_raw),
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
19 'yen': lambda img_raw, bz: skimage.filters.threshold_yen(img_raw),
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
20 'isodata': lambda img_raw, bz: skimage.filters.threshold_isodata(img_raw),
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
21
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
22 'loc_gaussian': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='gaussian'),
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
23 'loc_median': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='median'),
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
24 'loc_mean': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='mean')
0
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
25 }
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
26
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
27
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
28 def auto_thresholding(in_fn, out_fn, th_method, block_size=5, dark_bg=True):
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
29 img = skimage.io.imread(in_fn)
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
30 th = thOptions[th_method](img, block_size)
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
31 if dark_bg:
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
32 res = img > th
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
33 else:
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
34 res = img <= th
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
35 tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res))
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
36
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
37
0
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
38 if __name__ == "__main__":
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
39 parser = argparse.ArgumentParser(description='Automatic Image Thresholding')
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
40 parser.add_argument('im_in', help='Path to the input image')
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
41 parser.add_argument('im_out', help='Path to the output image (TIFF)')
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
42 parser.add_argument('th_method', choices=thOptions.keys(), help='Thresholding method')
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
43 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold')
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
44 parser.add_argument('dark_bg', default=True, type=bool, help='True if background is dark')
0
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
45 args = parser.parse_args()
6fc65082d1e6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit bd6ef77515c4c15901b67f73738afbdd5faadae5
imgteam
parents:
diff changeset
46
3
5224cc463a97 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents: 2
diff changeset
47 auto_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.dark_bg)