Mercurial > repos > imgteam > 2d_auto_threshold
annotate 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 | 
| rev | line source | 
|---|---|
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 1 """ | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 2 Copyright 2017-2024 Biomedical Computer Vision Group, Heidelberg University. | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 3 | 
| 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
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: 
2diff
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: 
2diff
changeset | 6 """ | 
| 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 7 | 
| 0 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 imgteam parents: diff
changeset | 8 import argparse | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 9 | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 10 import numpy as np | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
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: 
2diff
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 | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 16 th_methods = { | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 17 'manual': lambda thres, **kwargs: thres, | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 18 | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 19 'otsu': lambda img_raw, **kwargs: skimage.filters.threshold_otsu(img_raw), | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 20 'li': lambda img_raw, **kwargs: skimage.filters.threshold_li(img_raw), | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 21 'yen': lambda img_raw, **kwargs: skimage.filters.threshold_yen(img_raw), | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 22 'isodata': lambda img_raw, **kwargs: skimage.filters.threshold_isodata(img_raw), | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 23 | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 24 'loc_gaussian': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='gaussian'), | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 25 'loc_median': lambda img_raw, bz, **kwargs: skimage.filters.threshold_local(img_raw, bz, method='median'), | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 26 'loc_mean': lambda img_raw, bz, **kwargs: 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 | 27 } | 
| 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 imgteam parents: diff
changeset | 28 | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 29 | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 30 def do_thresholding(in_fn, out_fn, th_method, block_size=5, threshold=0, invert_output=False): | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 31 img = skimage.io.imread(in_fn) | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 32 th = th_methods[th_method](img_raw=img, bz=block_size, thres=threshold) | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 33 res = img > th | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 34 if invert_output: | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 35 res = np.logical_not(res) | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 36 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: 
2diff
changeset | 37 | 
| 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 38 | 
| 0 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 imgteam parents: diff
changeset | 39 if __name__ == "__main__": | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 40 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: 
2diff
changeset | 41 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: 
2diff
changeset | 42 parser.add_argument('im_out', help='Path to the output image (TIFF)') | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 43 parser.add_argument('th_method', choices=th_methods.keys(), help='Thresholding method') | 
| 3 
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
 imgteam parents: 
2diff
changeset | 44 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold') | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 45 parser.add_argument('threshold', type=float, default=0, help='Manual thresholding value') | 
| 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 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') | 
| 0 
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
 imgteam parents: diff
changeset | 47 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 | 48 | 
| 5 
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
 imgteam parents: 
3diff
changeset | 49 do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.threshold, args.invert_output) | 
