Mercurial > repos > imgteam > 2d_auto_threshold
annotate auto_threshold.py @ 3:0c777d708acc draft
"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:17:40 +0000 |
parents | 81f0cbca04a7 |
children | 7db4fc31dbee |
rev | line source |
---|---|
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
1 """ |
0c777d708acc
"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. |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
3 |
0c777d708acc
"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. |
0c777d708acc
"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 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
6 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
7 """ |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
8 |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
9 import argparse |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
10 |
0c777d708acc
"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
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
12 import skimage.io |
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
13 import skimage.util |
3
0c777d708acc
"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
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
15 |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
16 thOptions = { |
0c777d708acc
"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), |
0c777d708acc
"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), |
0c777d708acc
"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), |
0c777d708acc
"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), |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
21 |
0c777d708acc
"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'), |
0c777d708acc
"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'), |
0c777d708acc
"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
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
25 } |
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
26 |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
27 |
0c777d708acc
"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): |
0c777d708acc
"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) |
0c777d708acc
"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) |
0c777d708acc
"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: |
0c777d708acc
"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 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
33 else: |
0c777d708acc
"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 |
0c777d708acc
"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)) |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
36 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
37 |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
38 if __name__ == "__main__": |
3
0c777d708acc
"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') |
0c777d708acc
"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') |
0c777d708acc
"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)') |
0c777d708acc
"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') |
0c777d708acc
"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') |
0c777d708acc
"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
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
45 args = parser.parse_args() |
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
46 |
3
0c777d708acc
"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) |