Mercurial > repos > imgteam > 2d_auto_threshold
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 2:81f0cbca04a7 | 3:0c777d708acc |
|---|---|
| 1 """ | |
| 2 Copyright 2017-2022 Biomedical Computer Vision Group, Heidelberg University. | |
| 3 | |
| 4 Distributed under the MIT license. | |
| 5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | |
| 6 | |
| 7 """ | |
| 8 | |
| 1 import argparse | 9 import argparse |
| 2 import numpy as np | 10 |
| 3 import sys | 11 import skimage.filters |
| 4 import skimage.io | 12 import skimage.io |
| 5 import skimage.filters | |
| 6 import skimage.util | 13 import skimage.util |
| 14 import tifffile | |
| 7 | 15 |
| 8 threshOptions = { | 16 thOptions = { |
| 9 'otsu': lambda img_raw: skimage.filters.threshold_otsu(img_raw), | 17 'otsu': lambda img_raw, bz: skimage.filters.threshold_otsu(img_raw), |
| 10 'gaussian_adaptive': lambda img_raw: skimage.filters.threshold_local(img_raw, 3, method='gaussian'), | 18 'li': lambda img_raw, bz: skimage.filters.threshold_li(img_raw), |
| 11 'mean_adaptive': lambda img_raw: skimage.filters.threshold_local(img_raw, 3, method='mean'), | 19 'yen': lambda img_raw, bz: skimage.filters.threshold_yen(img_raw), |
| 12 'isodata': lambda img_raw: skimage.filters.threshold_isodata(img_raw), | 20 'isodata': lambda img_raw, bz: skimage.filters.threshold_isodata(img_raw), |
| 13 'li': lambda img_raw: skimage.filters.threshold_li(img_raw), | 21 |
| 14 'yen': lambda img_raw: skimage.filters.threshold_yen(img_raw), | 22 'loc_gaussian': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='gaussian'), |
| 23 'loc_median': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='median'), | |
| 24 'loc_mean': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='mean') | |
| 15 } | 25 } |
| 16 | 26 |
| 27 | |
| 28 def auto_thresholding(in_fn, out_fn, th_method, block_size=5, dark_bg=True): | |
| 29 img = skimage.io.imread(in_fn) | |
| 30 th = thOptions[th_method](img, block_size) | |
| 31 if dark_bg: | |
| 32 res = img > th | |
| 33 else: | |
| 34 res = img <= th | |
| 35 tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res)) | |
| 36 | |
| 37 | |
| 17 if __name__ == "__main__": | 38 if __name__ == "__main__": |
| 18 parser = argparse.ArgumentParser(description='Segment Foci') | 39 parser = argparse.ArgumentParser(description='Automatic Image Thresholding') |
| 19 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file') | 40 parser.add_argument('im_in', help='Path to the input image') |
| 20 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)') | 41 parser.add_argument('im_out', help='Path to the output image (TIFF)') |
| 21 parser.add_argument('thresh_type', choices=threshOptions.keys(), help='thresholding method') | 42 parser.add_argument('th_method', choices=thOptions.keys(), help='Thresholding method') |
| 22 parser.add_argument('dark_background', default=True, type=bool, help='True if background is dark') | 43 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold') |
| 44 parser.add_argument('dark_bg', default=True, type=bool, help='True if background is dark') | |
| 23 args = parser.parse_args() | 45 args = parser.parse_args() |
| 24 | 46 |
| 25 img_in = skimage.io.imread(args.input_file.name) | 47 auto_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.dark_bg) |
| 26 img_in = np.reshape(img_in, [img_in.shape[0], img_in.shape[1]]) | |
| 27 thresh = threshOptions[args.thresh_type](img_in) | |
| 28 | |
| 29 if args.dark_background: | |
| 30 res = img_in > thresh | |
| 31 else: | |
| 32 res = img_in <= thresh | |
| 33 | |
| 34 res = skimage.util.img_as_uint(res) | |
| 35 skimage.io.imsave(args.out_file.name, res, plugin="tifffile") |
