Mercurial > repos > imgteam > 2d_auto_threshold
comparison auto_threshold.py @ 5:7db4fc31dbee draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
| author | imgteam |
|---|---|
| date | Mon, 11 Mar 2024 17:12:33 +0000 |
| parents | 0c777d708acc |
| children | 8bccb36e055a |
comparison
equal
deleted
inserted
replaced
| 4:3df9f0a4bf34 | 5:7db4fc31dbee |
|---|---|
| 1 """ | 1 """ |
| 2 Copyright 2017-2022 Biomedical Computer Vision Group, Heidelberg University. | 2 Copyright 2017-2024 Biomedical Computer Vision Group, Heidelberg University. |
| 3 | 3 |
| 4 Distributed under the MIT license. | 4 Distributed under the MIT license. |
| 5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 6 | |
| 7 """ | 6 """ |
| 8 | 7 |
| 9 import argparse | 8 import argparse |
| 10 | 9 |
| 10 import numpy as np | |
| 11 import skimage.filters | 11 import skimage.filters |
| 12 import skimage.io | 12 import skimage.io |
| 13 import skimage.util | 13 import skimage.util |
| 14 import tifffile | 14 import tifffile |
| 15 | 15 |
| 16 thOptions = { | 16 th_methods = { |
| 17 'otsu': lambda img_raw, bz: skimage.filters.threshold_otsu(img_raw), | 17 'manual': lambda thres, **kwargs: thres, |
| 18 'li': lambda img_raw, bz: skimage.filters.threshold_li(img_raw), | |
| 19 'yen': lambda img_raw, bz: skimage.filters.threshold_yen(img_raw), | |
| 20 'isodata': lambda img_raw, bz: skimage.filters.threshold_isodata(img_raw), | |
| 21 | 18 |
| 22 'loc_gaussian': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='gaussian'), | 19 'otsu': lambda img_raw, **kwargs: skimage.filters.threshold_otsu(img_raw), |
| 23 'loc_median': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='median'), | 20 'li': lambda img_raw, **kwargs: skimage.filters.threshold_li(img_raw), |
| 24 'loc_mean': lambda img_raw, bz: skimage.filters.threshold_local(img_raw, bz, method='mean') | 21 'yen': lambda img_raw, **kwargs: skimage.filters.threshold_yen(img_raw), |
| 22 'isodata': lambda img_raw, **kwargs: skimage.filters.threshold_isodata(img_raw), | |
| 23 | |
| 24 'loc_gaussian': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='gaussian'), | |
| 25 'loc_median': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='median'), | |
| 26 'loc_mean': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='mean') | |
| 25 } | 27 } |
| 26 | 28 |
| 27 | 29 |
| 28 def auto_thresholding(in_fn, out_fn, th_method, block_size=5, dark_bg=True): | 30 def do_thresholding(in_fn, out_fn, th_method, block_size=5, threshold=0, invert_output=False): |
| 29 img = skimage.io.imread(in_fn) | 31 img = skimage.io.imread(in_fn) |
| 30 th = thOptions[th_method](img, block_size) | 32 th = th_methods[th_method](img_raw=img, bz=block_size, thres=threshold) |
| 31 if dark_bg: | 33 res = img > th |
| 32 res = img > th | 34 if invert_output: |
| 33 else: | 35 res = np.logical_not(res) |
| 34 res = img <= th | |
| 35 tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res)) | 36 tifffile.imwrite(out_fn, skimage.util.img_as_ubyte(res)) |
| 36 | 37 |
| 37 | 38 |
| 38 if __name__ == "__main__": | 39 if __name__ == "__main__": |
| 39 parser = argparse.ArgumentParser(description='Automatic Image Thresholding') | 40 parser = argparse.ArgumentParser(description='Automatic Image Thresholding') |
| 40 parser.add_argument('im_in', help='Path to the input image') | 41 parser.add_argument('im_in', help='Path to the input image') |
| 41 parser.add_argument('im_out', help='Path to the output image (TIFF)') | 42 parser.add_argument('im_out', help='Path to the output image (TIFF)') |
| 42 parser.add_argument('th_method', choices=thOptions.keys(), help='Thresholding method') | 43 parser.add_argument('th_method', choices=th_methods.keys(), help='Thresholding method') |
| 43 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold') | 44 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') | 45 parser.add_argument('threshold', type=float, default=0, help='Manual thresholding value') |
| 46 parser.add_argument('invert_output', default=False, type=bool, help='Values below/above the threshold are labeled with 0/255 if False, and with 255/0 otherwise') | |
| 45 args = parser.parse_args() | 47 args = parser.parse_args() |
| 46 | 48 |
| 47 auto_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.dark_bg) | 49 do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.threshold, args.invert_output) |
