Mercurial > repos > imgteam > 2d_auto_threshold
annotate auto_threshold.py @ 8:699a5e9146b3 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
author | imgteam |
---|---|
date | Wed, 24 Apr 2024 08:11:33 +0000 |
parents | e5c8e7e72373 |
children |
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 """ |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
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:
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 |
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:
2
diff
changeset
|
9 |
8
699a5e9146b3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents:
7
diff
changeset
|
10 import giatools.io |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
11 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:
2
diff
changeset
|
12 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
|
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 |
8
699a5e9146b3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents:
7
diff
changeset
|
16 |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
17 th_methods = { |
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
18 '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:
2
diff
changeset
|
19 |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
20 '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:
3
diff
changeset
|
21 '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:
3
diff
changeset
|
22 '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:
3
diff
changeset
|
23 '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:
3
diff
changeset
|
24 |
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
25 '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:
3
diff
changeset
|
26 '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:
3
diff
changeset
|
27 '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
|
28 } |
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
29 |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
30 |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
31 def do_thresholding(in_fn, out_fn, th_method, block_size, offset, threshold, invert_output=False): |
8
699a5e9146b3
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents:
7
diff
changeset
|
32 img = giatools.io.imread(in_fn) |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
33 img = np.squeeze(img) |
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
34 assert img.ndim == 2 |
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
35 |
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
36 th = offset + th_methods[th_method](img_raw=img, bz=block_size, thres=threshold) |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
37 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:
3
diff
changeset
|
38 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:
3
diff
changeset
|
39 res = np.logical_not(res) |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
40 |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
41 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
|
42 |
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
43 |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
44 if __name__ == "__main__": |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
45 parser = argparse.ArgumentParser(description='Automatic image thresholding') |
3
0c777d708acc
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit b1b3c63ab021aa77875c3b04127f6836024812f9"
imgteam
parents:
2
diff
changeset
|
46 parser.add_argument('im_in', help='Path to the input image') |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
47 parser.add_argument('im_out', help='Path to the output image (uint8)') |
5
7db4fc31dbee
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit 8b9f24cbfaf54f140705f0bf4b6732269bd401da
imgteam
parents:
3
diff
changeset
|
48 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:
2
diff
changeset
|
49 parser.add_argument('block_size', type=int, default=5, help='Odd size of pixel neighborhood for calculating the threshold') |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
50 parser.add_argument('offset', type=float, default=0, help='Offset of automatically determined threshold value') |
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
51 parser.add_argument('threshold', type=float, default=0, help='Manual threshold value') |
6
8bccb36e055a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit cb3d628f4c91e996e10dcb35b1168332e692293a
imgteam
parents:
5
diff
changeset
|
52 parser.add_argument('--invert_output', default=False, action='store_true', help='Values below/above the threshold are labeled with 0/255 by default, and with 255/0 if this argument is used') |
0
d4da97f51700
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_auto_threshold/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
53 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
|
54 |
7
e5c8e7e72373
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_auto_threshold/ commit c045f067a57e8308308cf6329060c7ccd3fc372f
imgteam
parents:
6
diff
changeset
|
55 do_thresholding(args.im_in, args.im_out, args.th_method, args.block_size, args.offset, args.threshold, args.invert_output) |